Qt 常用控件介绍

ui中的控件要使用ui来指向:ui->xxx控件名称。

Buttons

Push Button:普通按钮
Tool Button:可以隐藏文字只显示图片
Radio Button:单选框
Check Box:复选框。可以有三种状态,对勾选择为2、方块为1(tristate)、不选为0。

1     connect(ui->checkBox, &QCheckBox::stateChanged, [=](int status){
2         if (0 == status) {
3             QMessageBox::information(this, "information0", QString(status));
4         } else if (1 == status) {
5             QMessageBox::information(this, "information1", QString(status));
6         } else {
7             QMessageBox::information(this, "information2", QString(status));
8         }
9     });

Containers

Group Box:可以将单选框/复选框分组,在本控件内的单选框/复选框为一组。
Widget:

Display Widgets

Label:可以显示文字、图片、动图。

1     ui->label->setPixmap(QPixmap(":/xxx/xxx.png")); //添加图盘对象
2     ui->label->setScaledContents(true); //设置填充
3     
4     QMovie *mv = new QMovie(":/xxx/xxx.png");
5     ui->label->setMovie(mv); //添加动图
6     ui->label->setScaledContents(true);
7     mv->start(); //开始动图

Item Views

List View
Tree View
Table View
Column View

Item Widgets:继承自Item Views,功能较多,建议使用

List Widget

1     QStringList names;
2     names << "55555" << "44444" << "33333" << "22222" << "11111";
3     ui->listWidget->addItems(names);
4     connect(ui->listWidget, &QListWidget::currentRowChanged, [=](int currentRow){
5         qDebug() << QString("选择的是第%1行").arg(currentRow);
6     });

Qt 常用控件介绍

Tree Widget

 1     //设置头标签
 2     ui->treeWidget->setHeaderLabels(QStringList() << "人物" << "人物简介");
 3     //创建项
 4     QTreeWidgetItem *item1 = new QTreeWidgetItem(QStringList() << "A公司");
 5     QTreeWidgetItem *item2 = new QTreeWidgetItem(QStringList() << "B公司");
 6     QTreeWidgetItem *item3 = new QTreeWidgetItem(QStringList() << "C公司");
 7     //添加top项
 8     ui->treeWidget->addTopLevelItem(item1);
 9     ui->treeWidget->addTopLevelItem(item2);
10     ui->treeWidget->addTopLevelItem(item3);
11     //创建项
12     QTreeWidgetItem *item1_1 = new QTreeWidgetItem(QStringList() << "张三" << "你好");
13     QTreeWidgetItem *item1_2 = new QTreeWidgetItem(QStringList() << "李四" << "你好");
14     QTreeWidgetItem *item2_1 = new QTreeWidgetItem(QStringList() << "张三" << "你好");
15     QTreeWidgetItem *item2_2 = new QTreeWidgetItem(QStringList() << "李四" << "你好");
16     QTreeWidgetItem *item3_1 = new QTreeWidgetItem(QStringList() << "张三" << "你好");
17     QTreeWidgetItem *item3_2 = new QTreeWidgetItem(QStringList() << "李四" << "你好");
18  
19     //添加子项
20     item1->addChild(item1_1);
21     item1->addChild(item1_2);
22     item2->addChild(item2_1);
23     item2->addChild(item2_2);
24     item3->addChild(item3_1);
25     item3->addChild(item3_2);

Qt 常用控件介绍
Table Widget

 1     //设置行列
 2     ui->tableWidget->setRowCount(5);
 3     ui->tableWidget->setColumnCount(3);
 4     //设置水平标签头
 5     ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "姓名" << "性别" << "年龄");
 6     //设置表格项
 7     QStringList names;
 8     names << "A" << "B" << "C" << "D" << "E";
 9     QStringList sex;
10     sex << "男" << "女" << "男" << "女" << "男";
11     for (int i = 0; i < 5; i++) {
12         ui->tableWidget->setItem(i, 0, new QTableWidgetItem(names[i]));
13         ui->tableWidget->setItem(i, 1, new QTableWidgetItem(sex[i]));
14         ui->tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(20 + i)));
15     }

Qt 常用控件介绍

上一篇:架构设计之CPU架构


下一篇:js 统计数组去重 (精简)