赞
踩
QT开发过程中,经常使用Qt designer设计器和代码方式结合来及进行ui设计,本节将介绍这两种方式混合进行ui开发。
目录
(1)项目鼠标右键 - 添加新文件
(2) Qt -> Qt Resource File
(3)输入资源名称
(4)添加到工程,资源文件也是工程文件的一部分呢
(5)工程m目录中就有资源文件
(6)给资源添加前缀,一个前缀可以理解为一个分组,多个前缀就是多个分组,在资源文件比较多时,通过添加不同的前缀进行分组管理很有必要。
(7)将图片资源拷贝到工程目录系下,否则打包时候找不到
如下图显示,图片资源目录和工程文件.pro处在相同的目录
(8)添加资源文件
可以看到在前缀/images下增加了好多图片资源
(1)添加顶级菜单,输入后回车就会跳转到添加另外一项
(2)添加子菜单
添加子菜单图标
直接键盘 ctrl N可一增加快捷键
(3)拖拽菜单
在设计器界面鼠标右键
设置工具栏文字显示在图标下面
给Action添加信号和槽
说明:创建了Action以后,可以将其拖拽到子菜单,也可以拖拽到子工具栏,对应同样的功能。
ACtion很有用,因为其可以创建菜单项,也可以创建工具栏按钮。如下图不管时点击菜单项还时工具栏按钮,触发的动作都是一样的。
(1)效果
(2)代码结构
(3)关键代码
huihe_gui.pro
- QT += core gui
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- CONFIG += c++17
-
- # You can make your code fail to compile if it uses deprecated APIs.
- # In order to do so, uncomment the following line.
- #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
-
- SOURCES += \
- main.cpp \
- mainwindow.cpp
-
- HEADERS += \
- mainwindow.h
-
- FORMS += \
- mainwindow.ui
-
- # Default rules for deployment.
- qnx: target.path = /tmp/$${TARGET}/bin
- else: unix:!android: target.path = /opt/$${TARGET}/bin
- !isEmpty(target.path): INSTALLS += target
-
- RESOURCES += \
- res.qrc
-
- RC_ICONS = edit_work.ico
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
main.cpp
- #include "mainwindow.h"
-
- #include <QApplication>
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QMainWindow>
- #include <QProgressBar>
- #include <QLabel>
- #include <QSpinBox>
- #include <QFontComboBox>
-
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- private:
- QProgressBar *m_progressBar;
- QSpinBox *m_spinFontSize;
- QFontComboBox *m_comFont;
- QLabel *m_fLabCurFile;
-
- void initUi();
- void initSignalSlots();
-
- public:
- MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
-
- private slots:
- void on_actionBold_triggered(bool checked);
-
- void on_textEdit_copyAvailable(bool b);
-
- void on_textEdit_selectionChanged();
-
- void on_spinBoxFontSize_valueChanged(int size);
- void on_comboFont_currentIndexChanged(const QString &arg1);
-
- private:
- Ui::MainWindow *ui;
- };
- #endif // MAINWINDOW_H
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- initUi();
- initSignalSlots();
- // textEdit自适应主窗口
- setCentralWidget(ui->textEdit);
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
- void MainWindow::initUi() {
- m_fLabCurFile = new QLabel;
- m_fLabCurFile->setMidLineWidth(150);
- m_fLabCurFile->setText("当前文件:");
- ui->statusbar->addWidget(m_fLabCurFile);
-
- m_progressBar = new QProgressBar;
- m_progressBar->setMinimum(10);
- m_progressBar->setMaximum(100);
- m_progressBar->setValue(ui->textEdit->font().pointSize());
- ui->statusbar->addWidget(m_progressBar);
-
- m_spinFontSize = new QSpinBox;
- m_spinFontSize->setMinimum(10);
- m_spinFontSize->setMaximum(100);
- ui->toolBar->addWidget(new QLabel("字体大小"));
- ui->toolBar->addWidget(m_spinFontSize);
-
- m_comFont = new QFontComboBox;
- ui->toolBar->addWidget(new QLabel("字体"));
- ui->toolBar->addWidget(m_comFont);
- }
-
- void MainWindow::initSignalSlots()
- {
- connect(m_spinFontSize, SIGNAL(valueChanged(int)),
- this, SLOT(on_spinBoxFontSize_valueChanged(int)));
- connect(m_comFont, SIGNAL(currentTextChanged(const QString &)),
- this, SLOT(on_comboFont_currentIndexChanged(const QString &)));
- }
-
- void MainWindow::on_actionBold_triggered(bool checked)
- {
- QTextCharFormat fmt;
- if (checked) {
- fmt.setFontWeight(QFont::Bold);
- }
- else {
- fmt.setFontWeight(QFont::Normal);
- }
- ui->textEdit->mergeCurrentCharFormat(fmt);
- }
-
-
- void MainWindow::on_textEdit_copyAvailable(bool b)
- {
- // 是否可以拷贝文字的状态
- ui->actionCut->setEnabled(b);
- ui->actionCopy->setEnabled(b);
- ui->actionPaust->setEnabled(ui->textEdit->canPaste());
- }
-
- void MainWindow::on_textEdit_selectionChanged()
- {
- QTextCharFormat fmt;
- ui->actionBold->setChecked(fmt.font().bold());
- }
-
- void MainWindow::on_spinBoxFontSize_valueChanged(int size)
- {
- QTextCharFormat fmt;
- fmt.setFontPointSize(size);
- ui->textEdit->mergeCurrentCharFormat(fmt);
- m_progressBar->setValue(size);
- }
-
- void MainWindow::on_comboFont_currentIndexChanged(const QString &arg1)
- {
- QTextCharFormat fmt;
- fmt.setFontFamily(arg1);
- ui->textEdit->mergeCurrentCharFormat(fmt);
- }
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
IDI_ICON1 ICON DISCARDABLE "app.ico"
图片转换网站
https://jinaconvert.com/cn/index.php
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。