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

新建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_OBJECT

public:
    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);

    //initalize
    initMenu();
    initToolBar();
    initConnect();
}

ImageViewer::~ImageViewer()
{
    delete ui;
}

void ImageViewer::initMenu()
{
    //QMenuBar *menuBar = this->menuBar();
    //add three menus
    fileMenu = new QMenu(tr("&File"),this);
    viewMenu = new QMenu(tr("&View"),this);
    helpMenu = new QMenu(tr("&About"),this);

    //add actions and add it into corresponding menu
    openAct = 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 menubar
    menuBar() -> addMenu(fileMenu);
    menuBar() -> addMenu(viewMenu);
    menuBar() -> addMenu(helpMenu);
}

void ImageViewer::initToolBar()
{
    //add a toolbar and add its actions
     fileToolBar = new QToolBar(this);

     fileToolBar -> addAction(openAct);
     fileToolBar -> addAction(printAct);
     fileToolBar -> addAction(exitAct);

     addToolBar(Qt::TopToolBarArea,fileToolBar);
}

void ImageViewer::initConnect()
{
    //singals and slots
    connect(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()
{

}

 

界面效果如图:

 

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

可以使用 `QMenuBar` 的 `hovered` 信号来实现鼠标在菜单栏移动时切换工具栏。具体实现代码如下: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QMenuBar, QToolBar, QAction class MyWindow(QMainWindow): def __init__(self): super().__init__() self.init_ui() def init_ui(self): # 创建菜单栏 menuBar = self.menuBar() # 创建工具栏 toolBar1 = QToolBar('Tool Bar 1', self) self.addToolBar(toolBar1) toolBar2 = QToolBar('Tool Bar 2', self) self.addToolBar(toolBar2) # 添加菜单和动作 fileMenu = menuBar.addMenu('File') fileMenu.addAction('New') fileMenu.addAction('Open') fileMenu.addAction('Save') editMenu = menuBar.addMenu('Edit') editMenu.addAction('Cut') editMenu.addAction('Copy') editMenu.addAction('Paste') # 监听菜单栏 hovered 信号 menuBar.hovered.connect(self.on_menu_bar_hovered) def on_menu_bar_hovered(self, action): # 根据鼠标所在的菜单项切换工具栏 if action.text() == 'File': self.toolBarArea(self.toolBarArea(self.toolBarWidgets()[0])) elif action.text() == 'Edit': self.toolBarArea(self.toolBarArea(self.toolBarWidgets()[1])) if __name__ == '__main__': app = QApplication([]) window = MyWindow() window.show() app.exec_() ``` 在这个例子中,我们创建了两个工具栏 `toolBar1` 和 `toolBar2`,并将它们添加到了主窗口中。然后,我们创建了一个菜单栏 `menuBar`,并添加了两个菜单 `File` 和 `Edit`,每个菜单中都包含了三个动作。最后,我们监听了菜单栏的 `hovered` 信号,并在回调函数 `on_menu_bar_hovered` 中根据鼠标所在的菜单项切换工具栏
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值