1. 復(fù)選框:QCheckBox
實(shí)例化
//實(shí)例化
// QCheckBox* checkBox = new QCheckBox("是否同意該條款",this);
QCheckBox* checkBox = new QCheckBox(this);
1.1?代碼實(shí)現(xiàn)
1.1.1?復(fù)選框的基本函數(shù)
復(fù)選框選中狀態(tài)的參數(shù)
Qt::Unchecked //未選中狀態(tài)
Qt::PartiallyChecked //半選中狀態(tài)
Qt::Checked //全選中狀態(tài)
QCheckBox
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
//復(fù)選框的使用
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
//設(shè)置大小
this->checkBox->resize(150,40);
//設(shè)置位置
this->checkBox->move(100,0);
//設(shè)置復(fù)選框文本提示
this->checkBox->setText("是否同意該協(xié)議");
//設(shè)置使復(fù)選框狀態(tài)具有三種狀態(tài)
this->checkBox->setTristate(true);//true 代表三種狀態(tài),false兩種
//設(shè)置復(fù)選框的狀態(tài)
this->checkBox->setCheckState(Qt::PartiallyChecked);//Qt::PartiallyChecked半選中狀態(tài)
//獲取復(fù)選框狀態(tài)
Qt::CheckState state = this->checkBox->checkState();
if(state==Qt::PartiallyChecked){
qDebug()<<"半選中狀態(tài)呢"<<endl;
}
}
Dialog::~Dialog()
{
delete ui;
}
1.2?信號(hào)
復(fù)選框被選中狀態(tài)改變?觸發(fā)信號(hào)
QCheckBox
綁定
dialog.cpp
//綁定復(fù)選框被選中信號(hào)
connect(this->checkBox,SIGNAL(stateChanged(int)),this,SLOT(receive_State(int)));
槽
定義
dialog.h
public slots://復(fù)選框被選中觸發(fā)
void receive_State(int state);
實(shí)現(xiàn)
dialog.cpp
//復(fù)選框被選中觸發(fā)
void Dialog::receive_State(int state){
if(state==0){
qDebug()<<"未選中"<<endl;
}else if(state==1){
qDebug()<<"半選中"<<endl;
}else if(state==2){
qDebug()<<"全選中"<<endl;
}
}
1.3?UI實(shí)現(xiàn)
text:設(shè)置文本
icon:設(shè)置圖片
iconsize:圖片大小
shortcut:快捷鍵
....
2.分組框:QGroupBox
實(shí)例化
//實(shí)例化
// QGroupBox* groupBox = new QGroupBox("按鈕組",this);
QGroupBox* groupBox = new QGroupBox(this);
2.1?代碼實(shí)現(xiàn)
2.1.1?分組框的基本函數(shù)
QGroupBox
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
//分組框的使用
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
//獲取標(biāo)題對(duì)齊方式
Qt::Alignment align = this->groupBox->alignment();
//設(shè)置分組框的大小
this->groupBox->resize(100,80);
//移動(dòng)
this->groupBox->move(100,100);
//設(shè)置分組框的對(duì)齊方式
this->groupBox->setAlignment(Qt::AlignRight);//右對(duì)齊
//設(shè)置標(biāo)題
this->groupBox->setTitle("按鈕組");
//獲取標(biāo)題
QString title = this->groupBox->title();
//設(shè)置分組框的復(fù)選框
this->groupBox->setCheckable(true);//分組框標(biāo)題前面右復(fù)選框
//設(shè)置選中該分組框
this->groupBox->setChecked(true);
//設(shè)置分組框邊框是否隱藏
// this->groupBox->setFlat(true);//true 隱藏 false不隱藏
}
Dialog::~Dialog()
{
delete ui;
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-791890.html
2.2?信號(hào)
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-791890.html
void clicked(bool checked = false)當(dāng)鼠標(biāo)光標(biāo)在按鈕內(nèi)時(shí)按下然后釋放,或當(dāng)鍵入快捷鍵時(shí)發(fā)出此信號(hào)。
void toggled(bool on)如果組框是可檢查的,則在復(fù)選框被切換時(shí)發(fā)出此信號(hào)。如果復(fù)選框被選中,On為true;否則為false
2.3?UI實(shí)現(xiàn)
UI:
titile:設(shè)置標(biāo)題
alignment:設(shè)置對(duì)齊方式
flat:設(shè)置邊框是否隱藏
checkable:設(shè)置是否具有復(fù)選框
checked:設(shè)置復(fù)選框是否選中
到了這里,關(guān)于復(fù)選框QCheckBox和分組框QGroupBox的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!