0

I have created a QWidget (i.e something that acts as a window). However I am unable to set a Menu Bar to the QWidget using QMenu (NOT QMenuBar). I am not using Qt Designer. I am making the program in C++ and would like help with how I can get a menu bar to be displayed in the top - left of a window. Here is the code -

#include <QApplication>
#include <QAction>
#include <QMenu>

int main(int argc, char** argv){
    QApplication qapp_base = QApplication(argc, argv);
    QWidget qwin_main = QWidget();
    qwin_main.setFixedSize(1280, 720);
    QMenu qmen_main = QMenu(&qwin_main);
    qwin_main.show();
    qmen_main.show();
    return qapp_base.exec();
}

scopchanov
  • 7,966
  • 10
  • 40
  • 68
Arnav Deo
  • 33
  • 1
  • 3
  • The code compiles perfectly. I just can't seem to get a menu bar displayed. You should provide the error output if you claim that the code doesn't compile. – Arnav Deo Feb 19 '21 at 07:35
  • https://pastebin.com/zqUMnkrn – Minh Feb 19 '21 at 07:40
  • 1
    I am using Qt 6.0.1 – Arnav Deo Feb 19 '21 at 07:42
  • I think you should read [this](https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html) first. – scopchanov Feb 19 '21 at 07:52
  • @scopchanov the link points to a resource that uses QMainWindow. I am trying to avoid using the same.I currently have it working by using QMenuBar and Adding QMenus to it. – Arnav Deo Feb 19 '21 at 08:22
  • 1
    I'm not entirely sure I understand what you're trying to do but I'm not aware of any way to associate a `QMenuBar` with a widget other than by placing them both in a layout. Is there any particular reason you don't want to use a `QMainWindow`? – G.M. Feb 19 '21 at 08:32
  • The link shows how to populate a QMenu and toggle it on and off from QMenuBar. It is up to you to decide what would you like to do with the QMenuBar. – scopchanov Feb 19 '21 at 08:33
  • Alternatively [QWidget::contextMenuEvent](https://doc.qt.io/qt-5/qwidget.html#contextMenuEvent) can be used. It will trigger on right click. – Kao Feb 19 '21 at 10:02
  • Only QMainWindow can have top menu (QMenuBar), QWidget can not have one (this is the main difference between them) – mugiseyebrows Feb 19 '21 at 12:16
  • @mugiseyebrows thank you for the answer. Reply to G.M. - It is a school assignment where we have to develop an app over a long time and improve it as we learn more about GUIs. We were told to begin with minimal component, hence I am avoiding QMainWindow as I dont know whether it will be considered minimal by the school. – Arnav Deo Feb 19 '21 at 12:24
  • 1
    QMainWindow is considered the base Qt widget for all main applications. If I were your professor, I'd absolutely allow it. But you can always fire off an email if you're not sure. – Joseph Larson Feb 19 '21 at 13:08
  • @JosephLarson _`QMainWindow` is considered the base Qt widget for all main applications_ I presume you mean _for all main windows_. Even so, it is not true, as `QWidget` is the base one. `QMainWindow` is a part of the `Qt Main Window Framework` designed to be used __specifically__ for natively looking desktop applications. – scopchanov Feb 22 '21 at 11:54

0 Answers0