MAC OS LION 上的菜单栏问题 QT 4.7.4
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(由OP在问题编辑中回答。转换为社区维基答案。请参阅没有答案的问题,但问题在评论中得到解决(或在聊天中扩展))
OP 写道:
(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: