当前位置:   article > 正文

QT系列第3节 QT中混合UI设计_qt ui设计

qt ui设计

QT开发过程中,经常使用Qt designer设计器和代码方式结合来及进行ui设计,本节将介绍这两种方式混合进行ui开发。

目录

1.工程添加图片资源

 2.添加菜单

3.添加工具栏

4.简单文本编辑器实现

5. QT Creator常用快捷键

6.qml修改应用程序图标

7.qml中修改应用程序图标


1.工程添加图片资源

(1)项目鼠标右键 - 添加新文件

(2) Qt -> Qt Resource File

 (3)输入资源名称

(4)添加到工程,资源文件也是工程文件的一部分呢

(5)工程m目录中就有资源文件

 (6)给资源添加前缀,一个前缀可以理解为一个分组,多个前缀就是多个分组,在资源文件比较多时,通过添加不同的前缀进行分组管理很有必要。

 (7)将图片资源拷贝到工程目录系下,否则打包时候找不到

如下图显示,图片资源目录和工程文件.pro处在相同的目录

 (8)添加资源文件

可以看到在前缀/images下增加了好多图片资源

 2.添加菜单

(1)添加顶级菜单,输入后回车就会跳转到添加另外一项

 (2)添加子菜单

添加子菜单图标

 

 

直接键盘 ctrl  N可一增加快捷键

  (3)拖拽菜单

3.添加工具栏

在设计器界面鼠标右键

 

 设置工具栏文字显示在图标下面

 给Action添加信号和槽

说明:创建了Action以后,可以将其拖拽到子菜单,也可以拖拽到子工具栏,对应同样的功能。

ACtion很有用,因为其可以创建菜单项,也可以创建工具栏按钮。如下图不管时点击菜单项还时工具栏按钮,触发的动作都是一样的。

 

4.简单文本编辑器实现

(1)效果

 (2)代码结构

(3)关键代码

huihe_gui.pro 

  1. QT += core gui
  2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  3. CONFIG += c++17
  4. # You can make your code fail to compile if it uses deprecated APIs.
  5. # In order to do so, uncomment the following line.
  6. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
  7. SOURCES += \
  8. main.cpp \
  9. mainwindow.cpp
  10. HEADERS += \
  11. mainwindow.h
  12. FORMS += \
  13. mainwindow.ui
  14. # Default rules for deployment.
  15. qnx: target.path = /tmp/$${TARGET}/bin
  16. else: unix:!android: target.path = /opt/$${TARGET}/bin
  17. !isEmpty(target.path): INSTALLS += target
  18. RESOURCES += \
  19. res.qrc
  20. RC_ICONS = edit_work.ico

main.cpp

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. MainWindow w;
  7. w.show();
  8. return a.exec();
  9. }

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QProgressBar>
  5. #include <QLabel>
  6. #include <QSpinBox>
  7. #include <QFontComboBox>
  8. QT_BEGIN_NAMESPACE
  9. namespace Ui { class MainWindow; }
  10. QT_END_NAMESPACE
  11. class MainWindow : public QMainWindow
  12. {
  13. Q_OBJECT
  14. private:
  15. QProgressBar *m_progressBar;
  16. QSpinBox *m_spinFontSize;
  17. QFontComboBox *m_comFont;
  18. QLabel *m_fLabCurFile;
  19. void initUi();
  20. void initSignalSlots();
  21. public:
  22. MainWindow(QWidget *parent = nullptr);
  23. ~MainWindow();
  24. private slots:
  25. void on_actionBold_triggered(bool checked);
  26. void on_textEdit_copyAvailable(bool b);
  27. void on_textEdit_selectionChanged();
  28. void on_spinBoxFontSize_valueChanged(int size);
  29. void on_comboFont_currentIndexChanged(const QString &arg1);
  30. private:
  31. Ui::MainWindow *ui;
  32. };
  33. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. MainWindow::MainWindow(QWidget *parent)
  4. : QMainWindow(parent)
  5. , ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8. initUi();
  9. initSignalSlots();
  10. // textEdit自适应主窗口
  11. setCentralWidget(ui->textEdit);
  12. }
  13. MainWindow::~MainWindow()
  14. {
  15. delete ui;
  16. }
  17. void MainWindow::initUi() {
  18. m_fLabCurFile = new QLabel;
  19. m_fLabCurFile->setMidLineWidth(150);
  20. m_fLabCurFile->setText("当前文件:");
  21. ui->statusbar->addWidget(m_fLabCurFile);
  22. m_progressBar = new QProgressBar;
  23. m_progressBar->setMinimum(10);
  24. m_progressBar->setMaximum(100);
  25. m_progressBar->setValue(ui->textEdit->font().pointSize());
  26. ui->statusbar->addWidget(m_progressBar);
  27. m_spinFontSize = new QSpinBox;
  28. m_spinFontSize->setMinimum(10);
  29. m_spinFontSize->setMaximum(100);
  30. ui->toolBar->addWidget(new QLabel("字体大小"));
  31. ui->toolBar->addWidget(m_spinFontSize);
  32. m_comFont = new QFontComboBox;
  33. ui->toolBar->addWidget(new QLabel("字体"));
  34. ui->toolBar->addWidget(m_comFont);
  35. }
  36. void MainWindow::initSignalSlots()
  37. {
  38. connect(m_spinFontSize, SIGNAL(valueChanged(int)),
  39. this, SLOT(on_spinBoxFontSize_valueChanged(int)));
  40. connect(m_comFont, SIGNAL(currentTextChanged(const QString &)),
  41. this, SLOT(on_comboFont_currentIndexChanged(const QString &)));
  42. }
  43. void MainWindow::on_actionBold_triggered(bool checked)
  44. {
  45. QTextCharFormat fmt;
  46. if (checked) {
  47. fmt.setFontWeight(QFont::Bold);
  48. }
  49. else {
  50. fmt.setFontWeight(QFont::Normal);
  51. }
  52. ui->textEdit->mergeCurrentCharFormat(fmt);
  53. }
  54. void MainWindow::on_textEdit_copyAvailable(bool b)
  55. {
  56. // 是否可以拷贝文字的状态
  57. ui->actionCut->setEnabled(b);
  58. ui->actionCopy->setEnabled(b);
  59. ui->actionPaust->setEnabled(ui->textEdit->canPaste());
  60. }
  61. void MainWindow::on_textEdit_selectionChanged()
  62. {
  63. QTextCharFormat fmt;
  64. ui->actionBold->setChecked(fmt.font().bold());
  65. }
  66. void MainWindow::on_spinBoxFontSize_valueChanged(int size)
  67. {
  68. QTextCharFormat fmt;
  69. fmt.setFontPointSize(size);
  70. ui->textEdit->mergeCurrentCharFormat(fmt);
  71. m_progressBar->setValue(size);
  72. }
  73. void MainWindow::on_comboFont_currentIndexChanged(const QString &arg1)
  74. {
  75. QTextCharFormat fmt;
  76. fmt.setFontFamily(arg1);
  77. ui->textEdit->mergeCurrentCharFormat(fmt);
  78. }

5. QT Creator常用快捷键

6.qml修改应用程序图标

7.qml中修改应用程序图标

IDI_ICON1 ICON DISCARDABLE "app.ico"

图片转换网站
https://jinaconvert.com/cn/index.php

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/560855
推荐阅读
相关标签
  

闽ICP备14008679号