新建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. 机器人差速驱动方式(Differential Drive)
  2. ubuntu11.04下配置中文输入法
  3. 你的搜索其实很糟糕?
  4. ICCV 2019 | 清华等联合提出高精度、高效率点云3D重建网络框架PointMVSNet
  5. Springboot环境下mybatis配置多数据源配置
  6. ftp上传乱码_ftp同步图片到本地文件夹,ftp同步图片到本地文件夹的实现步骤
  7. mybatis if test 之 like concat()函数
  8. Skyline软件二次开发初级——4如何在WEB页面中的三维地图上使用弹出框Popups
  9. Taskctl是什么软件,有什么用?
  10. java邮箱格式校验
  11. 惠普服务器故障代码_惠普服务器常见问题及故障排除
  12. Matplotlib:线类型
  13. Word跨文件使用格式刷
  14. mysql flush explain_Mysql_mysql 性能分析及explain用法
  15. rstudio的数据集怎么建立和保存_在R Studio中保存
  16. 别再骂百度难用了,问题是你根本就不会用搜索引擎!
  17. macbook proa1708_识别 MacBook Pro 机型
  18. 协同过滤系统基于用户的评分预测
  19. JMM 8 大原子操作
  20. 营在微博:企业微博营销实战宝典

热门文章

  1. php强类型 vscode,白刃之战:PHP vs. ASP.NET(节选)-架构比较
  2. 10款超赞的Android智能车机软件
  3. springboot项目推荐的打包方式以及springboot项目的瘦身!!!!
  4. python建立复数数组_深入理解NumPy简明教程---数组1
  5. 腾讯云服务器建站小白教程
  6. 阿里刘振飞:我们用了10年,从去IOE到OceanBase世界第一!
  7. 【论文阅读】【CVPR2022】Contrastive Learning of Class-agnostic Activation Map
  8. 面向对象程序设计实验报告
  9. intel bsf指令
  10. 云南大学 计算机技术 调剂,云南大学调剂信息(全),希望对大家有所帮助!