当前位置: 代码迷 >> 综合 >> Qt用代码实现菜单栏(MenuBar)和工具栏(ToolBar)
  详细解决方案

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

热度:99   发布时间:2023-12-03 04:35:53.0

新建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()
{}

 

界面效果如图:

 

本次只展示了如何用代码创建界面(工具栏和菜单栏),里面的函数功能为空,并没有实现,下一章博客里会实现相应图片查看功能。

  相关解决方案