QTextEdit 读取一行 删除一行

QTextEdit 读取一行 删除一行

建议使用QPlainTextEdit控件的setMaximumBlockCount(int)函数控制总行数,并且使用setUndoRedoEnabled(false),可以有效控制内存增长。

QPlainTextEdit控件的maximumBlockCount : int属性说明如下:
此属性保留文档中块的限制。
指定文档可能具有的最大块数。如果文档中有更多使用此属性指定的块,则会从文档开头删除这些块。
负值或零值指定文档可以包含无限量的块。
默认值为0。
请注意,设置此属性将立即对文档内容应用限制。设置此属性也会禁用撤消重做历史记录。

maximumBlockCount : int
This property holds the limit for blocks in the document.
Specifies the maximum number of blocks the document may have. If there are more blocks in the document that specified with this property blocks are removed from the beginning of the document.
A negative or zero value specifies that the document may contain an unlimited amount of blocks.
The default value is 0.
Note that setting this property will apply the limit immediately to the document contents. Setting this property also disables the undo redo history.
Access functions:

int  maximumBlockCount() const
void setMaximumBlockCount(int maximum)

QPlainTextEdit控件的undoRedoEnabled : bool属性说明如下:
此属性保存是否启用了撤消和重做
如果此属性为true,并且存在可以撤消(或重做)的操作,则用户只能撤消或重做操作。
默认情况下,此属性为true。

undoRedoEnabled : bool
This property holds whether undo and redo are enabled
Users are only able to undo or redo actions if this property is true, and if there is an action that can be undone (or redone).
By default, this property is true.
Access functions:

bool  isUndoRedoEnabled() const
void  setUndoRedoEnabled(bool enable)

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中,我们可以通过使用QFile类来读取文件的每一行,然后将每行的内容放入不同的QTextEdit部件中。 首先,我们需要在Qt项目中包含必要的头文件: ```c++ #include <QFile> #include <QTextStream> ``` 接下来,我们可以创建一个槽函数来处理读取文件的操作,并将每行内容分别放入不同的QTextEdit部件中: ```c++ void MainWindow::readFile() { // 创建QFile对象并打开文件 QFile file("input.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; // 创建QTextStream对象并绑定到QFile对象 QTextStream in(&file); // 创建多个QTextEdit对象,用于显示每行的内容 QTextEdit* textEdit1 = new QTextEdit(this); QTextEdit* textEdit2 = new QTextEdit(this); QTextEdit* textEdit3 = new QTextEdit(this); // 设置布局管理器,将文本部件添加到窗口中 QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(textEdit1); layout->addWidget(textEdit2); layout->addWidget(textEdit3); this->setLayout(layout); // 读取文件的每一行并将内容放入文本部件中 QString line; int count = 0; while (!in.atEnd()) { line = in.readLine(); if (count == 0) textEdit1->setText(textEdit1->toPlainText() + line); else if (count == 1) textEdit2->setText(textEdit2->toPlainText() + line); else if (count == 2) textEdit3->setText(textEdit3->toPlainText() + line); count++; } // 关闭文件 file.close(); } ``` 在这个例子中,我们假设有一个名为"input.txt"的文本文件,其中包含了三行内容。我们创建了三个QTextEdit部件,并将每行的内容分别放入这些部件中。 记得在MainWindow类中添加这个槽函数,并在适当的地方调用`readFile()`函数,比如在构造函数中或者在按下相关按钮时。 这样,每读取一行,就会将该行的内容添加到相应的QTextEdit部件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值