如何在Qt中读取QPlainTextEdit的每一行?

use*_*359 3 qt webkit qt4 timer webview

我想创建一个程序,我将每行QPlainTextEdit发送到WebView,它将加载这些URL.我不需要检查URL,因为系统就是这样

http://someurl.com/ + each line of the QPlainTextEdit
Run Code Online (Sandbox Code Playgroud)

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

  1. 使用foreach循环,使其自我等待5秒钟再次循环
  2. 让QTimer等待5秒并用整数打勾,当整数达到它将停止的行数时

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

pne*_*zis 6

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

QString plainTextEditContents = ui->plainTextEdit->toPlainText()
QStringList lines = plainTextEditContents.split("\n");
Run Code Online (Sandbox Code Playgroud)

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

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

现在,只要触发定时器,就会调用插槽.它只是得到当前的行,你可以随心所欲地做到这一点.

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); 
}
Run Code Online (Sandbox Code Playgroud)

同样地用4h计时器做同样的事情.