Qt在单独的类文件中使用菜单栏和工具栏创建

时间:2017-08-14 16:58:18

标签: c++ class qt-creator toolbar menubar

我想使用Qt Creator创建一个GUI,并启动了一个普通的Qt Application Desktop项目。为了防止“MainWindow”类文件变得太大,我想在一个单独的类中打破菜单栏和工具栏。我是Qt的新手,虽然我在Google上搜索过高,但我找不到合适的例子。我在Qt 5.8工作。

我拥有的是:

main.cpp中:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "mwmenubar.h"
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mwmenubar.h"

#include <QWidget>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    // Now populate empty menubar & toolbar
    MWMenubar *MWmenu = new MWMenubar(this);
//    MWmenu->show();  // Shows a detached menubar (i.e. outside of the main window).

    ui->setupUi(this);
}

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

mwmenubar.h

#ifndef MWMENUBAR_H
#define MWMENUBAR_H

#include <QMainWindow>
#include <QMenuBar>
#include <QToolBar>

class QAction;
class QMenu;
class QToolBar;

class MWMenubar : public QMainWindow
{
    Q_OBJECT

public:
    MWMenubar(QMainWindow *inMainWindow); // Constructor
    ~MWMenubar();                         // Destructor

private:
    QMainWindow  *mainWindow;

private slots:
    // Declare function prototypes
    void aFileNewCallback();
    void aFileOpenCallback();
    void aFileExitCallback();

private:
    // Declare function prototypes for creating actions and menus
    void createActions();
    void createMenus();
    void createToolbar();

    // Define menubar elements
    QMenu *mFile;

    // Define toolbar elements
    QToolBar *tFile;

    // Define menubar and toolbar actions
    QAction *aFileNew;
    QAction *aFileOpen;
    QAction *aFileExit;
};

#endif // MWMENUBAR_H

mwmenubar.cpp

#include "mwmenubar.h"
#include <QDebug>

MWMenubar::MWMenubar(QMainWindow *parent)
{
    mainWindow = parent;

    createActions(); // Create all individual menu actions
    createMenus();   // Create main window menubar
    createToolbar(); // Create main window toolbar
}

MWMenubar::~MWMenubar()
{
    // The destructor should not be needed, as the menubar is part of the
    // main window and will be deleted when the main window is closed.
}

// Create individual menu action callbacks
void MWMenubar::aFileNewCallback()
{
    qDebug() << "Pressing File->New!";
}

void MWMenubar::aFileOpenCallback()
{
    qDebug() << "Pressing File->Open!";
}

void MWMenubar::aFileExitCallback()
{
    qDebug() << "Pressing File->Exit!";
    mainWindow->close();
}

// Function for creating all menu actions
void MWMenubar::createActions()
{
//    const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(":/images/new.png"));
//    aFileNew = new QAction(newIcon, tr("&New..."), this);
    aFileNew = new QAction(tr("&New..."), this);
    aFileNew->setShortcuts(QKeySequence::New);
    aFileNew->setStatusTip(tr("Create a new file"));
    connect(aFileNew, &QAction::triggered, this, &MWMenubar::aFileNewCallback);

//    const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/images/open.png"));
//    aFileOpen = new QAction(openIcon, tr("&Open..."), this);
    aFileOpen = new QAction(tr("&Open..."), this);
    aFileOpen->setShortcuts(QKeySequence::Open);
    aFileOpen->setStatusTip(tr("Open an existing file"));
    connect(aFileOpen, &QAction::triggered, this, &MWMenubar::aFileOpenCallback);

    aFileExit = new QAction(tr("E&xit"), this);
    aFileExit->setShortcuts(QKeySequence::Quit);
    aFileExit->setStatusTip(tr("Exit the application"));
    connect(aFileExit, &QAction::triggered, this, &MWMenubar::aFileExitCallback);
}

void MWMenubar::createMenus()
{
    mFile = mainWindow->menuBar()->addMenu(tr("&File"));
    mFile->addAction(aFileNew);
    mFile->addAction(aFileOpen);
    mFile->addSeparator();
    mFile->addAction(aFileExit);
}

void MWMenubar::createToolbar()
{
    tFile = mainWindow->addToolBar(tr("File"));
    tFile->addAction(aFileNew);
    tFile->addAction(aFileOpen);
}

(为简单起见,我已经注释了QActions的图标(“新”和“打开”))。

toobar很好,但我不能让菜单栏出现。我花了好几个小时尝试各种各样的东西但是徒劳无功。因此,非常感谢代码示例。我意识到我所做的一切都可能是错的 - 如果是这样,请告诉我该怎么做。

0 个答案:

没有答案