#include
QDialog dlg(this); // 可以在栈区建立对象,因为对话框执行时是阻塞的
dlg.resize(200, 100); // 设置合适的大小,不然编译器会报警
dlg.exec(); // 代码会阻塞在这个位置,直到对话框被关闭
#include
QDialog dlg = new QDialog(this); // 由于对话框不是阻塞的,因此要在堆区创建
dlg->resize(200, 100); // 设置合适的大小,不然编译器会报警
dlg->setAttribute(Qt::WA_DeleteOnClose); // 当对话框关闭后自动释放内存
dlg->show(); // 代码不会阻塞在这个位置
QMessageBox
qDebug() << "critical:" << QMessageBox::critical(this, "标题", "错误对话框");
qDebug() << "information:" << QMessageBox::information(this, "标题", "提示对话框");
qDebug() << "warning:" << QMessageBox::warning(this, "标题", "警告对话框");
qDebug() << "question1:" << QMessageBox::question(this, "标题", "提问对话框1");
qDebug() << "question2:" << QMessageBox::question(this, "标题", "提问对话框2", "不退出","要退出");
qDebug() << "question3:" << QMessageBox::question(this, "标题", "提问对话框3", QMessageBox::Save|QMessageBox::Cancel);
qDebug() << "question4:" << QMessageBox::question(this, "标题", "提问对话框4", QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
QColorDialog
QColor color = QColorDialog::getColor(QColor(255, 0, 0));
qDebug() << "red:" << color.red() << " green:" << color.green() << " blue:" << color.blue();
QFileDialog
// 只能选择一个文件
QString str = QFileDialog::getOpenFileName(this, "打开文件", "d:/", "(*.md *.txt)");
qDebug() << "getOpenFileName:" << str;
// 可以同时选择多个文件
QStringList strlist = QFileDialog::getOpenFileNames(this, "打开文件", "d:/");
qDebug() << "getOpenFileNames:" << strlist;
QFontDialog
bool flag;
QFont font = QFontDialog::getFont(&flag, QFont("宋体", 11));
qDebug() << "flag: " << flag << " 字体: " << font.family().toUtf8().data() << " 字号: " << font.pointSize() << " 加粗: " << font.bold() << " 倾斜: " << font.italic();
QInputDialog
bool ok;
int i = QInputDialog::getInt(this, "设置", "请选择以下数值:", 25, 0, 100, 1, &ok);
qDebug() << "ok:" << ok << " i:" << i;
QStringList items;
items << "Spring" << "Summer" << "Fall" << "Winter";
QString item = QInputDialog::getItem(this, "设置", "选择", items, 0, false, &ok);
qDebug() << "getItem: " << item;
QProgressDialog
QProgressDialog *pd = new QProgressDialog("正在处理中...", "取消", 0, 100, this);
pd->setWindowTitle("请稍等。。。");
pd->setWindowModality(Qt::WindowModal); // 设置为模态对话框(可以不设置,默认为非模态)
pd->show();
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, [=]{
static int step = 1;
pd->setValue(step++);
if(step > pd->maximum() || pd->wasCanceled()){
timer->stop();
step = 0;
delete pd;
}
});
timer->start(100);
ToolButton
主要用来显示图片,可以通过设置 Icon 和 toolButtonStyle、autoRaise 属性进行美化(鼠标选中才凸起)
RadioButton
需要增加 GroupBox 来框住控件,那么该 GroupBox 里面的所有 RadioButton 控件只能选择一个
// checked: true->选中 false->未选中
connect(ui->radioButton, &QRadioButton::clicked, [=](bool checked){
qDebug() << "单选框被" << (checked ? "选中" : "取消");
});
CheckBox
QCheckBox->tristate 属性可以设置为半选状态
// state: 0->未选中 1->半选中 2->已选中
connect(ui->checkBox, &QCheckBox::stateChanged, [=](int state){
qDebug() << "复选框状态:" << state;
});
ListWidget
QListWidgetItem *item = new QListWidgetItem("单行加入");
item->setTextAlignment(Qt::AlignHCenter); // 设置水平居中
ui->listWidget->addItem(item);
QStringList list;
list << "以下为多行加入" << "窗前明月光" << "疑是地上霜" << "举头望明月" << "低头思故乡";
ui->listWidget->addItems(list);
TreeWidget
// 设置显示宽度根据内容自适应
QHeaderView *head = ui->treeWidget->header();
head->setSectionResizeMode(QHeaderView::ResizeToContents);
// 设置标题
ui->treeWidget->setHeaderLabels(QStringList() << "项目分类" << "项目介绍");
// 设置根
QTreeWidgetItem *toysItem = new QTreeWidgetItem(QStringList() << "玩具类" << "面向儿童玩具市场");
ui->treeWidget->addTopLevelItem(toysItem);
// 设置二级
QTreeWidgetItem *toysCarItem = new QTreeWidgetItem(QStringList() << "遥控车" << "遥控车类玩具");
toysItem->addChild(toysCarItem);
// 设置三级
QTreeWidgetItem *toysAiCarItem = new QTreeWidgetItem(QStringList() << "编程遥控车" << "主要面向儿童的遥控车类玩具");
toysCarItem->addChild(toysAiCarItem);
// 设置根
QTreeWidgetItem *iotItem = new QTreeWidgetItem(QStringList() << "物联网类" << "面向物联网市场");
ui->treeWidget->addTopLevelItem(iotItem);
// 设置二级
QTreeWidgetItem *iotSocketItem = new QTreeWidgetItem(QStringList() << "智能插座" << "一款可用于控制家电电源的产品");
iotItem->addChild(iotSocketItem);
// 设置自动展开所有
ui->treeWidget->expandAll();
// 根据内容自动调整所有行的行高
ui->tableWidget->resizeRowsToContents();
ui->tableWidget->resizeColumnsToContents();
// 设置单击选中一行的效果
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
// 其它列保持默认宽度,最后一列拉伸填满
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
// 设置表格不可编辑
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
// 去掉每行的行号
ui->tableWidget->verticalHeader()->setHidden(true);
// 设置行数和标题
QStringList titleList = { "姓名", "性别", "年龄", "介绍说明"};
ui->tableWidget->setColumnCount(titleList.count());
ui->tableWidget->setHorizontalHeaderLabels(titleList);
// 添加内容
QStringList nameList = {"张三", "李四", "关羽", "曹操", "花木兰"};
QStringList sexList = {"男", "男", "男", "男", "女"};
int ageList[] = {20, 21, 23, 25, 19};
QStringList descList = {"只在语文课本出现的神人", "与张三存在说不清楚的关系", "战国时期的忠义英雄", "宁负天下人", "巾帼不让须眉"};
int i = 0;
for(i = 0; i < nameList.count(); i++)
{
int col = 0;
ui->tableWidget->insertRow(i); // 插入一行
ui->tableWidget->setItem(i, col++, new QTableWidgetItem(nameList[i]));
ui->tableWidget->setItem(i, col++, new QTableWidgetItem(sexList[i]));
ui->tableWidget->setItem(i, col++, new QTableWidgetItem(QString::number(ageList[i])));
ui->tableWidget->setItem(i, col++, new QTableWidgetItem(descList[i]));
}
// 设置表格内所有的字体为幼圆, 要在设置内容后再设置字体, 否则会被覆盖
ui->tableWidget->setFont(QFont("宋体"));
// 针对单独条目设置颜色和字体以及对齐格式
QTableWidgetItem *item = new QTableWidgetItem("字体测试");
item->setBackground(QColor(255,250,250));
item->setForeground(QColor(0,0,255));
item->setFont(QFont("宋体"));
ui->tableWidget->insertRow(i);
ui->tableWidget->setItem(i, 0, item);
ui->tableWidget->item(i, 0)->setTextAlignment(Qt::AlignCenter);
// 默认选中倒数第一行
ui->tableWidget->setCurrentCell(i-1, QItemSelectionModel::Select);
ComboBox
ui->comboBox->addItem("飞机");
ui->comboBox->addItem("汽车");
ui->comboBox->addItem("摩托");
ui->comboBox->setCurrentIndex(1);
connect(ui->comboBox, &QComboBox::currentTextChanged, [=](QString text){
qDebug() << "QComboBox::currentTextChanged:" << text;
});
TextEdit 和 Plain Text Edit
Lable
// 显示文本
ui->label->setText("显示文本");
// 显示静态图
ui->label->setPixmap(QPixmap(":/jpeg/1.jpg"));
// 显示动态图
QMovie *movie= new QMovie(":/jpeg/1.gif");
ui->label->setMovie(movie);
movie->start();