QObject::startTimer: Timers cannot be started from another thread
Solved
General and Desktop
-
Hello,
My program works well as intended. But I got an error like this:QObject::startTimer: Timers cannot be started from another thread
If you do not mind, can you help me to solve this error?
I suppose, the error comes from QtConcurrent::run() function.
Here is my code:#include "mainwindow.h" #include "qheaderview.h" #include "qtablewidget.h" #include "ui_mainwindow.h" #include "countline.h" #include <QtConcurrentRun> #include <QMutexLocker> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setReturnState(false); ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed); ui->tableWidget->setColumnWidth(0, 782); ui->tableWidget->setColumnWidth(1, 150); ui->tableWidget->setColumnWidth(2, 150); } MainWindow::~MainWindow() { delete ui; } void MainWindow::addItemToList(const QString &file, const int &fileSize, const int &count) { QTableWidgetItem *iFileName = new QTableWidgetItem(file); QTableWidgetItem *iFileSize = new QTableWidgetItem(QString::number(fileSize)); QTableWidgetItem *iFileCount = new QTableWidgetItem(QString::number(count)); int rowCount = ui->tableWidget->rowCount(); ui->tableWidget->insertRow(rowCount); rowCount = ui->tableWidget->rowCount(); ui->tableWidget->setItem(rowCount-1, 0, iFileName); ui->tableWidget->setItem(rowCount-1, 1, iFileSize); ui->tableWidget->setItem(rowCount-1, 2, iFileCount); } QFileInfoList MainWindow::getFileListFromDir(const QString &directory) { QDir qdir(directory); QString include = ui->lineEdit->text(); QString except = ui->lineEdit2->text(); QStringList includeList = include.split(QLatin1Char(';')); QStringList exceptList = except.split(QLatin1Char(';')); QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size); for(const QFileInfo& file : qdir.entryInfoList(QStringList() << exceptList)) { for(const QFileInfo& filee: fileList) { if(file.fileName() == filee.fileName()) fileList.removeOne(filee); } } for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot)) { fileList << getFileListFromDir(subDirectory.absoluteFilePath()); } ui->lineEdit->setText(ui->lineEdit->displayText()); return fileList; } void *MainWindow::process(const QString& path) { ui->Ok->setEnabled(false); ui->cancel->setEnabled(true); ui->tableWidget->setRowCount(0); QFileInfoList fileList = getFileListFromDir(path); int count = 0; int sum = 0; foreach(const QFileInfo& file, fileList) { if(getReturnState()) break; count = m_ig->funcCountLines(file.filePath()); addItemToList(file.filePath(), file.size(), count); sum += count; } ui->label->setText(QString::number(sum)); ui->Ok->setEnabled(true); ui->cancel->setEnabled(false); setReturnState(false); } bool MainWindow::getReturnState() { QMutexLocker locker(&m_mutex); return m_return; } void MainWindow::setReturnState(bool state) { QMutexLocker locker(&m_mutex); m_return = state; } void MainWindow::on_Browse_clicked() { QString path = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home/efe/Desktop/build-SatirSay-Unnamed2-Release", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); if(path.isEmpty()) return; ui->FullPath->setText(path); } void MainWindow::on_Ok_clicked() { QString path = ui->FullPath->text(); if(path.isEmpty()) return; QtConcurrent::run(this, &MainWindow::process, path); } void MainWindow::on_cancel_clicked() { m_return = true; }
-
You must not access ui elements from outside the main (gui) thread!
-
You must not access ui elements from outside the main (gui) thread!
@Christian-Ehrlicher Thanks.