2011-11-01 16 views
6

QT programlamada yeniyim. İki menü ile basit bir menü oluşturmak ve birkaç eylem (FILE menüsü için 3 eylem ve VIEW menüsü için bir eylem) oluşturmak istiyorum. Bu menüleri ve eylemleri oluşturduğum createMenus() yöntemim var, ardından menubar için oluşturulan menüler ekliyorum, ancak Uygulamayı çalıştırdığımda, bu menü çubuğunu göstermiyor ve nedenini bilmiyorum. Sorunun ne olduğunu bilen var mı? MainWindow.cpp aitMenuBar issue QT 4.7.4 üzerinde MAC OS LION

Kaynak kod

#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); 
} 
+0

Kodunuzu Linux üzerinde Qt 4.5.2 ile denediniz ve iyi çalıştı. Yanlış bir şey göremiyorum. Hangi Qt sürümünü kullanıyorsunuz ve hangi platformdasınız? – Troubadour

+0

MAC OS LION –

+0

'da QT 4.7.4 kullanıyorum QMenuBar * menuBar = yeni QMenuBar (0); 'komutunu kullanıp menüden" setMenuBar (menuBar) "ile ana pencereye yüklüyor musunuz? Sadece QMainWindow için dokümanlar olarak bahsederim (http://doc.trolltech.com/4.7/qmainwindow.html#menuBar) bunun Mac üzerinde bir etkisi olması gerektiğini belirtmektedir. Bunun yapmak istediğin bir şey olmadığını biliyorum, sadece bir etkisi varsa merak ediyorum. – Troubadour

cevap