How to show dialog on thread other.



  • 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!

  • Moderators

    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.


  • Moderators

    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



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


  • Moderators

    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!



  • 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.


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.