点击按钮弹出对话框 qt

如题所述

我用的qt4+kdevelop开发,我来说一下步骤吧,供你参考:
(ps:qt3是可以建立 C++工程的,qt4只能做界面)
现在,我要建立一个对话框,对话框内只有一个按钮(pushButton),点击按钮会弹出一个MessageBox.步骤如下:
1、新建一个文件夹test3,打开designer制作好界面
2、在test3文件夹内添加3个文件,分别为test3.h,test3.cpp,main.cpp,在test3.h添加如下代码:
#ifndef TEST3_H
#define TEST3_H

#include <QtGui/QDialog>
#include <QStandardItemModel>
#include "ui_test3.h"

class test : public QDialog
{
Q_OBJECT

public:
test(QWidget *parent = 0);
~test();
private:
Ui_Dialog ui;
private slots:
void on_pushButton_clicked();
};

#endif
在test3.cpp中添加如下代码:
#include "test3.h"
#include <QLibrary>
#include <QMessageBox>

test::test(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
}

test::~test()
{

}

void test::on_pushButton_clicked()
{
QMessageBox::information(this,tr("hello"),tr("Mars"));
}
在main.cpp中添加如下代码:
#include <QtGui>
#include <QApplication>
#include "test3.h"

int main(int argc, char *argv[])
{
QApplication app(argc,argv);
test *dialog = new test;
dialog->show();
return app.exec();
}
3、打开控制台,输入如下命令:qmake -project会生成一个test3.pro的工程文件
4、打开kdevelop,选择project->import existing project,选中test3文件夹。
5、build->build project
6、运行吧

再说两句(ps:我承认我多嘴)

vc里面给一个按钮添加事件只需要通过双击按钮就可以了,但是这里不一样,通常情况下有两种方法。1、按照一定的命名规则为一个函数取名字,规则如下:on_name_singal,比如要为pushButton_2添加一个事件,只需要将相应函数命名为 on_pushButton_2_clicked();就可以了.2、使用connect连接控件和相应的函数,比如上面的例子我们可以在构造函数里添加 connect(pushButton,SIGNAL()clicked(),this,SLOT(hahaha()));
(ps:先把on_pushButton_clicked()函数改名为hahaha)
也行。那么,我想你也应该明白代码写在那里了吧,就是在on_pushButton_clicked()函数里面。
我讲完了
温馨提示:答案为网友推荐,仅供参考
相似回答