Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QObject::startTimer: Timers cannot be started from another thread

QObject::startTimer: Timers cannot be started from another thread

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 304 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • eefesafakE Offline
    eefesafakE Offline
    eefesafak
    wrote on last edited by eefesafak
    #1

    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;
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You must not access ui elements from outside the main (gui) thread!

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      eefesafakE 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        You must not access ui elements from outside the main (gui) thread!

        eefesafakE Offline
        eefesafakE Offline
        eefesafak
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved