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. error C2027: use of undefined type 'QMenuBar'

error C2027: use of undefined type 'QMenuBar'

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.7k 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.
  • C Offline
    C Offline
    CroCo
    wrote on last edited by CroCo
    #1

    I'm reading a book in which there is a code as follows:

    void MainWindow::createMenus()
    {
    	fileMenu = menuBar()->addMenu(tr("&File"));
    	fileMenu->addAction(newAction);
    	fileMenu->addAction(openAction);
    ...
    

    fileMenu is defined as QMenu *fileMenu;. I'm getting the following error

    mainwindow.cpp
    .\mainwindow.cpp(196): error C2027: use of undefined type 'QMenuBar'
    c:\qt\5.9.1\msvc2015\include\qtwidgets\qmainwindow.h(54): note: see declaration of 'QMenuBar'
    

    I've taken a look at menuBar() in the QMainWindow class which is defined as QMenuBar * menuBar() const. Does that mean the code is old? Is there any change has been made for menuBar() from Qt4 to Qt5? Do I need to change the code to make it run? I'm Using Qt 5.9 with Visual Studio 2014. The author uses Qt 4.

    aha_1980A 1 Reply Last reply
    0
    • C CroCo

      I'm reading a book in which there is a code as follows:

      void MainWindow::createMenus()
      {
      	fileMenu = menuBar()->addMenu(tr("&File"));
      	fileMenu->addAction(newAction);
      	fileMenu->addAction(openAction);
      ...
      

      fileMenu is defined as QMenu *fileMenu;. I'm getting the following error

      mainwindow.cpp
      .\mainwindow.cpp(196): error C2027: use of undefined type 'QMenuBar'
      c:\qt\5.9.1\msvc2015\include\qtwidgets\qmainwindow.h(54): note: see declaration of 'QMenuBar'
      

      I've taken a look at menuBar() in the QMainWindow class which is defined as QMenuBar * menuBar() const. Does that mean the code is old? Is there any change has been made for menuBar() from Qt4 to Qt5? Do I need to change the code to make it run? I'm Using Qt 5.9 with Visual Studio 2014. The author uses Qt 4.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @CroCo,

      http://doc.qt.io/qt-5/qmenubar.html :

      Header: #include <QMenuBar>

      have you done this?

      Regards

      Qt has to stay free or it will die.

      C 1 Reply Last reply
      1
      • aha_1980A aha_1980

        Hi @CroCo,

        http://doc.qt.io/qt-5/qmenubar.html :

        Header: #include <QMenuBar>

        have you done this?

        Regards

        C Offline
        C Offline
        CroCo
        wrote on last edited by
        #3

        @aha_1980 said in error C2027: use of undefined type 'QMenuBar':

        #include <QMenuBar>

        that solved the problem. Thanks. How come a QMenu object is treated as a QMenuBar object?

        1 Reply Last reply
        0
        • aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @CroCo

          I guess because here you use QMenuBar:

          fileMenu = menuBar()->addMenu(tr("&File"));

          Qt has to stay free or it will die.

          C 1 Reply Last reply
          0
          • aha_1980A aha_1980

            @CroCo

            I guess because here you use QMenuBar:

            fileMenu = menuBar()->addMenu(tr("&File"));

            C Offline
            C Offline
            CroCo
            wrote on last edited by
            #5

            @aha_1980 No fileMenu is QMenu pointer whereas the static function menuBar() returns a const QMenuBar pointer. I didn't do any casting.

            aha_1980A 1 Reply Last reply
            0
            • C CroCo

              @aha_1980 No fileMenu is QMenu pointer whereas the static function menuBar() returns a const QMenuBar pointer. I didn't do any casting.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @CroCo said in error C2027: use of undefined type 'QMenuBar':

              whereas the static function menuBar() returns a const QMenuBar pointer.

              That's what I said.

              Qt has to stay free or it will die.

              C 1 Reply Last reply
              0
              • aha_1980A aha_1980

                @CroCo said in error C2027: use of undefined type 'QMenuBar':

                whereas the static function menuBar() returns a const QMenuBar pointer.

                That's what I said.

                C Offline
                C Offline
                CroCo
                wrote on last edited by
                #7

                @aha_1980 I'm sorry I didn't get it. Could you please elaborate a bit.

                aha_1980A 1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  Hi
                  it works because you assign the return of menuBar()->addMenu function call.
                  QMenu *addMenu(const QString &title);

                  1 Reply Last reply
                  0
                  • C CroCo

                    @aha_1980 I'm sorry I didn't get it. Could you please elaborate a bit.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @CroCo

                    You already answered you question yourself. You have a QMenuBar *menuBar pointer that you want to de-reference. The compiler cannot do it if the declaration of QMenuBar is unknown.

                    Therefore you have to add the #include <QMenuBar> before de-referencing. Ok?

                    Qt has to stay free or it will die.

                    C 1 Reply Last reply
                    0
                    • aha_1980A aha_1980

                      @CroCo

                      You already answered you question yourself. You have a QMenuBar *menuBar pointer that you want to de-reference. The compiler cannot do it if the declaration of QMenuBar is unknown.

                      Therefore you have to add the #include <QMenuBar> before de-referencing. Ok?

                      C Offline
                      C Offline
                      CroCo
                      wrote on last edited by
                      #10

                      @aha_1980 thank you. it is clear.

                      1 Reply Last reply
                      1

                      • Login

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