因为需要在QMessageBox 上添加复选框,百度了半天居然没发现相关的东东,最后还是从老外那边找到了想要的。跟大家共享一下。我英语不好,大家见谅哈。
原文意思大概如下:
Qt提供了功能强大的QMessageBox类可以以操作系统的本地风格来显示对话框。当涉及按钮布局,消息图标或消息文本的风格时,这个类很灵活。但很不幸QMessageBox不支持自定义窗口,例如添加复选框或一组单选按钮。
然而,结果表明如果你了解QMessageBox布局的内部结构时,你可以很容易的添加自定义的窗口。
右过的图显示的是QMessageBox的顶层布局(QGridLayout),它包括了消息图标、消息文本和按钮集合。其中消息图标占据了[0,0]和[1,0]单元,消息文本位于[0,1]单元,而按钮集合占据了[2,0]和[2,1]单元。我们感兴趣的部分是[1,1]单元(informative text),它通过QMessageBox::setInformativeText()设置,仅用于对消息文本作详细的说明。
总之,如果你不使用informative text,你就有地方放置你的自定义窗口!添加多个按钮-修正边缘/间隔.在此我们推荐竖向布局(QVBoxLayout)来排列你的自定义窗口:
bool showMessageBox(QWidget * pParent)
{
// 创建你需要的消息框
QMessageBox mb(QMessageBox::Information,
tr("My message-box"),
tr("The quick brown fox.."),
QMessageBox::Ok,
pParent);
// 创建一个竖向布局并插入到你的消息框中
QVBoxLayout * pLayout = new QVBoxLayout(this);
//mb.layout()->addWidget(pLayout, 1, 1);//作者注明此句有误,应改成下一句,并说明在实际应用中dynamic_cast转换后应该检查非空
dynamic_cast< QGridLayout *>(mb.layout())->addWidget(pLayout, 1, 1); //Qt4.4.0 时addWidget应该改成addLayout ,
// 添加自定义窗口
[...]
// 显示消息框并等待用户的输入
if(mb.exec() != QMessageBox::Ok) {
return false;
}
// 处理用户的选择
[...]
return true;
}
例如 创建一个自定义的复选框...
// 添加自定义窗口
QCheckBox * pCheckBox = new QCheckBox
(tr("Don't show this dialog again"),
pParent);
pLayout->addWidget(pCheckBox);
[...]
…创建一组单选按钮 (有自已的竖向布局和间距!)…
// 添加自定义窗口
QButtonGroup * pBtnGroup = new QButtonGroup(pParent);
QVBoxLayout * pSubLayout = new QVBoxLayout(pParent);
pSubLayout->setSpacing(2);
pLayout->addLayout(pSubLayout);
QRadioButton * pRadio1 = new QRadioButton(tr("One"),
pParent);
pSubLayout->addWidget(pRadio1);
pBtnGroup->addButton(pRadio1, 0);
QRadioButton * pRadio2 = new QRadioButton(tr("Two"),
pParent);
pSubLayout->addWidget(pRadio2);
pBtnGroup->addButton(pRadio2, 0);
QRadioButton * pRadio3 = new QRadioButton(tr("Three"),
pParent);
pSubLayout->addWidget(pRadio3);
pBtnGroup->addButton(pRadio3, 0);
[...]
…创建其他自定义窗口!
请注意:上述的技术只在Windows XP, Windows Vista, 和几种Linux系列的系统平台下,在Qt 4.2.x 和4.3.x (以及Qt 4.4.x的Technical Review)上测试通过了。不知道同样的方法在其他版本的Qt或操作系统下是否能成功。请在评论里写下你的经验吧!
原文:http://blog.beef.de/2008/01/12/qmessagebox-widgets/
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/diaopan1985/archive/2009/08/01/4399432.aspx
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
/*msgBox w;
w.show();*/
QMessageBox msgBox(QMessageBox::Warning, "", "Do You wish to save file?",
0, NULL, Qt::Sheet);
QCheckBox dontPrompt("Do not prompt again", &msgBox);
//msgBox.addButton(&dontPrompt, QMessageBox::ActionRole);
msgBox.layout()->addWidget(&dontPrompt);
QPushButton* pOK = msgBox.addButton("OK", QMessageBox::AcceptRole);
pOK->setMaximumSize(200, 31);
pOK->setMinimumSize(70, 31);
msgBox.exec();
bool b = dontPrompt.checkState();
qDebug() << "result=" << b;
return a.exec();
}
// 改进版本 注意Title, 与addButton对象的位置
#include
#include
{
QApplication a(argc, argv);
/*msgBox w;
w.show();*/
0, NULL, Qt::Sheet);
msgBox.setFixedWidth(200);
QCheckBox dontPrompt("Do not prompt again", &msgBox);
QCheckBox dontPrompt2("Do not prompt again8888888", &msgBox);
QPushButton* pAdd = new QPushButton("add");
QPushButton* pSave = msgBox.addButton(QMessageBox::Save);
QPushButton* pOK = msgBox.addButton(QMessageBox::Close);
QLabel* pLabel = new QLabel("ddddddddddddddddddddddddddddddddddddddddddddddddddddd dddddddddddddddddddddddddddddddddddd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
pLabel->setScaledContents(true);
pLabel->setIndent(20);
QSpacerItem *pSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(&dontPrompt, 2, 1,1,4);
dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(&dontPrompt2, 3, 1,1,4);
dynamic_cast< QGridLayout *>(msgBox.layout())->addItem(pSpacer, 4, 1);
dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(pAdd, 4, 2);
dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(pSave, 4, 3);
dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(pOK, 4, 4);
QList lWgt = msgBox.buttons();
foreach(QAbstractButton *btn, lWgt)
{
qDebug() << "btn = " << btn->text();
i++;
}
//pOK->setMaximumSize(200, 31);
//pOK->setMinimumSize(70, 31);
msgBox.exec();
bool b = dontPrompt.checkState();
bool b2 = dontPrompt.checkState();
qDebug() << "result=" << b;
qDebug() << "result=" << b2;
return a.exec();
QApplication a(argc, argv); /*msgBox w; w.show();*/ QMessageBox msgBox(QMessageBox::NoIcon, "每组打印条数", "", 0, NULL); msgBox.setFixedWidth(200); QCheckBox dontPrompt("Do not prompt again", &msgBox); QCheckBox dontPrompt2("Do not prompt again8888888", &msgBox); QPushButton *pAdd = new QPushButton("add"); QPushButton* pSave = msgBox.addButton(QMessageBox::Save); QPushButton* pOK = msgBox.addButton(QMessageBox::Close); msgBox.addButton(pAdd,QMessageBox::DestructiveRole); msgBox.setDefaultButton(pAdd); QLabel* pLabel = new QLabel("enter a int number "); pLabel->setScaledContents(true); pLabel->setIndent(20); QSpacerItem *pSpacer = new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Minimum); dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(pLabel, 0, 1, 1, 1); dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(&dontPrompt, 3, 1,1,4); dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(&dontPrompt2, 4, 1,1,4); dynamic_cast< QGridLayout *>(msgBox.layout())->addItem(pSpacer, 5, 1); dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(pAdd, 5, 2); dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(pSave, 5, 3); dynamic_cast< QGridLayout *>(msgBox.layout())->addWidget(pOK, 5, 4); int i=1; QList<QAbstractButton *> lWgt = msgBox.buttons(); qDebug()<<lWgt.length(); foreach(QAbstractButton *btn, lWgt) { qDebug() << "btn = " << btn->text(); i++; } //pOK->setMaximumSize(200, 31); //pOK->setMinimumSize(70, 31); msgBox.exec(); bool b = dontPrompt.checkState(); bool b2 = dontPrompt2.checkState(); qDebug() << "result=" << b; qDebug() << "result=" << b2; return a.exec();