1

In the code below I create Qt Widget's Application, base class QMainWindow, and without .ui form. Cant understand why MenuBar doesn't show, tried different variants and no one works.

This image demonstrate what i got

.

System Ubuntu 16.04. Using QMake version 3.0 and Qt version 5.5.1

Note: on other machines the same code works correctly.

Below mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include <QWidget>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    QMenu *file;
};

#endif // MAINWINDOW_H

Below mainwindow.cpp, commented lines show how I tried to fix it.

#include "mainwindow.h"
#include <QtGui>
#include <QtWidgets>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //QVBoxLayout *vbl = new QVBoxLayout;
    QMenu *file = new QMenu("&File"); //menuBar()->addMenu("&File");//new QMenu("&File");
    file->addAction("&Quit",qApp,SLOT(quit()),Qt::CTRL+Qt::Key_Q);


    QMenuBar *mb = menuBar();

    mb->addMenu(file);
    mb->show();
    setMenuBar(mb);

    //vbl->setMenuBar(mb);
    //setLayout(vbl);

    resize(400,400);
}

MainWindow::~MainWindow()
{

}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I do not understand why you create the QVBoxLayout, but even with that the code works and I observe the menus, you must provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), that is, a code that reproduces your error, and as I see it is not since I used your code and it works obviously doing a small modification, change Widget to QWidget. – eyllanesc Nov 19 '17 at 18:53
  • the problem is that the same code works on other machines, only on my computer it show wrong result. – Vadym Voitsekhovskyi Nov 19 '17 at 19:25
  • Then reinstall Qt, and execute it back, very particular answers will be impossible to answer in SO. – eyllanesc Nov 19 '17 at 19:27
  • Done, allready reinstalled the newest version of Qt 5.9.2. Didn't help – Vadym Voitsekhovskyi Nov 19 '17 at 20:48

1 Answers1

2

After some investigations and reinstalling of all components I solved this simple problem. Need to change in 'System Settings -> Appearance -> Behavior' parameter for 'Show the menus for a window' from the "In the menu bar" to "In the window's title bar". Thanks to everyone who tried to help.

  • Where is `System Settings`? Do u mean `Preferences`? I'm on `Qt Creator 4.8.0 Based on Qt 5.12.0 (Clang 10.0 (Apple), 64 bit)` and can't find the command you mentioned. – CN_Cabbage Jul 08 '22 at 01:57
  • This is the answer for Ubuntu system, not for MacOS. I don't know how to fix such problem for your system. Maybe it has fully another reason. – Vadym Voitsekhovskyi Aug 02 '22 at 15:05