पर मेनूबार मुद्दा QT 4.7.4 QT प्रोग्रामिंग के लिए नया हूं। मैं दो मेन्यू के साथ एक साधारण मेनूबार बनाना चाहता हूं, और कई क्रियाएं (फ़ाइल मेनू के लिए 3 क्रियाएं और दृश्य मेनू के लिए एक क्रिया)। मैंने मेनेस() विधि बनाई है जहां मैं उन मेनू और क्रियाएं बनाता हूं, फिर मैं मेन्यूबार में बनाए गए मेनू जोड़ता हूं, लेकिन जब मैं ऐप चलाता हूं, यह यह मेनूबार नहीं दिखा रहा है, और मुझे नहीं पता क्यों। क्या कोई बता सकता है कि समस्या क्या है? MainWindow.cpp कीमैक ओएस LION
स्रोत कोड
#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);
}
लिनक्स पर क्यूटी 4.5.2 के साथ बस अपना कोड आजमाया और यह ठीक काम किया। मैं इसके साथ कुछ भी गलत नहीं देख सकता। क्यूटी का कौन सा संस्करण आप उपयोग कर रहे हैं और आप किस मंच पर हैं? – Troubadour
मै मैक ओएस LION –
पर क्यूटी 4.7.4 का उपयोग कर रहा हूं यदि आप 'QMenuBar * menuBar = new QMenuBar (0) करते हैं, तो आप मेनू प्राप्त करते हैं; और फिर इसे' setMenuBar (menuBar) 'के साथ मुख्य विंडो पर इंस्टॉल करें? मैं केवल इसे [QMainWindow के लिए दस्तावेज़] के रूप में उल्लेख करता हूं (http://doc.trolltech.com/4.7/qmainwindow.html#menuBar) इसका उल्लेख मैक पर प्रभाव होना चाहिए। मुझे पता है कि आप निश्चित रूप से ऐसा नहीं करना चाहते हैं, अगर इसका कोई प्रभाव हो तो उत्सुक हो। – Troubadour