新建Qt项目,选择Qt Widgets Application,填入项目名称“ImageView”,点击完成。

在Qt Designer里会生成如图所示的几个文件:

此时我们右键删除imageviewer.ui这个文件,因为我们本次是用纯代码的方式生成界面,所以不需要这个ui文件了。

关键代码主要在imageviewer.h和imageviewer.cpp里。下面是代码:

imageviewer.h

#ifndef IMAGEVIEWER_H
#define IMAGEVIEWER_H#include <QMainWindow>
#include <QLabel>
#include <QScrollArea>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>namespace Ui {class ImageViewer;
}class ImageViewer : public QMainWindow
{Q_OBJECTpublic:explicit ImageViewer(QWidget *parent = nullptr);~ImageViewer();void initMenu();void initToolBar();void initConnect();private:Ui::ImageViewer *ui;QLabel *imageLabel;QScrollArea *scrollAera;QMenu *fileMenu;QMenu *viewMenu;QMenu *helpMenu;QToolBar *fileToolBar;QAction *openAct;QAction *printAct;QAction *exitAct;QAction *zoomInAct;QAction *zoomOutAct;QAction *normalSizeAct;QAction *fitToWindowAct;QAction *aboutAct;QAction *aboutQtAct;private slots:void open();void print();void exit();void zoomIn();void zoomOut();void normalSize();void fitToWindow();void about();void aboutQt();
};#endif // IMAGEVIEWER_H

imageviewer.cpp

#include "imageviewer.h"
#include "ui_imageviewer.h"ImageViewer::ImageViewer(QWidget *parent) :QMainWindow(parent),ui(new Ui::ImageViewer)
{ui->setupUi(this);//initalizeinitMenu();initToolBar();initConnect();
}ImageViewer::~ImageViewer()
{delete ui;
}void ImageViewer::initMenu()
{//QMenuBar *menuBar = this->menuBar();//add three menusfileMenu = new QMenu(tr("&File"),this);viewMenu = new QMenu(tr("&View"),this);helpMenu = new QMenu(tr("&About"),this);//add actions and add it into corresponding menuopenAct = new QAction(tr("&Open"),this);openAct -> setShortcut(tr("ctrl+O"));printAct = new QAction(tr("&Print"),this);printAct -> setShortcut(tr("ctrl+P"));exitAct = new QAction(tr("&Exit"),this);exitAct -> setShortcut(tr("ctrl+Q"));fileMenu -> addAction(openAct);fileMenu -> addAction(printAct);fileMenu -> addSeparator();fileMenu -> addAction(exitAct);zoomInAct = new QAction(tr("Zoom &In"),this);zoomInAct -> setShortcut(tr("ctrl+="));zoomOutAct = new QAction(tr("Zoom &Out"),this);zoomOutAct -> setShortcut(tr("ctrl+-"));normalSizeAct = new QAction(tr("&Normal Size"),this);normalSizeAct -> setShortcut(tr("ctrl+S"));fitToWindowAct = new QAction(tr("&Fit to Window"),this);fitToWindowAct -> setShortcut(tr("ctrl+F"));viewMenu -> addAction(zoomInAct);viewMenu -> addAction(zoomOutAct);viewMenu -> addAction(normalSizeAct);viewMenu -> addSeparator();viewMenu -> addAction(fitToWindowAct);aboutAct = new QAction(tr("&About"),this);aboutQtAct = new QAction(tr("&About Qt"),this);helpMenu -> addAction(aboutAct);helpMenu -> addAction(aboutQtAct);//add menus to menubarmenuBar() -> addMenu(fileMenu);menuBar() -> addMenu(viewMenu);menuBar() -> addMenu(helpMenu);
}void ImageViewer::initToolBar()
{//add a toolbar and add its actionsfileToolBar = new QToolBar(this);fileToolBar -> addAction(openAct);fileToolBar -> addAction(printAct);fileToolBar -> addAction(exitAct);addToolBar(Qt::TopToolBarArea,fileToolBar);
}void ImageViewer::initConnect()
{//singals and slotsconnect(openAct,SIGNAL(triggered),this,SLOT(open()));connect(printAct,SIGNAL(triggered),this,SLOT(print()));connect(exitAct,SIGNAL(triggered),this,SLOT(exit()));connect(zoomInAct,SIGNAL(triggered),this,SLOT(zoomIn()));connect(zoomOutAct,SIGNAL(triggered),this,SLOT(zoomOut()));connect(normalSizeAct,SIGNAL(triggered),this,SLOT(normalSize()));connect(fitToWindowAct,SIGNAL(triggered),this,SLOT(fitToWindow()));connect(aboutAct,SIGNAL(triggered),this,SLOT(about()));connect(aboutQtAct,SIGNAL(triggered),this,SLOT(aboutQt()));
}//implement slot functions
void ImageViewer::open()
{}void ImageViewer::print()
{}void ImageViewer::exit()
{}void ImageViewer::zoomIn()
{}void ImageViewer::zoomOut()
{}void ImageViewer::normalSize()
{}void ImageViewer::fitToWindow()
{}void ImageViewer::about()
{}void ImageViewer::aboutQt()
{}

界面效果如图:

Qt 用代码实现菜单栏(MenuBar)和工具栏(ToolBar)相关推荐

  1. Qt用代码实现菜单栏(MenuBar)和工具栏(ToolBar)

    新建Qt项目,选择Qt Widgets Application,填入项目名称"ImageView",点击完成. 在Qt Designer里会生成如图所示的几个文件: 此时我们右键删 ...

  2. Qt纯代码实现菜单栏、工具栏、状态栏

    目录 菜单栏 工具栏 状态栏 总体效果 在QWidget中实现菜单栏.工具栏.状态栏 其他 子窗口获取父窗口指针 QWidget阻塞模式 本篇演示的例子是在QMainWindow中进行的,在QWidg ...

  3. Python Qt GUI设计:菜单栏、工具栏和状态栏的使用方法(拓展篇—2)

    目录 1.菜单栏 1.1.Qt Creator创建菜单栏 1.2. 菜单栏类创建菜单栏 2.工具栏 2.1.Qt Creator创建工具栏 2.2. 工具栏类创建工具栏 3.状态栏 在使用Qt Cre ...

  4. 【Qt学习】---- 实战|菜单栏

    Qt中用代码实现一个菜单栏 #include "mainwindow.h" #include <QDebug> #include <QMenuBar> // ...

  5. Qt Creator代码重构

    Qt Creator代码重构 代码重构 查找符号 查找QML类型 查看搜索结果 重命名符号 列编辑 应用重构动作 创建功能 插入虚函数 创建获取器和设置器 重构动作摘要 重构C ++代码 重构QML代 ...

  6. Qt 使用代码编写的自定义控件类

    Qt 使用代码编写的自定义控件类 首先需要完成继承QWidget 或者Qt 原生控件类的类编写实现 在需要使用自定义控件类的 UI 文件中添加一个 自定义类的控件(也就是自定义类继承的控件) 将这个控 ...

  7. VS2008中 没有QT的代码智能提示

    2008本身自带有注释,本人不太喜欢VC助手把界面弄的很烦.配好QT环境之后,发现QT的代码没有智能提示,VC的代码却有智能提示. 原因是QT的一些文件没有包含到VS2008中,做了以下包含: (1) ...

  8. Qt+MySQL:在Qt中用代码新建数据库

    一般都是在MySQL的命令行或者workbench中建一个数据库,然后直接用Qt对已存在的数据库进行连接,像这样: // 连接数据库database_1=QSqlDatabase::addDataba ...

  9. QT纯代码打造音乐播放器

    QT纯代码打造音乐播放器 在.pro文件中添加 QT = prmultimedia 然后就是在.h文件中添加相关库函数 我添加的库函数 有些是不需要的,可以自定义删除修改 我是为方便以后扩展功能就留下 ...

最新文章

  1. 拼多多面试|如何用 Redis 统计独立用户访问量?
  2. 他用“1 和 0”解决了人类两大难题,他是信息论之父,却渴望做“杂耍博士”...
  3. TX2 开发套件串口
  4. Samba与Vsftpd结合在企业中的应用
  5. scikit-image 几个案例(下)
  6. python gui tkinter_python学习之GUI(Tkinter)
  7. JavaScript中的数学对象Math
  8. 桶排序算法c语言10个数组,桶排序算法
  9. mysql的备份与恢复_实验十一 MySQLl备份与恢复1
  10. python︱写markdown一样写网页,代码快速生成web工具:streamlit 缓存(五)
  11. 3803. 数组去重-AcWing题库
  12. 2014全国计算机等级考试四级数据库工程师考试大纲,全国计算机等级考试四级数据库工程师...
  13. 桌面计算机图标不删除,电脑小技巧:教你删除桌面无法删除的图标
  14. SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-003-四种方式获取DataSource...
  15. STM32开发笔记108:将STM32CubeIDE设置为中文
  16. Vue基础语法必知必会
  17. Jmeter-BeanShell后置处理器
  18. Visual Studio Code(VSCode) 编辑/编译/调试 C++ 代码
  19. 电子计算机的基本思想,冯-诺依曼原理的基本思想
  20. 电子邮件签名档 HTML 手写时的折腾(附 原创工具)

热门文章

  1. 2020年中国燃气灶行业发展现状分析,受房地产政策影响,行业需求减弱「图」
  2. React插件及动画
  3. MongoDB-数据库系列第二次
  4. 国内首秀元宇宙Live House圆满收官,百事可乐虚拟偶像真的好会!!
  5. 水木joke十月画月刊
  6. 使用U盘启动盘进入winPE后蓝屏怎么解决
  7. C语言消除未使用变量的警告
  8. Bayesian Model Averaging (BMA)的R实现
  9. Loj #2983. 「WC2019」数树
  10. 记一次解析旧版本Excel文件