c++ - How to add a Menubar to a QWidget? -


i writing c++ application using qt framework, in 'main window' inherits qwidget class:

class draughts : public qwidget {     q_object public:     explicit draughts(qwidget *parent = 0);     ~draughts();  private:     ui::draughts *ui; }; 

and attempted add simple menu bar application, using following code:

draughts::draughts(qwidget *parent) :      qwidget(parent),     ui(new ui::draughts) {     ui->setupui(this);      qwidget *menuwidget = new qwidget;      qmenu *menugame = new qmenu("game");     menugame->addaction("new");     menugame->addaction("exit");      qmenu *menuhelp = new qmenu("help");     menuhelp->addaction("how play...");     menuhelp->addaction("about");      //setup application menu     qmenubar mainmenu(this);     mainmenu.addmenu(menugame);     mainmenu.addmenu(menuhelp); } 

should using qmainwindow class instead of qwidget class application?

it easier use qmainwindow, because provides convenient menubar() method:

qmenubar* mainmenu = this->menubar(); 

but possible add qwidget, other widget, don't allocate in local scope, because deleted after function finishes. instead, other widgets:

qmenubar mainmenu = new qmenubar(this); 

you should add layout widget, , add menu layout have more control on appear. may find this tutoria useful.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -