【问题标题】:Qt QMessageBox - closes application instead of just message boxQt QMessageBox - 关闭应用程序而不仅仅是消息框
【发布时间】:2017-11-07 11:18:08
【问题描述】:

创建了一个 QMessageBox,当我在我的应用程序上按下“开始”按钮时会出现。

按下任一按钮(确定或取消)时 - 我希望消息框关闭 - 但应用程序保持打开状态。

不幸的是,当我点击“确定”或“取消”时,整个应用程序都会关闭。有什么建议吗?

J

这是我的代码:

  Main.cpp:

    #include "openingdialog.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        w.show();

        return a.exec();
    }

Opening Dialog:

patientsetup::patientsetup(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::patientsetup)
{
    ui->setupUi(this);
     connect(ui->okButton, SIGNAL(clicked()), this, SLOT(confirmButtonClicked()));
     connect(ui->ACSMode, SIGNAL(clicked()), this, SLOT(ACSButtonClicked()));
     connect(ui->PromptMode, SIGNAL(clicked()), this, SLOT(PMPSAIButtonClicked()));
     connect(ui->FullMode, SIGNAL(clicked()), this, SLOT(PMPSFullbuttonClicked()));
}

void patientsetup::PMPSAIButtonClicked()
{
direct = new pmps_f(this);
        direct->setData(patient, ui->Weight->value(), ui->Height->value(), lbmvalue, bsavalue, gender);
        direct->show();
}

pmps_f MainWindow    

           pmps_f::pmps_f(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::pmps_f)
        {
            ui->setupUi(this);
            connect(ui->startbutton,SIGNAL(clicked(bool)), this, SLOT(start()));
        }

             void pmps_f::start()
            {
                QMessageBox msgBox;
                msgBox.setText("Pressing continue will start the process with the process");
                msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
                msgBox.setDefaultButton(QMessageBox::Cancel);
                int ret = msgBox.exec();
                switch(ret) {
                    case QMessageBox::Ok:
                    msgBox.close();
                    break;
                case QMessageBox::Cancel:
                    msgBox.close();
                    break;
                }
            }

完整代码:

pmps_f::pmps_f(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::pmps_f)
{
    ui->setupUi(this);
    connect(ui->startbutton,SIGNAL(clicked(bool)), this, SLOT(start()));
}

pmps_f::~pmps_f()
{
    delete ui;
}

//public function that pulls the Patient data from the patientsetup.ui interface
void pmps_f::setData(const QString &fileName, const double &weightValue, const double &heightValue, const double &lbmValue, const double &bsaValue, const QString &genderName)
{
    ui->file->setText(fileName);
    ui->weight->setText(QString::number(weightValue));
    ui->height->setText(QString::number(heightValue));
    ui->lbm->setText(QString::number(lbmValue));
    ui->bsa->setText(QString::number(bsaValue));
    ui->gender->setText(genderName);
}

//Sedation start confirmation check
void pmps_f::start()
{
    QMessageBox msgBox;
    msgBox.setText("Pressing continue will start the sedation process");
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Cancel);
    int ret = msgBox.exec();
    switch(ret) {
       case QMessageBox::Ok:
       break;
      case QMessageBox::Cancel:
      break;
    }
}

【问题讨论】:

  • 什么是pmps_f,你怎么称呼它的start()函数?
  • 您的应用程序是否还打开了其他窗口?
  • 它是一个 QWidget,start() 是通过按下一个名为 Start 的按钮来调用的...
  • @JamesSprinks,尝试在您的QApplication 上致电setQuitOnLastWindowClosed(false)
  • 这确实需要SSCCE。做一个用最少的代码重现问题的例子(删除所有不相关的东西,只保留重现问题所需的东西),并将它放在一个我们可以复制和编译的文件中。

标签: qt qmessagebox


【解决方案1】:

我设法解决了这个问题:

setQuitOnLastWindowClosed(false);

但是 - 现在按下窗口“x”按钮时程序无法正常退出...

谁能帮忙?

【讨论】:

  • 任何python方法?因为这是我找到的最接近的东西
  • 没关系,我找到了,self.app.setQuitOnLastWindowClosed(False)
猜你喜欢
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
  • 2018-11-17
相关资源
最近更新 更多