【问题标题】:How to read each line of a QPlainTextEdit in Qt?如何在 Qt 中读取 QPlainTextEdit 的每一行?
【发布时间】:2011-11-13 16:50:14
【问题描述】:

我想制作一个程序,在其中获取 QPlainTextEdit 的每一行并将其发送到将加载这些 url 的 WebView。我不需要检查 URL,因为系统是这样的

http://someurl.com/ + each line of the QPlainTextEdit

我有一些我不知道如何使用的想法:

  1. 使用 foreach 循环,使其自身等待 5 秒以再次循环
  2. 让 QTimer 等待 5 秒,然后用一个整数打勾,当整数达到行数时,它将停止

所有这些都将通过等待另一个计时器每 4 小时完成一次。

【问题讨论】:

  • "我想制作一个程序,在其中获取 QPlainTextEdit 的每一行并将其发送到将加载这些 url 的 WebView。" 你没有阅读我输入的内容

标签: qt qt4 timer webkit webview


【解决方案1】:

首先你需要QPlainTextEdit的内容。获取它们并使用新的行分隔符将它们拆分,以获得QStrings 的列表,每个代表一行。

QString plainTextEditContents = ui->plainTextEdit->toPlainText()
QStringList lines = plainTextEditContents.split("\n");

处理这些行的最简单方法是使用QTimer 并将当前索引存储在列表中的某个位置。

// Start the timer
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(processLine()));
timer->start(5000);

现在,只要触发计时器,就会调用插槽。它只是获取当前行,您可以随心所欲地进行操作。

void processLine(){
   // This is the current index in the string list. If we have reached the end
   // then we stop the timer.
   currentIndex ++;

   if (currentIndex == lines.count())
   {
       timer.stop();
       currentIndex = 0; 
       return;
   }

   QString currentLine = lines[currentIndex];
   doSomethingWithTheLine(currentLine); 
}

对 4 小时计时器进行同样的操作。

【讨论】:

    猜你喜欢
    • 2016-04-15
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多