charu
charu

Reputation: 65

Error while adding menubar in qt

I was trying to add the menubar in QDialog but got the following error:

error: 'class Qt_Stackwidget' has no member named 'menuBar'
         QMenuBar* menubar = this->menuBar();

I was confused why it is so. I searched and got that QDialog has no function function named menuBar(). How can I add menubar to QDialog. Can anyone please help me to solve this?

Upvotes: 2

Views: 244

Answers (1)

svlasov
svlasov

Reputation: 10456

You can add a menubar like this:

#include <QtGui>

class Dialog : public QDialog
{
public:
    Dialog(QWidget *parent = 0) : QDialog(parent)
    {
        QVBoxLayout *layout = new QVBoxLayout;
        setLayout(layout);

        QMenu *menu = new QMenu("File");
        menu->addAction("Exit");

        QMenuBar *menubar = new QMenuBar();
        menubar->addMenu(menu);

        layout->setMenuBar(menubar);
    }
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QPushButton *button = new QPushButton("Button");

    Dialog *dialog = new Dialog;
    dialog->layout()->addWidget(button);
    dialog->show();

    return app.exec();
}

enter image description here

Upvotes: 2

Related Questions