新建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. 【欧拉回路】解题报告:luogu P6066 [USACO]Watchcow (欧拉回路详解)【模板】
  2. 不写技术文档是个什么梗
  3. Android Studio 从入门到精通
  4. 12.history的用法
  5. python五子棋游戏15*15_在STM32上运行五子棋小游戏(15x15)
  6. sass封装h5适配文件
  7. html 表格行排序,用客户端HTML表格排序对行进行分组
  8. 4-1 面向对象概述
  9. JAVA只要掌握内部类,多继承和单继承都不是问题
  10. sql 2008 每次打开一个表都要登录_如何实现一个简易的orm
  11. 如何手动删除并重新安装 .NET Framework 2.0
  12. HTML+CSS静态页面`西安旅游网站设计——西安旅游(7页) 大学生旅游网页作品 出行网页设计作业模板 学生游玩网页制作源代码下载
  13. Helm — Chart介绍
  14. 英语3500词(16/20)trade主题(2022.1.28)
  15. 特步公布全新战略定位世界级中国跑鞋;电通集团计划过渡到新的全球集成领导架构 | 美通企业日报...
  16. Boost库-功能介绍-Geometry-图形开发库-计算几何-常用功能封装-GraphicalDebugging(二)
  17. MVP模式请求网络数据
  18. flutter需要定义product flavors
  19. 漫画:有趣的扔鸡蛋问题
  20. 作用域插槽、slot-scope、v-slot指令

热门文章

  1. html添加花瓣,JS绘制生成花瓣效果的方法
  2. Linux应用层查看系统时间的方法
  3. 单片机C51期末复习答案
  4. ss: Display Linux TCP / UDP Network and Socket ...
  5. android lr 输出分辨率设置,LR输出照片时的分辨率究竟输出为多少?
  6. 【系】微信小程序云开发实战坚果商城-前后端交互之订单实现
  7. uniapp获取手机号(详细教程附代码)
  8. Web上的用户登录功能安全
  9. Android 3D游戏基础介绍
  10. linux mint 卸载compiz,Linux Mint 11 开启 Compiz Fusion 特效