gpt4 book ai didi

qt4 - 菜单栏在 MAC OS LION 上发布 QT 4.7.4

转载 作者:行者123 更新时间:2023-12-04 16:12:43 26 4
gpt4 key购买 nike

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

最佳答案

(由 OP 在问题编辑中回答。转换为社区 wiki 答案。见 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.

关于qt4 - 菜单栏在 MAC OS LION 上发布 QT 4.7.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7972721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com