QT Creater入门编程:学习QT编程后,你会发现界面就像小孩子拼积木那样简单.

来源:互联网 发布:java读取xml文件 编辑:程序博客网 时间:2024/04/28 00:48
要注意到:注意 qt实际上是个编辑器,而不是编译器.所以依赖vs或者gcc编译器


代码实现界面操作:

创建Qt Weidgets Application项目的时候,有个""创建界面"的单选按钮,去掉在实现代码进行界面编程.

感觉没什么好讲的,直接上代码吧:

.h头文件

#include <QDialog>
//添加各种控件所需要的界面库
#include <qpushbutton.h>
#include <qlayout.h>
#include <QLabel>
#include <qlineedit.h>
class Dialog : public QDialog
{
    Q_OBJECT
public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private:
    QPushButton*    btn;
    QLineEdit*      edit1;
    QLineEdit*      edit2;
    QLabel*         labell;
};
#endif // DIALOG_H
.cpp文件
#include <QGridLayout>
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    btn = new QPushButton(this);
    edit1 = new QLineEdit(this);
    edit2 = new QLineEdit(this);
    labell = new QLabel(this);
    labell->setText("默认值");
    QGridLayout* layout = new QGridLayout(this);
    layout->addWidget(edit1,0,0);
    layout->addWidget(edit2,0,1);
    layout->addWidget(btn,1,0);
    layout->addWidget(labell,1,1);
}
Dialog::~Dialog()
{
}
若要想实现按钮的消息响应函数
接下来添加代码:
class Dialog : public QDialog
{
    Q_OBJECT
public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private:
    QPushButton*    btn;
    QLineEdit*      edit1;
    QLineEdit*      edit2;
    QLabel*         labell;
private slots://后面关键字是宏,响应槽所需要的
    void add();
};

再在
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
 connect(btn,SIGNAL(clicked()),this,SLOT(add()));
//中添,以将btn1的点击函数和add函数关联起来
}
//实现add函数即可
void Dialog::add()
{
   QString one = edit1->text();
   QString two = edit2->text();
   int nSun = one.toInt() + two.toInt();
   labell->setText(QString::number(nSun));
}

0 0