QCheckbox
●继承自QAbstractButton 支持快捷键
●可以设置文本 text() setText()
状态
●bool isChecked() const //是否选中
●void setChecked(bool) toggled(bool) //改变状态
QCheckBox多选和单选
●setAutoExclusive //排它, 单选(同一个层级)
QCheckBox事件
●clicked(bool) 鼠标点击
●toggled(bool) 状态改变
ui.checkBox->setChecked(!ui.checkBox->isChecked());
例子1:
只要不在同一级之下:设置:setAutoExclusive //排它, 单选(同一个层级)
就会是一个单选。
例子2:
.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_QCheckBoxTest.h"
class QCheckBoxTest : public QWidget
{
Q_OBJECT
public:
QCheckBoxTest(QWidget *parent = Q_NULLPTR);
public slots:
void Click(bool check);
void Toggle(bool check);
void Test();
private:
Ui::QCheckBoxTestClass ui;
};
.cpp
#include "QCheckBoxTest.h"
#include <QDebug>
QCheckBoxTest::QCheckBoxTest(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
void QCheckBoxTest::Click(bool check)
{
qDebug() << "Click = " << check;
}
void QCheckBoxTest::Toggle(bool check)
{
qDebug() << "Toggle = " << check;
}
void QCheckBoxTest::Test()
{
ui.checkBox7->setChecked(!ui.checkBox7->isChecked());
}
被选中,和不被选中 触发事件。
加一个Test按钮,可以触发Toggle
QButtonGroup
setExclusive
手动创建
/*
public slots:
void GClick(QAbstractButton *but);
void GToggle(QAbstractButton *but, bool check);
*/
#include "QButtonGroupCheckbox.h"
#include <QDebug>
QButtonGroupCheckbox::QButtonGroupCheckbox(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
QButtonGroup *group = new QButtonGroup(this);
//加组后默认变为单选
group->addButton(ui.checkBox1);
group->addButton(ui.checkBox2);
group->addButton(ui.checkBox3);
//改为多选
//group->setExclusive(false);
//手动点击事件
QObject::connect(group, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(GClick(QAbstractButton *)));
//状态变化触发事件
QObject::connect(group, SIGNAL(buttonToggled(QAbstractButton *,bool)), this, SLOT(GToggle(QAbstractButton *, bool)));
}
void QButtonGroupCheckbox::GClick(QAbstractButton *but)
{
qDebug() <<"GClick:" <<but << but->isChecked();
}
void QButtonGroupCheckbox::GToggle(QAbstractButton *but, bool check)
{
qDebug() <<"GToggle:"<< but << check;
}
QRadioButton
●类似QCheckBox
●默认单选
●布局器
●QButtonGroup获取选中的Button
/**
public slots:
void Save();
**/
#include "QRadioButtonTest.h"
#include <QRadioButton>
#include<QVBoxLayout>
#include <QButtonGroup>
#include <QPushButton>
#include <QMessageBox>
QRadioButtonTest::QRadioButtonTest(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
QRadioButton *rb1 = new QRadioButton("QRadioButton001");
QRadioButton *rb2 = new QRadioButton("QRadioButton002");
QRadioButton *rb3 = new QRadioButton("QRadioButton003");
//按钮组
QButtonGroup *group = new QButtonGroup(this);
group->setObjectName("group");
group->addButton(rb1);
group->addButton(rb2);
group->addButton(rb3);
//垂直布局器
QVBoxLayout *vb = new QVBoxLayout(this);
//遍历按钮组
for (int i = 0; i < group->buttons().size(); i++)
{
vb->addWidget(group->buttons()[i]);
}
//保存按钮
QPushButton *save = new QPushButton("Save");
QObject::connect(save, SIGNAL(clicked()), this, SLOT(Save()));
vb->addWidget(save);
}
void QRadioButtonTest::Save()
{
//获取按钮组
QButtonGroup *group = this->findChild<QButtonGroup*>("group");
if (!group)return;
//获取被选中的按钮
QRadioButton *but = (QRadioButton *)group->checkedButton();
if (!but)
{
QMessageBox::warning(this, "", "Please checked!");
return;
}
QMessageBox::information(this, "", but->text());
}
QComboBox下拉列表
●属性:编辑,图标大小
能否编辑 图标
●插入数据(字符串、图标、自定义数据)
QComboBox新增数据
QComboBox插入数据(字符串、图标、自定义数据)
QComboBox删除数据
QVariant
●信号事件
public slots:
void Click();
void Act(int index);
void Act(QString str);
void Cur(int index);
void Cur(QString str);
void Hi(int index);
#include "QComboBoxTest.h"
#include <QDebug>
struct MyType
{
int x = 0;
int y = 0;
};
//使得QT支持此类型
Q_DECLARE_METATYPE(MyType);
void QComboBoxTest::Click()
{
qDebug() << ui.comboBox->itemText(0);
//访问用户自定义数据
QVariant var = ui.comboBox->itemData(0);
qDebug()<<"var.value<MyType>().x = "<<var.value<MyType>().x;
ui.comboBox->setCurrentIndex(3);
}
void QComboBoxTest::Hi(int index)
{
qDebug() << "Hi int = " << index;
}
//用户选择,和代码调用
void QComboBoxTest::Cur(int index)
{
qDebug() << "Cur int = " << index;
}
void QComboBoxTest::Cur(QString str)
{
qDebug() << "Cur QString = " << str;
}
//只有用户选择
void QComboBoxTest::Act(int index)
{
qDebug() << "Act int = " << index;
}
void QComboBoxTest::Act(QString str)
{
qDebug() << "Act QString = " << str;
}
QComboBoxTest::QComboBoxTest(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
//清空所有列表
ui.comboBox->clear();
//插入用户自定义数据
QVariant var;
MyType my;
my.x = 101;
var.setValue(my);
ui.comboBox->addItem("Item001",var);
ui.comboBox->addItem("Item002");
//图标
QIcon icon(":/QComboBoxTest/Resources/test.png");
ui.comboBox->addItem(icon, "Item003_icon");
//列表项
QStringList sl = {"Item004_list","Item005_list" };
//sl.push_back("Item004_list");
ui.comboBox->addItems(sl);
//第二行
ui.comboBox->insertItem(1, "Item002_insert");
}
QSlider
●常用属性
●信号事件
●qss样式设计
//样式表
QSlider::groove{
background-color: rgba(200, 83, 245,100);
height:28px;
border:1px solid rgba(200, 83, 245,100);
border-radius:10px;
}
QSlider::handle{
width:30px;
background-color: rgba(225, 10, 0,200);
border:1px solid rgba(225, 10, 0,200);;
border-radius:10px;
margin:-10px 0;
}
QSlider::sub-page{
background-color: rgba(125, 13, 135,80);
}
QSlider::add-page{
background-color: rgba(0, 13, 135,80);
}
●重载鼠标事件自定义XSlider
QSliderTest.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_QSliderTest.h"
class QSliderTest : public QWidget
{
Q_OBJECT
public:
QSliderTest(QWidget *parent = Q_NULLPTR);
public slots:
void Move(int);
void Change(int);
void Press();
void Rel();
void Click();
private:
Ui::QSliderTestClass ui;
};
QSliderTest.cpp
#include "QSliderTest.h"
#include <QDebug>
QSliderTest::QSliderTest(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
//拖动滑动条
void QSliderTest::Move(int v)
{
qDebug() << "Move " << v;
}
//点击、拖动、直接设置
void QSliderTest::Change(int v)
{
qDebug() << "Change " << v;
}
//点击滑块
void QSliderTest::Press()
{
qDebug() << "Press " << ui.horizontalSlider->value();
}
void QSliderTest::Rel()
{
qDebug() << "Rel " << ui.horizontalSlider->value();
}
void QSliderTest::Click()
{
int v = ui.horizontalSlider->value() + 10;
ui.horizontalSlider->setValue(v);
}
XSlider.h 自己重载的类
#pragma once
#include <QSlider>
class XSlider :public QSlider
{
public:
XSlider(QWidget *p = NULL);
~XSlider();
virtual void mousePressEvent(QMouseEvent *e);
};
XSlider.cpp
#include "XSlider.h"
#include <QMouseEvent>
#include <QDebug>
void XSlider::mousePressEvent(QMouseEvent *e)
{
qDebug() << e->pos().x();
QSlider::mousePressEvent(e);
//百分比 0.32
double p = (double)e->pos().x() / (double)width();
int val = p*(maximum() - minimum()) + minimum();
setValue(val);
}
XSlider::XSlider(QWidget *p):QSlider(p)
{
}
XSlider::~XSlider()
{
}