一般在 m a i n . c p p main.cpp main.cpp中自动生成这样的代码
通过 w . s h o w ( ) w.show() w.show()来展示窗口
但是如果想在窗口里面加一些函数,一般写在 m a i n W i d g e t mainWidget mainWidget的构造函数内
#include "mainwidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mainWidget w;
w.show();
return a.exec();
}
举个例子,这里给窗口添加一个按钮吧
打开 m a i n W i d g e t . h mainWidget.h mainWidget.h在 p u b l i c public public下面添加私有变量
private:
QPushButton b1;//添加两个按钮
QPushButton *b2;
然后打开 m a i n W i d g e t . c p p mainWidget.cpp mainWidget.cpp去完成构造函数
#include "mainwidget.h"
#include <QPushButton>
mainWidget::mainWidget(QWidget *parent)
: QWidget(parent)
{
b1.setParent(this);//设置父亲
b1.setText("我是变量");//设置名字
b1.move(100,100);//移动座标
b2 = new QPushButton(this);//给指针分配一个指向的地址
b2->setText("我是指针");
connect(&b1,&QPushButton::pressed,this,&mainWidget::close);
/*
* &b1: 信号发送者
* &QPushButton::pressed 处理的信号, &发送者类名::信号名
* this 信号接收者
* &mainWidget::close 槽函数,信号处理函数 &接收的类名::槽函数名字
*/
}
mainWidget::~mainWidget()
{
}
上面的 c o n n e c t connect connect函数就是连接信号的函数
c o n n e c t ( 信 号 发 送 地 址 , 处 理 的 信 号 , 信 号 接 收 者 , 槽 函 数 ) connect(信号发送地址,处理的信号,信号接收者,槽函数) connect(信号发送地址,处理的信号,信号接收者,槽函数)
就是把事件,触发条件,作用对象绑定在一起了…