Qt from entry to actual combat
first day
https://blog.csdn.net/qq_40507857/article/details/125730739
the next day
2.1 Window with menu bar
QMainWindow is a class that provides the main window program, including a menu bar (Menu bar), multiple toolbars (tool bars), multiple anchor components (dock widgets), and a central component (central widget).
- There is only one menu bar
//菜单栏创建
QMenuBar *bar=new QMenuBar();
//将菜单栏放入窗口中
setMenuBar(bar);
//创建菜单
QMenu *fileMenu=bar->addMenu("文件");
QMenu *editMenu=bar->addMenu("编辑");
//创建菜单项
QAction *newAction1 = fileMenu->addAction("新建1");
QAction *newAction2 = fileMenu->addAction("新建2");
//添加分割线
fileMenu->addSeparator();
QAction *newAction3 = fileMenu->addAction("新建3");
- Toolbars can have multiple
//工具栏 可以有多个
QToolBar *toolBar=new QToolBar(this);
addToolBar(Qt::LeftToolBarArea,toolBar);
//后期设置 只允许左右停靠
toolBar->setAllowedAreas(Qt::LeftToolBarArea|Qt::RightToolBarArea);
//设置浮动
toolBar->setFloatable(false);
//设置移动(总开关)
toolBar->setMovable(false);
//工具栏中设置内容
toolBar->addAction(newAction1);
toolBar->addSeparator();
toolBar->addAction(newAction2);
toolBar->addAction(newAction3);
QPushButton *btn=new QPushButton(this);
btn->setText("aaa");
toolBar->addWidget(btn);
- Status bar at most one
//状态栏 最多有一个
QStatusBar *stBar=statusBar();
//设置到窗口中
setStatusBar(stBar);
//放标签控件
QLabel *label=new QLabel("提示信息",this);
stBar->addWidget(label);
QLabel *label2=new QLabel("右侧提示信息",this);
stBar->addPermanentWidget(label2);
- Floating windows can have multiple
//铆接部件(浮动窗口)可以有多个
QDockWidget *dockWidget=new QDockWidget("浮动",this);
addDockWidget(Qt::BottomDockWidgetArea,dockWidget);
- There can only be one central part
//设置中心部件 只能有一个
QTextEdit *edit=new QTextEdit(this);
setCentralWidget(edit);
2.2 Resource files
- Copy the picture folder to the project location
- Right-click the project-"Add New File-"Qt-"Qt resource file-"Name the resource folder
- Generate: resource folder name.qrc
- Right-click the resource folder name.qrc, open in editor for editing
- Add the prefix first, then add the file
- Use: ":+prefix+filename"
//使用添加Qt资源 ": + 前缀名 + 文件名"
ui->actionnew->setIcon(QIcon(":/Image/img1.jpg"));
ui->actionopen->setIcon(QIcon(":/Image/img2.jpg"));
2.3 Dialog
Modal dialog box (cannot operate on other windows)
connect(ui->actionNew,&QAction::triggered,[=](){
//对话框 分类
//模态对话框 (不可以对其他窗口进行操作)
//非模态对话框(可以对其他窗口进行操作)
//模态创建 阻塞
QDialog dlg(this);
dlg.resize(200,100);
dlg.exec();
qDebug()<<"模态对话框弹出了";
});
Non-modal dialog box (can operate on other windows)
connect(ui->actionNew,&QAction::triggered,[=](){
QDialog *dlg2=new QDialog(this);
dlg2->resize(200,100);
dlg2->setAttribute(Qt::WA_DeleteOnClose);
dlg2->show();
qDebug()<<"非模态对话框弹出了";
});
standard dialog
- QMessageBox static member function to create a dialog box
- error, information, question, warning
- Parameter 1: parent, parameter 2: title, parameter 3: display content, parameter 4: key type, parameter 5: default associated enter key
- The return value is also of StandardButton type, use the return value to judge user input
//消息对话框
connect(ui->actionNew,&QAction::triggered,[=](){
//错误对话框
QMessageBox::critical(this,"critical","错误");
//信息对话框
QMessageBox::information(this,"information","信息");
//提问对话框
//参数1:父亲,参数2:标题,参数3:提示内容,参数4:按键类型,参数5:默认关联回车按键
if(QMessageBox::Save==QMessageBox::question(this,"question","问题",QMessageBox::Save|QMessageBox::Cancel,QMessageBox::Cancel)){
qDebug()<<"Save";
}else{
qDebug()<<"Cancel";
}
//警告对话框
QMessageBox::warning(this,"warning","警告");
other dialog
//颜色对话框
QColor color=QColorDialog::getColor(QColor(255,0,0));
qDebug()<<"r="<<color.red()<<"g="<<color.green()<<"b="<<color.blue();
//选择文件对话框
QString qstr=QFileDialog::getOpenFileName(this,"打开文件","C:\\Users\\hulin\\Desktop","(*.pdf)");
qDebug()<<qstr;
//选择字体对话框
bool flag;
QFont font =QFontDialog::getFont(&flag,QFont("微软雅黑",36));
qDebug()<<"字体:"<<font.family()<<"字号:"<<font.pointSize()<<"加粗:"<<font.bold()<<"倾斜:"<<font.italic();
2.4 Interface Layout
- Implement the login window
- Use the layout method to beautify the window
- Select Widget for layout, horizontal layout, vertical layout, grid layout
- Layout for account, password, login, cancel buttons
- There is a 9 pixel gap between the default window and the control
- Layout with Spring
2.5 Common Controls
button group
- QPushButton common button
- The QToolButton tool button is used to display pictures. If you want to display text, modify the style: toolButtonStyle, raised style autoRaise
- radioButton radio button, set the default
- checkbox Multi-choice button, monitoring state, 2 is selected, 1 is half-selected, 0 is not selected
ListWidget list container
- QListWidgetItem * Item content of a row
- ui->listWidget->addItem(Item)
- Set the centering method item->setTextAlignment(Qt::AlignHCenter)
- You can use addItems to add the entire content at once
//利用listwidget写诗
QListWidgetItem *item=new QListWidgetItem("锄禾日当午");
//将一行诗放到listWidget控件中
ui->listWidget->addItem(item);
//QStringList QList<QString>
QStringList list;
list<<"锄禾日当午"<<"汗滴禾下土"<<"谁知盘中餐"<<"粒粒皆辛苦";
ui->listWidget->addItems(list);
QTreeWidget tree control
- set header
- create root node
- Add root node to tree control
- add child node
//设置头
ui->treeWidget->setHeaderLabels(QStringList()<<"标题"<<"内容");
//创建根节点
QTreeWidgetItem *item_1=new QTreeWidgetItem(QStringList()<<"标题1");
QTreeWidgetItem *item_2=new QTreeWidgetItem(QStringList()<<"标题2");
QTreeWidgetItem *item_3=new QTreeWidgetItem(QStringList()<<"标题3");
//添加根节点到树控件上
ui->treeWidget->addTopLevelItem(item_1);
ui->treeWidget->addTopLevelItem(item_2);
ui->treeWidget->addTopLevelItem(item_3);
//添加子节点
QTreeWidgetItem *item_1_1=new QTreeWidgetItem(QStringList()<<"标题1.1"<<"内容1.1");
item_1->addChild(item_1_1);
QTreeWidgetItem *item_1_2=new QTreeWidgetItem(QStringList()<<"标题1.2"<<"内容1.2");
item_1->addChild(item_1_2);
QTreeWidgetItem *item_2_1=new QTreeWidgetItem(QStringList()<<"标题2.1"<<"内容2.1");
item_2->addChild(item_2_1);
QTreeWidgetItem *item_2_2=new QTreeWidgetItem(QStringList()<<"标题2.2"<<"内容2.2");
item_2->addChild(item_2_2);
QTreeWidgetItem *item_3_1=new QTreeWidgetItem(QStringList()<<"标题3.1"<<"内容3.1");
item_3->addChild(item_3_1);
QTreeWidgetItem *item_3_2=new QTreeWidgetItem(QStringList()<<"标题3.2"<<"内容3.2");
item_3->addChild(item_3_2);
QTableWidget form control
//表格控件
//设置列数
ui->tableWidget->setColumnCount(3);
//设置表头
ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"姓名"<<"性别"<<"年龄");
//设置行数
ui->tableWidget->setRowCount(3);
//设置正文
QStringList nameList;
nameList<<"亚瑟"<<"赵云"<<"花木兰";
QList<QString> sexList;
sexList<<"男"<<"男"<<"女";
for(int i=0;i<3;i++){
int col=0;
ui->tableWidget->setItem(i,col++,new QTableWidgetItem(nameList[i]));
ui->tableWidget->setItem(i,col++,new QTableWidgetItem(sexList.at(i)));
//int 转 QString
ui->tableWidget->setItem(i,col++,new QTableWidgetItem(QString::number(i+18)));
}
2.6 Custom controls
- Use of stack controls
//栈控件的使用
//设置默认定位
ui->stackedWidget->setCurrentIndex(1);
connect(ui->pushButton_ScrollArea,&QPushButton::clicked,[=](){
ui->stackedWidget->setCurrentIndex(0);
});
connect(ui->PushButton_ToolBox,&QPushButton::clicked,[=](){
ui->stackedWidget->setCurrentIndex(1);
});
connect(ui->PushButton_TabWidget,&QPushButton::clicked,[=](){
ui->stackedWidget->setCurrentIndex(2);
});
- drop down box
//下拉框
ui->comboBox->addItem("奔驰");
ui->comboBox->addItem("宝马");
ui->comboBox->addItem("奥迪");
- display image
- Show animation
ui->label_img->setPixmap(QPixmap(":/Image/img1.jpg"));
QMovie *movie=new QMovie("/Image/img7.gif");
ui->label_gif->setMovie(movie);
movie->start();