2011-11-01 36 views
6

Tôi mới tham gia lập trình QT. Tôi muốn tạo một thanh menu đơn giản với hai menu, và một vài hành động (3 hành động cho menu FILE và một hành động cho menu VIEW) .Tôi có phương thức createMenus() nơi tôi tạo các menu và hành động sau đó tôi thêm các menu đã tạo vào thanh thực đơn, nhưng khi tôi chạy ứng dụng, nó không hiển thị thanh thực đơn này, và tôi không biết tại sao. Bất cứ ai có thể cho biết vấn đề là gì?MenuBar phát hành QT 4.7.4 trên MAC OS LION

Source code của 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); 
} 
+0

Chỉ cần thử mã của bạn với Qt 4.5.2 trên Linux và nó hoạt động tốt. Tôi không thể nhìn thấy bất cứ điều gì sai trái với nó. Bạn đang sử dụng phiên bản Qt nào và bạn đang sử dụng nền tảng nào? – Troubadour

+0

Tôi đang sử dụng QT 4.7.4 trên MAC OS LION –

+0

Bạn có nhận được một menu nếu bạn thực hiện 'QMenuBar * menuBar = new QMenuBar (0);' và sau đó cài đặt nó trên cửa sổ chính với 'setMenuBar (menuBar)'? Tôi chỉ đề cập đến nó như là [tài liệu cho QMainWindow] (http://doc.trolltech.com/4.7/qmainwindow.html#menuBar) đề cập đến điều này sẽ có tác dụng trên Mac. Tôi biết đó không phải là những gì bạn muốn làm tất nhiên, chỉ tò mò nếu nó có hiệu lực. – Troubadour

Trả lời

Các vấn đề liên quan