Qt 对话框里添加确定取消按钮

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tszhangjunqiao/article/details/38516941

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

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

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());
	}


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


猜你喜欢

转载自blog.csdn.net/tszhangjunqiao/article/details/38516941