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 progress bar before show Dialog.
Forum Updated to NodeBB v4.3 + New Features

How to show progress bar before show Dialog.

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 3.4k 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

    I have class SimpleProgressBar about (QLabel and QProgressBar)

    SimpleProgressBar .h

    class SimpleProgressBar : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit SimpleProgressBar(QWidget *parent = 0);
        ~SimpleProgressBar();
        void SetContent(QString text);
    
    private:
        Ui::SimpleProgressBar *ui;
    };
    

    SimpleProgressBar .cpp

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

    One class TableDialog
    TableDialog.h

    class TableDialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit TableDialog(QWidget *parent = 0);
        ~TableDialog();
        void loadDataTable(QStringList listData);
    
    private:
        Ui::TableDialog *ui;
    };
    

    TableDialog.cpp

    TableDialog::TableDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::TableDialog)
    {
        ui->setupUi(this);
    }
    
    TableDialog::~TableDialog()
    {
        delete ui;
    }
    
    void TableDialog::loadDataTable(QStringList listData)
    {
        QString checkBoxStyle = "margin-left:25%; margin-right:20%;";
        for (int i = 0; i < listData.size(); i++)
        {
            QCheckBox *m_checkbox = new QCheckBox;
            m_checkbox->setStyleSheet( checkBoxStyle );
            m_checkbox->setChecked(false);
    
            QTableWidgetItem *itemName = new QTableWidgetItem;
            QTableWidgetItem *index = new QTableWidgetItem;
            itemName->setText(listData.at(i));
            index->setText(QString::number(i+1));
            index->setTextAlignment(Qt::AlignCenter);
    
            m_checkbox->setFocusPolicy( Qt::NoFocus );
            ui->tableWidget->setItem(i,0,index);
            ui->tableWidget->setCellWidget(i,1,m_checkbox);
            ui->tableWidget->setItem(i, 2 ,itemName);
         }
    }
    

    And dialog main MyDialog (include TableDialog and SimpleProgressBar)
    MyDialog.h

    class MyDialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit MyDialog(QWidget *parent = 0);
        ~MyDialog();
    
    private slots:
        void on_pushButton_clicked();
        void finishAndShowDialog();
    
    private:
        Ui::MyDialog *ui;
        QFutureWatcher<void> FutureWatcher;
        SimpleProgressBar* m_progressBar;
        TableDialog *dialogTable;
    };
    

    MyDialog.cpp

    MyDialog::MyDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::MyDialog)
    {
        ui->setupUi(this);
        m_progressBar = NULL;
        dialogTable = NULL;
    }
    
    MyDialog::~MyDialog()
    {
        delete ui;
    }
    
    void MyDialog::on_pushButton_clicked()
    {
        if(m_progressBar == NULL)
        {
            this->m_progressBar = new SimpleProgressBar(this);
        }
        else
        {
            delete m_progressBar;
            m_progressBar = NULL;
            this->m_progressBar = new SimpleProgressBar(this);
        }
        if(dialogTable != NULL)
        {
            delete dialogTable;
            dialogTable = NULL;
        }
        dialogTable = new TableDialog;
        QStringList listData;
        for (int i = 0; i < 5000; i++)
        {
            listData.push_back("Thanh "+i);
        }
        FutureWatcher.setFuture(QtConcurrent::run(dialogTable,&TableDialog::loadDataTable,listData));
        connect(&FutureWatcher, SIGNAL (finished()), this, SLOT (finishAndShowDialog()));
        m_progressBar->SetContent("Please waiting....");
        this->m_progressBar->show();
    }
    
    void MyDialog::finishAndShowDialog()
    {
        m_progressBar->close();
        dialogTable->show();
    }
    

    I run the application but it fails when click button.Please show me how to use that right progress bar! Thank u.

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

      You cannot access UI related stuff in a different thread. UI can only be changed in the same thread as QApplication::exec().
      You start TableDialog::loadDataTable in a different thread. This is the first thing to change.
      You can execute heavy calculations in a different thread, but you should not manipulate the UI from that thread. Instead you can use signals and slots to pass data between the two threads (use queued connection).

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

      1 Reply Last reply
      2
      • H Offline
        H Offline
        HAHAHAHAHA
        wrote on last edited by
        #3

        Thank jsulm. Please can you help me make an example of it .

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

          Why do you call TableDialog::loadDataTable() in a different thread?
          All it does is to populate your table, so there is no need to call it in a different thread.

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

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

            I'm sorry, I understand. Because I dont know to create an other thread run TableDialog::loadDataTable() .

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by dheerendra
              #6

              what is the issue you are facing ? Is it not showing the table ? Or progress bar is not shown ?

              1. One potential issue is about the QCheckBox created in different thread.
              2. You are running load table in different thread and queing the vector list. This may give problem.

              Please do let us know what is exact issue. I can share you the sample which make it work.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              2
              • H Offline
                H Offline
                HAHAHAHAHA
                wrote on last edited by
                #7

                Thank u. Progress bar is shown, but dialog table not show and show error QWidget: "Widgets must be created in the GUI thread."

                jsulmJ 1 Reply Last reply
                0
                • H HAHAHAHAHA

                  Thank u. Progress bar is shown, but dialog table not show and show error QWidget: "Widgets must be created in the GUI thread."

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @HAHAHAHAHA As I sad: you should not manipulate UI in a different thread! This is not going to work.

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

                  1 Reply Last reply
                  1
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on last edited by dheerendra
                    #9

                    Yes. This is the potential problem i was hihlighting. You are calling load function which is executed in different thread. In load function you are creating the QCheckBox which is UI object. So it must be falling. This is nothing do with your progress bar. You should create those UI object in main thread itself. Hope this clarifies.

                    You can make simple change like the following to make it work without threads or QtConcurrent

                    void MyDialog::on_pushButton_clicked() {
                    // FutureWatcher.setFuture(QtConcurrent::run(dialogTable,&TableDialog::loadDataTable,listData));
                    // connect(&FutureWatcher, SIGNAL (finished()), this, SLOT (finishAndShowDialog()));
                    m_progressBar->SetContent("Please waiting....");
                    this->m_progressBar->show();
                    connect(dialogTable, SIGNAL (finished()), this, SLOT (finishAndShowDialog()));
                    dialogTable->loadDataTable(listData);
                    }

                    class TableDialog : public QDialog
                    {
                    Q_OBJECT

                    public:
                    explicit TableDialog(QWidget *parent = 0);
                    ~TableDialog();
                    void loadDataTable(QStringList listData);
                    void test();

                    signals :
                    void finished();
                    ...
                    }

                    void TableDialog::loadDataTable(QStringList listData)
                    {
                    QString checkBoxStyle = "margin-left:25%; margin-right:20%;";
                    for (int i = 0; i < listData.size(); i++)
                    {
                    qDebug() << " Loading the Data ="<<i <<endl;
                    QCheckBox *m_checkbox = new QCheckBox;
                    m_checkbox->setStyleSheet( checkBoxStyle );
                    m_checkbox->setChecked(false);
                    ......
                    }
                    emit finished();
                    }

                    Dheerendra
                    @Community Service
                    Certified Qt Specialist
                    http://www.pthinks.com

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

                      Thanks everyone. :))

                      1 Reply Last reply
                      0
                      • dheerendraD Offline
                        dheerendraD Offline
                        dheerendra
                        Qt Champions 2022
                        wrote on last edited by
                        #11

                        Cool. You can put this qn to SOLVED state. Also upvote if any of our answer helped you fix the issue.

                        Dheerendra
                        @Community Service
                        Certified Qt Specialist
                        http://www.pthinks.com

                        1 Reply Last reply
                        2

                        • Login

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