Qt 点击按钮实现弹窗(回话框) 利用信号槽机制

注意:button的click()是一个slot,加在signal不生效

只加关键代码,往自己代码里添
.h 添加代码

#include 

class QPushButton;
class QtProject : public QDialog
{
	Q_OBJECT
private:
	QPushButton *buttons;

public slots :
		void on_pushButton_clicked();

public:
	QtProject(QWidget *parent = 0);
	//~QtProject();

};

.cpp,main.cpp无改动

//设置buttons
buttons = new QPushButton(this);
buttons->setText("pushButton");
QObject::connect(buttons, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));

//弹出窗口模态对话框
void QtProject::on_pushButton_clicked()
{
	QDialog* dlg = new QDialog(this);
	dlg->setAttribute(Qt::WA_DeleteOnClose);//设置对话框关闭后,自动销毁
	dlg->setWindowModality(Qt::WindowModal);
	dlg->show();
}

你可能感兴趣的:(Qt 点击按钮实现弹窗(回话框) 利用信号槽机制)