来自lineEdit的Qt值和来自QCheckBox的isChecked的Qt值未按预期工作

时间:2018-06-22 04:11:34

标签: c++ qt

当我按下“提交”按钮时,我试图弹出QMessageBox,这很好用,但是里面的消息取决于是否选中了某些复选框以及该行的值,进行修改,例如:

QApplication a(argc, argv);
QMainWindow *w = new QMainWindow();
.
.
.
QPushButton but_submit("Submit");
QMessageBox msg_submit;

// The following will be so that we can get the val of the GPA and then add it
// To the full message that will contain the info of person
QString submit_string = "Hello, here's the summary: \n";
submit_string += "Here\'s your value: " +  line_misc[0]->text() +  ".\n";
if (chk_art->isChecked())
    submit_string += "Art major!";
msg_submit.setText(submit_string);
.
. 
.
QObject::connect(&but_submit, SIGNAL(clicked()), &msg_submit, SLOT(exec()));

w->show();
return a.exec();

我在此定义或初始化的代码中没有的所有内容,并且在运行代码时没有任何警告或错误,一切都很好,一切都可以显示,但就像没有任何连接。

出现消息框(msg_submit),但消息为

  

你好,这是摘要:
  这是您的价值:。

当我真的希望看到一个数字时,或者如果我已选中复选框(chk_art),也可以看到艺术专业评论,但不幸的是,我没有。

我遍历了文档并尝试了各种变体,例如使用spinBox,并使用属性函数value()来获取值并将其包装在QString::number()中,但是不管我将值更改为什么,就像更改值时都不会调用setValue属性,或者插槽valueChanged()也不运行。

我做错什么了吗?

1 个答案:

答案 0 :(得分:0)

点击事件发出后,您应该更新submit_string。 可以尝试使用lambda函数

QObject::connect(&but_submit, &QPushButton::clicked,
        [&line_misc, &chk_art](){
        QMessageBox msg_submit;
        QString submit_string = "Hello, here's the summary: \n";
        submit_string += "Here\'s your value: " + line_misc[0]->text() + ".\n";
        if (chk_art->isChecked())
            submit_string += "Art major!";
        msg_submit.setText(submit_string);
        msg_submit.exec();
    });