c++ - Qt Create with menubar and toolbar in a separate class file -


i create gui using qt creator, , have started normal qt application desktop project. prevent "mainwindow" class-files getting large, break out menubar , toolbar in separate class. new qt, , though have searched high , low on google, haven't been able find suitable example. working in qt 5.8.

what have is:

main.cpp:

#include "mainwindow.h" #include <qapplication>  int main(int argc, char *argv[]) {     qapplication a(argc, argv);     mainwindow w;     w.show();      return a.exec(); } 

mainwindow.h

#ifndef mainwindow_h #define mainwindow_h  #include "mwmenubar.h" #include <qmainwindow>  namespace ui { class mainwindow; }  class mainwindow : public qmainwindow {     q_object  public:     explicit mainwindow(qwidget *parent = 0);     ~mainwindow();  private:     ui::mainwindow *ui; };  #endif // mainwindow_h 

mainwindow.cpp

#include "mainwindow.h" #include "ui_mainwindow.h" #include "mwmenubar.h"  #include <qwidget> #include <qdebug>  mainwindow::mainwindow(qwidget *parent) :     qmainwindow(parent),     ui(new ui::mainwindow) {     // populate empty menubar & toolbar     mwmenubar *mwmenu = new mwmenubar(this); //    mwmenu->show();  // shows detached menubar (i.e. outside of main window).      ui->setupui(this); }  mainwindow::~mainwindow() {     delete ui; } 

mwmenubar.h

#ifndef mwmenubar_h #define mwmenubar_h  #include <qmainwindow> #include <qmenubar> #include <qtoolbar>  class qaction; class qmenu; class qtoolbar;  class mwmenubar : public qmainwindow {     q_object  public:     mwmenubar(qmainwindow *inmainwindow); // constructor     ~mwmenubar();                         // destructor  private:     qmainwindow  *mainwindow;  private slots:     // declare function prototypes     void afilenewcallback();     void afileopencallback();     void afileexitcallback();  private:     // declare function prototypes creating actions , menus     void createactions();     void createmenus();     void createtoolbar();      // define menubar elements     qmenu *mfile;      // define toolbar elements     qtoolbar *tfile;      // define menubar , toolbar actions     qaction *afilenew;     qaction *afileopen;     qaction *afileexit; };  #endif // mwmenubar_h 

mwmenubar.cpp

#include "mwmenubar.h" #include <qdebug>  mwmenubar::mwmenubar(qmainwindow *parent) {     mainwindow = parent;      createactions(); // create individual menu actions     createmenus();   // create main window menubar     createtoolbar(); // create main window toolbar }  mwmenubar::~mwmenubar() {     // destructor should not needed, menubar part of     // main window , deleted when main window closed. }  // create individual menu action callbacks void mwmenubar::afilenewcallback() {     qdebug() << "pressing file->new!"; }  void mwmenubar::afileopencallback() {     qdebug() << "pressing file->open!"; }  void mwmenubar::afileexitcallback() {     qdebug() << "pressing file->exit!";     mainwindow->close(); }  // function creating menu actions void mwmenubar::createactions() { //    const qicon newicon = qicon::fromtheme("document-new", qicon(":/images/new.png")); //    afilenew = new qaction(newicon, tr("&new..."), this);     afilenew = new qaction(tr("&new..."), this);     afilenew->setshortcuts(qkeysequence::new);     afilenew->setstatustip(tr("create new file"));     connect(afilenew, &qaction::triggered, this, &mwmenubar::afilenewcallback);  //    const qicon openicon = qicon::fromtheme("document-open", qicon(":/images/open.png")); //    afileopen = new qaction(openicon, tr("&open..."), this);     afileopen = new qaction(tr("&open..."), this);     afileopen->setshortcuts(qkeysequence::open);     afileopen->setstatustip(tr("open existing file"));     connect(afileopen, &qaction::triggered, this, &mwmenubar::afileopencallback);      afileexit = new qaction(tr("e&xit"), this);     afileexit->setshortcuts(qkeysequence::quit);     afileexit->setstatustip(tr("exit application"));     connect(afileexit, &qaction::triggered, this, &mwmenubar::afileexitcallback); }  void mwmenubar::createmenus() {     mfile = mainwindow->menubar()->addmenu(tr("&file"));     mfile->addaction(afilenew);     mfile->addaction(afileopen);     mfile->addseparator();     mfile->addaction(afileexit); }  void mwmenubar::createtoolbar() {     tfile = mainwindow->addtoolbar(tr("file"));     tfile->addaction(afilenew);     tfile->addaction(afileopen); } 

(for simplicity have commented out icons qactions ("new" , "open")).

the toobar shows fine, can't make menubar show up. have spent hours trying sorts of things in vain. code example appreciated. realize doing may wrong alltogether - if so, please show me do.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -