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. call the dialog inside the loop

call the dialog inside the loop

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 495 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.
  • B Offline
    B Offline
    Blackzero
    wrote on last edited by
    #1

    How can I call a dialog inside a loop process, I tried this but when the dialog appears the application becomes "Not responding", how can I solve this?

    void MainWindow::MyProcess()
    {
        for (;;)
        {
            if (result)
            {
                // code process ....
            }
            else
            {
                // result failed
                CMessageBox dialog;
                dialog.setMessage(Critical, "Error", "Failed.", static_cast<Btn>(Ok));
                dialog.exec_();
                break;
            }
        }
    }
    
    C jsulmJ 2 Replies Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, you can try open() instead of exec() and connect to the finished() signal from the messagebox to get notified when the user pressed Ok.

      1 Reply Last reply
      0
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #3

        Note: you'll have to create the messagebox somewhere outside the else clause, otherwise it will be destroyed when the break occurs.

        1 Reply Last reply
        0
        • B Blackzero

          How can I call a dialog inside a loop process, I tried this but when the dialog appears the application becomes "Not responding", how can I solve this?

          void MainWindow::MyProcess()
          {
              for (;;)
              {
                  if (result)
                  {
                      // code process ....
                  }
                  else
                  {
                      // result failed
                      CMessageBox dialog;
                      dialog.setMessage(Critical, "Error", "Failed.", static_cast<Btn>(Ok));
                      dialog.exec_();
                      break;
                  }
              }
          }
          
          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @Blackzero What is CMessageBox? The setMessage() and exec_() calls cast some doubt over whether it is a QMessageBox or similar sub-class.

          B 1 Reply Last reply
          0
          • C ChrisW67

            @Blackzero What is CMessageBox? The setMessage() and exec_() calls cast some doubt over whether it is a QMessageBox or similar sub-class.

            B Offline
            B Offline
            Blackzero
            wrote on last edited by
            #5

            @ChrisW67

            #ifndef CMESSAGEBOX_H
            #define CMESSAGEBOX_H
            
            #include <QDialog>
            
            namespace Ui {
            class CMessageBox;
            }
            
            enum Mode {
               Information = 1,
               Warning = 2,
               Question = 3,
               Critical = 4
            };
            
            enum Btn {
               Yes = 0x01,
               No = 0x02,
               Ok = 0x04,
               Cancel = 0x08
            };
            
            class CMessageBox : public QDialog {
               Q_OBJECT
            
            public:
               explicit CMessageBox(QWidget *parent = nullptr);
               ~CMessageBox();
            
               void setMessage(Mode modemsg, const QString &title, const QString &message, Btn modeBtn);
               void setIcon(const QString &iconPath);
               void addButton(const QString &text, Btn (CMessageBox::*handler)());
            
               Btn PushOk();
               Btn PushCancel();
               Btn PushYes();
               Btn PushNo();
               Btn exec_();
            
            private:
               Ui::CMessageBox *ui;
               Btn result_value; 
            
            };
            
            #endif // CMESSAGEBOX_H
            
            
            #include "cmessagebox.h"
            #include "ui_cmessagebox.h"
            #include <QFrame>
            
            #include <QPushButton>
            
            
            CMessageBox::CMessageBox(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::CMessageBox)
            {
                ui->setupUi(this);
                setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
            
                //this->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
            
                ui->frame->setFrameShape(QFrame::StyledPanel);
                ui->frame->setFrameShadow(QFrame::Raised);
            
            }
            
            CMessageBox::~CMessageBox()
            {
                delete ui;
            }
            
            void CMessageBox::setMessage(Mode modemsg, const QString &title, const QString &message, Btn modeBtn)
            {
                ui->msg->setText(message);
                this->setWindowTitle(title);
            
                ui->horizontalLayout_2->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
                if (modeBtn & Yes) addButton("Yes", &CMessageBox::PushYes);
                if (modeBtn & Ok) addButton("OK", &CMessageBox::PushOk);
                if (modeBtn & No) addButton("No", &CMessageBox::PushNo);
                if (modeBtn & Cancel) addButton("Cancel", &CMessageBox::PushCancel);
                ui->horizontalLayout_2->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
                int labelWidth = ui->msg->sizeHint().width();
                int labelHeight = ui->msg->sizeHint().height();
            
                setFixedWidth(labelWidth + 70);
                if (labelHeight > 39) {
                    setFixedHeight(labelHeight + 64);
                } else {
                    setFixedHeight(107);
                }
            
                switch (modemsg) {
                case Information:
                    setIcon(":/img/messagebox-asterisk.png");
                    break;
                case Warning:
                    setIcon(":/img/messagebox-exclamation.png");
                    break;
                case Question:
                    setIcon(":/img/messagebox-question.png");
                    break;
                case Critical:
                    setIcon(":/img/cross.png");
                    break;
                default:
                    break;
                }
            }
            
            void CMessageBox::addButton(const QString &text, Btn (CMessageBox::*handler)())
            {
                QPushButton *pushButton = new QPushButton(ui->frame);
                pushButton->setMinimumWidth(75);
                pushButton->setMaximumWidth(23);
                pushButton->setText(text);
                ui->horizontalLayout_2->addWidget(pushButton);
                connect(pushButton, &QPushButton::clicked, this, handler);
            }
            
            void CMessageBox::setIcon(const QString &iconPath)
            {
                ui->icn->setStyleSheet("image: url(" + iconPath + ");");
                QApplication::beep();
            }
            Btn CMessageBox::PushOk() {
                result_value = Ok;
                accept();
                return result_value;
            }
            Btn CMessageBox::PushCancel() {
                result_value = Cancel;
                accept();
                return result_value;
            }
            Btn CMessageBox::PushYes() {
                result_value = Yes;
                accept();
                return result_value;
            }
            Btn CMessageBox::PushNo() {
                result_value = No;
                accept();
                return result_value;
            }
            Btn CMessageBox::exec_() {
                QDialog::exec();
                return result_value;
            }
            
            
            1 Reply Last reply
            0
            • B Blackzero

              How can I call a dialog inside a loop process, I tried this but when the dialog appears the application becomes "Not responding", how can I solve this?

              void MainWindow::MyProcess()
              {
                  for (;;)
                  {
                      if (result)
                      {
                          // code process ....
                      }
                      else
                      {
                          // result failed
                          CMessageBox dialog;
                          dialog.setMessage(Critical, "Error", "Failed.", static_cast<Btn>(Ok));
                          dialog.exec_();
                          break;
                      }
                  }
              }
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Blackzero said in call the dialog inside the loop:

              but when the dialog appears the application becomes "Not responding"

              But does the dialog work?
              You're using exec() which means you're using the dialog as a modal dialog - the application is blocked until the dialog is closed.

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

              1 Reply Last reply
              1

              • Login

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