添加按钮控件:
在新建的文件添加 头文件
#include<qpushbutton.h>
然后再 mainwindow.cpp 文件中更改添加 btr 按钮控件
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *bt1 = new QPushButton(bt1_hit,this);
}
然后编译即可
使用move函数可以更改控件的位置:
bt1->move(100,100);
更改按钮的大小:
setFixedSize(param1,param2);
添加 Label 控件和通过按键更改按钮上面的值:
添加头文件:
#include<qlabel.h>
然后同样在 mainwindow.cpp 中添加:
label_ = new QLabel(111,this);
然后在头文件中添加可以修改 label参数的函数(声明):
public slots:
void labelChange();
回到 mainwindow.cpp 文件中定义函数:
void MainWindow::labelChange()
{
int t = rand()%100;
label_->setText(QString::number(t));
qDebug() << 你点击了 ;
}
然后就是 qt 比较经典的信槽和和控件的连接
connect(bt1,SIGNAL(clicked()),this,SLOT(labelChange()));