MenuBar在MAC OS LION上发布QT 4.7.4

时间:2011-11-01 20:50:35

标签: qt4 qt-creator menubar

我是QT编程的新手。我想创建一个简单的菜单栏,其中包含两个菜单和几个操作(FILE菜单有3个操作,VIEW菜单有一个操作)。我有createMenus()方法,我创建那些菜单和操作然后我将创建的菜单添加到菜单栏,但是当我运行应用程序时,它没有显示这个菜单栏,我不知道为什么。谁能说出问题是什么?

MainWindow.cpp的源代码

#include <QtGui>
#include <QAction>
#include "MainWindow.h"

MainWindow::MainWindow() {

        // Creeam fereastra principala
    QWidget *window = new QWidget;
    setCentralWidget(window);

    // Creeam eticheta unde vom afisa titlul item-ului selectat
    // Implicit va avea un titlu predefinit
   infoLabel = new QLabel("Selectati un item va rog ");

    createActions();
    createMenus();

    // Creeam un layout pentru pozitionarea etichetei
    QHBoxLayout *layout = new QHBoxLayout;

    layout->addWidget(infoLabel);
    window->setLayout(layout);

    setWindowTitle("GUI");
    setMinimumSize(300, 300);
    resize(480,320);
}

void MainWindow::contextMenuEvent(QContextMenuEvent *event) {

    QMenu menu(this);
    menu.addAction(newAction);
    menu.addAction(openAction);
    menu.addAction(closeAction);
    menu.addAction(preferencesAction);
    menu.exec(event->globalPos());
}

void MainWindow::new_() {

    infoLabel->setText("A fost selectat : NEW");
}

void MainWindow::open() {

     infoLabel->setText("A fost selectat : OPEN");
}

void MainWindow::close() {

}

void MainWindow::preferences() {

     infoLabel->setText("A fost selectat : PREFERENCES");
}


void MainWindow::createActions()
{
    newAction = new QAction("New", this);
    connect(newAction, SIGNAL(triggered()), this, SLOT(new_()));

    openAction = new QAction("Open", this);
    connect(openAction, SIGNAL(triggered()), this, SLOT(open()));

    closeAction = new QAction("Close", this);
    connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));

    preferencesAction = new QAction("Preferences", this);
    connect(preferencesAction, SIGNAL(triggered()), this, SLOT(preferences()));
}

void MainWindow::createMenus()
{
    // Creeaza sectiunea File
    fileMenu = new QMenu ("File");

    // Adauga actiunile new,open si close la sectiunea File
    fileMenu->addAction(newAction);
    fileMenu->addAction(openAction);
    fileMenu->addAction(closeAction);


    //  Creeaza sectiunea View
     viewMenu = new QMenu ("View");

    //Adauga actiunea preferences la sectiunea View
    viewMenu->addAction(preferencesAction);

    menuBar()->addMenu(fileMenu);
    menuBar()->addMenu(viewMenu);
}

1 个答案:

答案 0 :(得分:0)

(在问题编辑中由OP回答。转换为社区维基答案。请参阅Question with no answers, but issue solved in the comments (or extended in chat)

OP写道:

  

已解决:必须以这种方式创建无父菜单栏:

QMenuBar *menuBar = new QMenuBar(0);
  

然后添加菜单和操作。