Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use sublassed QMenuBar as menuBar()
Qt 6.11 is out! See what's new in the release blog

Use sublassed QMenuBar as menuBar()

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 770 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    westernCiv
    wrote on last edited by
    #1

    Hey there
    I sublassed a QMenuBar:

    // mainmenu.cpp
    #include "mainmenu.h"
    
    Mainmenu::Mainmenu(QWidget *parent) : QMenuBar(parent){
    
        ...
    }
    
    //mainmenu.h
    #include <QWidget>
    #include <QMenuBar>
    
    class Mainmenu : public QMenuBar{
        
        Q_OBJECT
    
        public:
            
            Mainmenu(QWidget *parent = 0);
    };
    
    

    Then I try to use it in a QMainWindow:

    //mainwindow.h
    #include <QMainWindow>
    #include <QMenuBar>
    ...
    #include "mainmenu.h"
    
    class Mainwindow : public QMainWindow {
        
        Q_OBJECT  
    
        public:
        
            Mainwindow(QWidget *parent = 0);
            
        private slots:
            ...
        private:
            ...
            Mainmenu *menu;
    };
    
    
    //mainwindow.cpp
    
    #include "mainwindow.h"
    #include <QVBoxLayout>
    #include <QMenu>
    #include <QAction>
    
    Mainwindow::Mainwindow(QWidget *parent) : QMainWindow(parent){
        ...
        menu = new Mainmenu(this);
        menu = menuBar();
        ...
    }
    
    

    But I get this error when compiling:

    ../sources/mainwindow.cpp: In constructor ‘Mainwindow::Mainwindow(QWidget*)’:
    ../sources/mainwindow.cpp:16:19: error: invalid conversion from ‘QMenuBar*’ to ‘Mainmenu*’ [-fpermissive]
         menu = menuBar();
                ~~~~~~~^~
    make: *** [Makefile:246: object/mainwindow.o] Error 1
    
    

    And I'm not sure what I'm doing wrong
    I want to subclass the menu because it is large (lots of actions/submenus etc),
    and I want to use menuBar() because it is layout-independent

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      menuBar() is the getter, use setMenuBar as setter

      This is a standard pattern in Qt (and C++ in general) methods where you have a method that gets the value and another that sets it

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved