MenuBar在MAC OS LION上发布QT 4.7.4 [英] MenuBar issue QT 4.7.4 on MAC OS LION

查看:86
本文介绍了MenuBar在MAC OS LION上发布QT 4.7.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to QT programming. I want to create a simple menubar with two menus , and several actions (3 actions for FILE menu and one action for VIEW menu).I have createMenus() method where i create those menus and actions then I add created menus to menubar ,but when i run the app , it's not showing this menubar , and i dont know why. Can anyone tell what the problem is ?

MainWindow.cpp的源代码

Source code of 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);
}

推荐答案

(由OP在问题编辑中回答.转换为社区Wiki答案.请参见

(Answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

OP写道:

已解决:必须通过以下方式创建无父菜单栏:

SOLVED : Must create a parent-less menu bar this way:

QMenuBar *menuBar = new QMenuBar(0);

然后添加菜单和操作.

Then add menus and actions.

这篇关于MenuBar在MAC OS LION上发布QT 4.7.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆