pushButton是Qt中最常用的按钮,主要使用的函数为以下几个
UI拖拽一个pushbutton到界面上
public:
QPushButton *pushButton;
//初始化UI
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QStringLiteral("Widget"));
Widget->resize(595, 402);
/***** 初始化UI里面的控件 *****/
pushButton = new QPushButton(Widget);
pushButton->setObjectName(QStringLiteral("pushButton"));
pushButton->setGeometry(QRect(90, 60, 75, 23));
retranslateUi(Widget);
QMetaObject::connectSlotsByName(Widget);
} // setupUi
//对UI进行修改
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QApplication::translate("Widget", "Widget", Q_NULLPTR));
pushButton->setText(QApplication::translate("Widget", "abc ", Q_NULLPTR));
} // retranslateUi
QPushButton* button = new QPushButton(this);
button->setGeometry(QRect(0,0,75,23)); //QRect(x坐标,y坐标,宽,高
button->setText("开");
ui右键转到槽->clicked()
Qt Creater自动生成槽函数并且自动关联信号与槽
对槽函数进行修改
void Widget::on_pushButton_clicked()
{
static int flag = 0;
if(flag){
flag = 0;
ui->pushButton->setText("open");
}
else{
flag = 1;
ui->pushButton->setText("close");
}
}
代码关联信号与槽
connect(this>button,SIGNAL(clicked(bool)),this,SLOT(My_pushButton_clicked()));
void Widget::My_pushButton_clicked()
{
static int flag1 = 0;
if(flag1){
flag1 = 0;
this->button->setText("开");
}
else{
flag1 = 1;
this->button->setText("关");
}
}
MenuButton = new QPushButton(this);
MenuButton->setGeometry(QRect(0,90,75,23)); //QRect(x坐标,y坐标,宽,高
QMenu *pMenu = new QMenu(this);
QAction* Action1 = new QAction;
Action1->setText("first");
pMenu->addAction(Action1);
pMenu->addAction("Second");
pMenu->addSeparator();
pMenu->addAction("third");
pMenu->addAction("Fourth");
onnect(Action1, SIGNAL(triggered()), this, SLOT(slot_ClickMenu1()));
// 设置菜单
MenuButton->setMenu(pMenu);
//槽函数
void MainWindow::slot_ClickMenu1()
{
QMessageBox msgBox;
msgBox.setText("点击了第一个QMenu菜单");
msgBox.exec();
}
QToolButton类提供了用于命令或选项可以快速访问的按钮,通常可以用在QToolBar
里面。一般不会放在Widget中直接使用
//一般toolbutton用来关联MainToolBar里面的QAction
ui->toolButton->setDefaultAction(ui->actionopen);
QRadioButton 单选项按钮, 状态分为选中和未选中,单选框一般用于在多选1。
单选题
QButtonGroup* sex;
/* RadioButton CheckButton */
sex = new QButtonGroup;
sex->setExclusive(true); //重点 设置互斥
sex->addButton(ui->radioButton,0);
sex->addButton(ui->radioButton_2,1);
//关联信号
connect(sex, SIGNAL(buttonClicked(int)), this,SLOT(slot_RadioButton_checket(int)));
//槽函数
void MainWindow::slot_RadioButton_checket(int ButtonId)
{
sex_index=ButtonId;
switch(ButtonId)
{
case 0:
qDebug()<<"点击了男";
break;
case 1:
qDebug()<<"点击了女";
break;
default:
qDebug()<<"default";
}
}
QCheckBox复选项按钮, 状态分为选中和未选中,复选框一般用于在多个中选
择多个。—— 多选题
QButtonGroup* love;
love = new QButtonGroup;
love->setExclusive(false); //重点 设置不互斥
love->addButton(ui->checkBox,0);
love->addButton(ui->checkBox_2,1);
love->addButton(ui->checkBox_3,2);
//关联信号
connect(love, SIGNAL(buttonClicked(int)), this, SLOT(slot_CheckButton_checket(int)));
//槽函数
void MainWindow::slot_CheckButton_checket(int ButtonId)
{
//love_index = ButtonId;
switch(ButtonId)
{
case 0:
qDebug()<<"点击了打王者";
break;
case 1:
qDebug()<<"点击了看综艺";
break;
case 2:
qDebug()<<"点击了聊妹纸";
break;
default:
qDebug()<<"default";
break;
}
}
void MainWindow::on_pushButton_2_clicked()
{
QCheckBox* temp;
QString love_info;
/* 匹配每一个CheckButton判断那几个checkbutton是选中状态 */
for(int i=0;i<3;i++)
{
temp = (QCheckBox*)love->button(i);
if(temp->isChecked())
{
love_info+=QString::number(i);
}
}
/* 弹出提示框 */
QMessageBox msgBox;
msgBox.setText("性别选择为:"+QString::number(sex_index)+"\t"
+"爱好选择为:"+love_info);
msgBox.exec();
}