问题描述
我是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写道:
QMenuBar *menuBar = new QMenuBar(0);
这篇关于MenuBar在MAC OS LION上发布QT 4.7.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!