qt 取消按钮点击效果_Qt 对话框里添加确定取消按钮

有时候需要弹出一个小对话框,不想用designer设计,直接在代码里动态的生成。

如下图所示:弹出一个带下拉列表和确定取消按钮的对话框

0818b9ca8b590ca3270a3433284dd417.png

QDialog dialog;

dialog.setWindowTitle(tr("选择要保存的格式"));

QComboBox *box = new QComboBox(&dialog);

box->addItem("jpg");

box->addItem("bmp");

box->addItem("png");

box->addItem("tif");

box->addItem("img");

QDialogButtonBox *button = new QDialogButtonBox(&dialog);

button->addButton( "OK", QDialogButtonBox::YesRole);

button->addButton( "NO", QDialogButtonBox::NoRole);

connect(button, SIGNAL(accepted()), &dialog, SLOT(accept()));

connect(button, SIGNAL(rejected()), &dialog, SLOT(reject()));

QVBoxLayout *layout = new QVBoxLayout;

layout->addWidget(box);

layout->addWidget( button);

dialog.setLayout(layout);

QString suffix ;

if ( dialog.exec() == QDialog::Accepted)

{

suffix = box->currentText();

ImageConvert img;

img.ConvertImages( files, dstpath.toStdString(), suffix.toStdString());

}

a2bdb7f3a0b958623a71b412832be18f.png

点击OK和NO按钮会有响应。


版权声明:本文为weixin_35540825原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。