Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

QMessageBox中,怎么区分触发了哪个按钮

`
//在文档中是这样示例的
For example,

QMessageBox msgBox;

QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole);

QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort);

msgBox.exec();

if (msgBox.clickedButton() == connectButton) {

// connect

} else if (msgBox.clickedButton() == abortButton) {

// abort

}`

但是我的代码会提示 if (msgBox.clickedButton() == connectButton) 这个部分类型不匹配,前边是抽象基类,
请问应当怎样完成安全的转换?或者有没有其他方法区分触发了哪个按钮?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

具体原因不详,但是找到了出问题的表面原因---没有添加<QPushButton>,未添加之前也能用,除了上面所提这一点没别的毛病,为什么没有头文件也能用我也不知道

之前的头文件是:

#include <QFile>
#include <QFileInfo>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include <QCloseEvent>
#include <QApplication>

添加头文件<QPushButton>之后上述毛病就好了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.5k users

...