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. I use QThread but UI is frozen
Qt 6.11 is out! See what's new in the release blog

I use QThread but UI is frozen

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 233 Views 1 Watching
  • 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.
  • P Offline
    P Offline
    popcon
    wrote on last edited by popcon
    #1

    I need to collect information and count all files in directory. Having used QThread I hoped to "unfroze" UI during scanning. But it didnt work. FileScaner inherits QObject.

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        QFileSystemModel model;
        QTreeView treeView;
        QThread thread;
        FileScaner scaner;
    }
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow){
    ...
        scaner.moveToThread(&thread);
        thread.start();
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        QModelIndexList ind;
        ind=treeView.selectionModel()->selectedIndexes();
        scaner.scan(model.filePath(ind.last()));
    }
    
    Any ideas what's the problem?
    
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      You're calling scaner.scan directly on the ui thread so it's blocking. Moving an object to another thread doesn't mean its members are gonna be run there when invoked directly.

      To run it on the worker thread either use signal/slot or QMetaObejct::invokeMethod.

      Btw. you should really check if ind is not empty before calling last() on it.

      1 Reply Last reply
      6

      • Login

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