MAC OS LION 上的菜单栏问题 QT 4.7.4

发布于 2024-12-13 13:13:39 字数 2467 浏览 3 评论 0原文

我是 QT 编程新手。我想创建一个简单的菜单栏,其中包含两个菜单和多个操作(文件菜单的 3 个操作和视图菜单的一个操作)。我有 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);
}

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 ?

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-12-20 13:13:39

(由OP在问题编辑中回答。转换为社区维基答案。请参阅没有答案的问题,但问题在评论中得到解决(或在聊天中扩展)

OP 写道:

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

QMenuBar *menuBar = new QMenuBar(0);

然后添加菜单和操作。

(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) )

The OP wrote:

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

QMenuBar *menuBar = new QMenuBar(0);

Then add menus and actions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文