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. How to show dialog on thread other.

How to show dialog on thread other.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 4.9k 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.
  • H Offline
    H Offline
    HAHAHAHAHA
    wrote on last edited by
    #1

    Class MyDialog have QtableWidget and QPushButon and load 100000 row:

    DialogUser::DialogUser(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::DialogUser)
    {
        ui->setupUi(this);
        int row = 100000;
        ui->tableWidget->setRowCount(row);
        QString select("No, Name1, Name2");
        QStringList labels = select.split(", ");
        ui->tableWidget->setColumnCount(labels.count());
        ui->tableWidget->setHorizontalHeaderLabels(labels);
        QTableWidgetItem *item_name1;
        QTableWidgetItem *item_name2 ;
        QTableWidgetItem *item_ID ;
    
        for (int i = 0; i < row; i++)
        {
            item_name1 = new QTableWidgetItem();
            item_name2 = new QTableWidgetItem();
            item_ID = new QTableWidgetItem();
            item_name1->setText("Name1::"+QString::number(i));
            item_name2->setText("Name2::"+QString::number(i));
            item_ID->setText(QString::number(i+1));
            ui->tableWidget->setItem(i,0,item_ID);
            ui->tableWidget->setItem(i,1,item_name1);
            ui->tableWidget->setItem(i,2,item_name2);
        }
    }
    
    DialogUser::~DialogUser()
    {
        delete ui;
    }
    
    void DialogUser::deleteMultiPoint()
    {
        QSet<int> selectedRows; //we use a set to prevent doubles
        QList<QTableWidgetItem*> itemList = ui->tableWidget->selectedItems();
        QTableWidgetItem * item;
        foreach(item, itemList)
            selectedRows.insert(item->row());
    
        QList<int> rows = selectedRows.toList();
        qSort(rows.begin(), rows.end(),qGreater<int>());
        int currentRow = 0 ;
        foreach(int row, rows)
        {
           currentRow = row;
           ui->tableWidget->removeRow(row);
        }
    
        if(ui->tableWidget->rowCount() > 0)
        {
            if(currentRow > 0)
            {
                if(ui->tableWidget->rowCount()== currentRow )
                {
                    ui->tableWidget->selectRow(currentRow-1);
                }
                else
                {
                    ui->tableWidget->selectRow(currentRow);
                }
    
            }
            else
            {
                ui->tableWidget->selectRow(0);
                currentRow = 0;
            }
    
            //Change Number for colum NO
            for(int i = currentRow; i < ui->tableWidget->rowCount(); i++)
            {
                QTableWidgetItem* item2 = new QTableWidgetItem;
                item2->setText(QString::number(i+1));
                item2->setTextAlignment(Qt::AlignCenter);
                ui->tableWidget->setItem(i, 0, item2);
            }
        }
        emit emitFinishDeleteMulti();
    }
    
    void DialogUser::on_pushButton_clicked()
    {
        MyThread *a = new MyThread(this);
        a->start();
        connect(this,SIGNAL(emitFinishDeleteMulti()),a,SLOT(finishProgressBar()));
        deleteMultiPoint();
    }
    

    One Gui dialog Progressbar have QprogressBar and label

    ProgressBar::ProgressBar(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::ProgressBar)
    {
        ui->setupUi(this);
        setModal(true);
    }
    
    ProgressBar::~ProgressBar()
    {
        delete ui;
    }
    
    void ProgressBar::SetContent(QString text)
    {
        ui->label->setText(text);
    }
    

    and class MyThread have ProgressBar dialog

    MyThread::MyThread(QWidget *parent)
    {
        moveToThread(this);
        m_progressBar = NULL;
        m_progressBar = new ProgressBar(parent);
        m_progressBar->SetContent("Please.............");
        m_progressBar->setModal(true);
        m_progressBar->show();
    }
    
    MyThread::~MyThread()
    {
    
    }
    
    void MyThread::finishProgressBar()
    {
        m_progressBar->close();
    }
    
    void MyThread::run()
    {
    
    }
    

    Step:

    1. select row table
    2. Ctrl +A
    3. Click button Delete multi.
      Because big data. I need show dialog progress bar and run function remove multi row table. But progress bar is show and show "Not responding" => crash app.
      Please help me!
    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      In Qt you cannot interact with UI directly from other threads than the GUI thread!
      So, do not try to execute the dialog from other thread.
      Another problem in your code: your for-loop is blocking the event loop, that's why the progress bar is not updated.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Isn't this more or less same question which you already asked here:
        http://forum.qt.io/topic/61826/how-to-show-progress-bar-before-show-dialog

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • H Offline
          H Offline
          HAHAHAHAHA
          wrote on last edited by
          #4

          I see but if i call show dialog in MyDialog, when Ui QtableWidget remove row => progress bar hang up.

          1 Reply Last reply
          0
          • jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            To not to block the event loop you can try to call

            QApplication::processEvents();
            

            inside loops which take long time to execute. Which way your progress bar should work. And don't use another thread for your progress bar!

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • H Offline
              H Offline
              HAHAHAHAHA
              wrote on last edited by
              #6

              Thank u. I try add QApplication::processEvents();
              foreach(int row, rows)
              {
              currentRow = row;
              QApplication::processEvents();
              ui->tableWidget->removeRow(row);
              }

              progress bar is smooth but time remove very slow.

              1 Reply Last reply
              0
              • jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You could try to call processEvents less often - for example only on each third iteration.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                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