本篇文章仅作Qt入门的控件指引,如果您已经具有一定Qt开发的基础,那么这篇文章可能不适合您。
用于显示文本或图像,不需要用户交互,适合用作静态信息的展示
QLabel* label = new QLabel(this); //在本窗口创建一个新对象QLabel
label->setText("Hello,world"); //设置文本内容
label->move(x,y); //距离父控件左/上边缘的偏移量,单位为像素
label->setFont(QFont("Times",15));
label->setStyle("color:red");
label->setGeometry(x,y,w,h); //w和h对应宽度和高度
label->setPixmap(QPixmap("")); //设置label显示图片
一个按钮控件,用于实现交互操作,是最常用的控件之一,可以触发点击事件来执行操作或功能
//QLabel中具有的方法,QPushButton大体都可使用
button->setIcon(""); //设置按钮图标
button->setIconSize(Qsize(w,h)); //设置图标大小
一个单行文本输入框控件,用户可以在其中输入或编辑单行文本
lineEdit->setEnabled(true); //设置是否可以输入
lineEdit->setEchoMode(QLineEdit::EchoMode::password);
//设置输入密码时,将明文显示替换为******符号串
一个单选按钮控件,通常用于在一组选项中供用户选择一个
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
QVBoxLayout layout(&window);
QRadioButton *radio1 = new QRadioButton("Option 1");
QRadioButton *radio2 = new QRadioButton("Option 2");
QRadioButton *radio3 = new QRadioButton("Option 3");
layout.addWidget(radio1);
layout.addWidget(radio2);
layout.addWidget(radio3);
QButtonGroup *buttonGroup = new QButtonGroup(&window);
//设置QButtonGroup,使得组内按钮最多只有一个处于选中状态
buttonGroup->addButton(radio1);
buttonGroup->addButton(radio2);
buttonGroup->addButton(radio3);
// 信号与槽:检测组内按钮切换
QObject::connect(buttonGroup, &QButtonGroup::idToggled, [&](int id, bool checked) {
if (checked)
qDebug() << "Button" << id << "selected";
});
window.show();
return app.exec();
}
常用方法
setText(const QString &text)
:设置复选框的文本。text() const
:获取复选框的文本。isChecked() const
:返回复选框是否被选中。setChecked(bool state)
:设置复选框的状态(选中或未选中)。setTristate(bool)
:启用三态模式(默认是两态)。checkState() const
:获取当前状态。setCheckState(Qt::CheckState state)
:设置复选框的状态。toggle()
:切换复选框的状态。信号
stateChanged(int state)
:当复选框状态发生改变时触发,state的值为 Qt::Unchecked, Qt::PartiallyChecked 或 Qt::Checked。#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// 创建主窗口
QWidget window;
window.setWindowTitle("QCheckBox 示例");
// 创建一个垂直布局
QVBoxLayout *layout = new QVBoxLayout(&window);
// 创建复选框
QCheckBox *checkBox1 = new QCheckBox("选项 1");
QCheckBox *checkBox2 = new QCheckBox("选项 2");
QCheckBox *checkBox3 = new QCheckBox("启用三态");
checkBox3->setTristate(true); // 启用三态模式
// 创建状态标签
QLabel *label = new QLabel("状态:未选中");
// 连接信号和槽函数
QObject::connect(checkBox1, &QCheckBox::stateChanged, [&](int state) {
QString stateText = (state == Qt::Checked) ? "选中" : "未选中";
label->setText("选项 1 状态:" + stateText);
});
// 将控件添加到布局
layout->addWidget(checkBox1);
layout->addWidget(checkBox2);
layout->addWidget(checkBox3);
layout->addWidget(label);
// 显示窗口
window.show();
return app.exec();
}
QComboBox 是下拉选择框,用于从多个选项中选择一个
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("QComboBox 示例");
QVBoxLayout *layout = new QVBoxLayout(&window);
QLabel *label = new QLabel("请选择一个选项:");
QComboBox *comboBox = new QComboBox();
// 添加选项
comboBox->addItem("选项 1");
comboBox->addItem("选项 2");
comboBox->addItem("选项 3");
// 连接信号和槽
QObject::connect(comboBox, &QComboBox::currentTextChanged, [&](const QString &text) {
label->setText("当前选择:" + text);
});
QString text = comboBox->currentText(); //使用currentText()方法获得当前选中的text
layout->addWidget(comboBox);
layout->addWidget(label);
window.show();
return app.exec();
}
QSlider 是滑块控件,用于调整连续的数值(例如音量或亮度)
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("QSlider 示例");
QVBoxLayout *layout = new QVBoxLayout(&window);
QLabel *label = new QLabel("当前值:50");
QSlider *slider = new QSlider(Qt::Horizontal); // 水平滑块
//后续更改方向为竖直方向
slider->setOrientation(Qt::Vertical);
slider->setRange(0, 100); // 设置范围
slider->setTickInterval(2); //每跳一格表示数值变化2个单位
slider->setValue(50); // 初始值
// 连接信号和槽
QObject::connect(slider, &QSlider::valueChanged, [&](int value) {
label->setText("当前值:" + QString::number(value));
});
layout->addWidget(slider);
layout->addWidget(label);
window.show();
return app.exec();
}
SetTickPosition(QSlider::TickPosition)
用于设置刻度线的位置。QSlider 支持以下刻度线位置选项:
QListWidget 是列表控件,用于显示和管理一组条目
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("QListWidget 示例");
QVBoxLayout *layout = new QVBoxLayout(&window);
QLabel *label = new QLabel("当前选择:");
QListWidget *listWidget = new QListWidget();
// 添加条目
listWidget->addItem("条目 1");
listWidget->addItem("条目 2");
listWidget->addItem("条目 3");
// 连接信号和槽
QObject::connect(listWidget, &QListWidget::itemClicked, [&](QListWidgetItem *item) {
label->setText("当前选择:" + item->text());
});
layout->addWidget(listWidget);
layout->addWidget(label);
window.show();
return app.exec();
}