Qt MainWindow doesn't show MenuBar [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
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()
{
}

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.

Related

How does one split a QT(C++) UI into multiple widget subclasses and get it to display the same as all in the same class?

I'm learning QT (I already know C++ very well) and trying to get a UI to display well but I do not want to use the QT Designer that comes with QT Creator. I have the following class:
#include "MainPanel.h"
#include<QVBoxLayout>
MainPanel::MainPanel(QWidget *parent)
: QWidget(parent)
{
QLayout *lo = new QVBoxLayout(this);
mList = new QListWidget(this);
mList->addItem("Testing");
setLayout(lo);
lo->addWidget(mList);
lo->setSpacing(5);
}
The Main window class has a bunch of extra protected functions but the initControls method just does this:
void MainWindow::initControls()
{
QHBoxLayout *loMain = new QHBoxLayout(this);
loMain->addWidget(new MainPanel(this));
setLayout(loMain);
}
When I put all the code from MainPanel into MainWindow::initControls() or even in the constructor(either way), it works - shows a list widget with a single item "Testing". With the code in MainPanel though, it shows up as a very small rectangle that wouldn't fit the word "Testing" nor is there any semblance of text in there even partially.
I have tried to override sizeHint() in and move the code to create and return the list widget to a method getList() so I can access it from sizeHint too but that did nothing - I still get a small rectangle.
What am I doing wrong and what do I need to do or include to get the widget to paint properly? I have more controls I want to add to this UI (a button panel below the list widget and a detail panel on the right 2/3 of the window) but until I can get this to display, I can't possibly proceed with the rest.
I also want to do this entirely with code - not using the designer as I have vision issues and found it to be difficult to place things correctly on the form.
Someone please help - documentation and tutorials other than the documentation on QT's website is helpful if it points me to the right direction. I have already looked on QT's docs site under QWidget, QListWidget, QLayout, and QH(and V)BoxLayouts but see nothing and many of the tutorials talk about the designer.
Before someone tries to scold about creating a SSME or whatever small program - I have given you the smallest one that displays the issue - I know that putting the code all into the main window fixes it but one should never have everything in one class.
Okay, too unclear, what's happening there, we should have requested for more of your code =)
Here is the smallest possible sample, demonstrating what you should have been trying to achieve:
mainwindow.cpp:
#include "mainwindow.h"
#include "mainpanel.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
MainPanel* p = new MainPanel(this);
setCentralWidget(p);
}
mainpanel.h:
#include "mainpanel.h"
#include <QListWidget>
#include <QLayout>
MainPanel::MainPanel(QWidget *parent)
: QWidget(parent)
{
QLayout *lo = new QVBoxLayout(this);
QListWidget* mList = new QListWidget(this);
mList->addItem("Testing");
setLayout(lo);
lo->addWidget(mList);
lo->setSpacing(5);
}
main.cpp:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.resize(400, 500);
w.show();
return app.exec();
}
Seemingly, you've tried to set a layout on QMainWindow, but it already has a built-in layout, it is exactly the case when setCentralWidget should work, leave manual layout creation for QWidget & QDialog subclasses.
The code above works fine, try it and refactor the way you want.
#MasterAler is correct that the root cause of your issue is setting a layout MainWindow. The reason for using a MainWindow is to support standard VBoxLayout of menubar, central widget, and status bar. So it makes no sense to set your own layout for a QMainWindow.
Since we didn't have your entire code, I didn't assume MainWindow was a QMainWindow. I got your code to work by making MainWindow a QDialog. This may be more of what you were originally looking for, where you want to put a widget of your own making into any container. Following is in Python (I find Python a faster prototyping environment than C++), but you can easily read it and see how to host your MainPanel is any widget (not just a MainWindow):
import sys
from PySide2.QtWidgets import QApplication, QWidget, QListWidget, QDialog, QVBoxLayout, QHBoxLayout
class MainPanel(QWidget):
def __init__(self, parent):
QWidget.__init__(self, parent)
lo = QVBoxLayout(self)
lo.setSpacing(5)
self.setLayout(lo)
mList = QListWidget(self)
mList.addItem("Testing")
lo.addWidget(mList)
class MainWindow(QDialog):
def __init__(self, parent):
QDialog.__init__(self, parent)
loMain = QHBoxLayout(self)
loMain.addWidget(MainPanel(self))
self.setLayout(loMain)
if __name__ == "__main__":
app = QApplication(sys.argv)
# Show the form
window = MainWindow(None)
window.exec_()

Qt symbol(s) not found for architecture x86_64

First of all I am new to Qt, so if there may be something painstakingly obvious to some more advanced users, I also know this error crops up a lot and I've done a lot of reading on Qt forums and have still not managed to solve my problem.
I don't however understand why I am even getting the symbols not found for architecture x86_64 error as I had been working on my application all day without any problems.
I am building an image editor in the likes of Photoshop for an assignment in my Cross Platform SW Development course at Uni and wanted to separate the filter logic I was writing from the rest of my code, I therefore created a Filters class.
In my mainwindow.h I created a private reference to the class Filters *filter; which I instantiated in mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// Instanciate a scene object that we can draw to and then set up the ui
scene = new QGraphicsScene(this);
filter = new Filters(this);
ui->setupUi(this);
QObject::connect(ui->cmdBlack_White, SIGNAL(triggered()), filter, SLOT(serialFilterBlackWhite(image)));
}
As you can see, I have created a connection to the serialFilterBlackWhite method, which resides in filters.cpp at the moment and doesn't do much due to the x86 error:
QPixmap Filters::serialFilterBlackWhite(QPixmap image)
{
image.fill( Qt::red );
return image;
}
The .h file for the class looks like this :
#ifndef FILTERS_H
#define FILTERS_H
#include <QObject>
#include <QPixmap>
class Filters : public QObject
{
Q_OBJECT
public:
explicit Filters(QWidget *parent = 0);
signals:
public slots:
QPixmap serialFilterBlackWhite(QPixmap);
};
#endif // FILTERS_H
I have tried not too bloat my question with too much code, but if you need more please let me know. I really have no idea whats going on with this.

Qt Creator no member named stackedWidget

I've just designed my ui in the QT-Creator and, as the main application is based on two panels, I decided to use the StackedWidget for "change the layout" without opening a new window.
So, I added a QTStackedWidget named: stackedWidget (as default).
The problem is in mainwindow.cpp, I added a custom SLOT that contain:
ui->stackedWidget->setCurrentIndex(1);
when I build this the compiler says:
mainwindow.cpp:25: error: no member named 'stackedWidget' in 'Ui::MainWindow'
ui->stackedWidget->setCurrentIndex(1);
~~ ^
also in the qt-creator itself I was unable to attach a signal to the stackedWidget because it doesn't show to me the setCurrentIndex SLOT...
any advice?
Please note that I'm a noob with C++ I just used Qt a couple of years ago with PyQt4.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void showOtherPage();
void showMainPage();
};
#endif // MAINWINDOW_H
mainiwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qDebug() << "MainWindow - Debug Mode ON";
connect(ui->btnOther, SIGNAL(clicked()), SLOT(showOtherPage()));
}
void MainWindow::showOtherPage()
{
qDebug() << "Showing Other Page";
ui->stackedWidget->setCurrentIndex(1);
}
void MainWindow::showMainPage()
{
qDebug() << "Showing Main Page";
ui->stackedWidget->setCurrentIndex(0);
}
MainWindow::~MainWindow()
{
delete ui;
}
I had a very similar issue.
One of the UI actions was defined as the others and used.
I had a ui has no member named xyz compilation error for this one only, without possible explanation.
I could solve it by unchecking/checking the Shadow build option in the project compilation options!
Hope it saves someone the 30 minutes I just lost :)
Faced the very same issue today. Recreating the project does work, though I've found a solution that won't make one rewrite everything :) Run it in Debug. Once you've done it, problem's solved.
Creating a new project fixed it... I don't know why, I'm still looking for an explanation.
I started a new project, created the stackedWidget, and tested the code for the page-switching... it just simply works... and I still do not know WHY the other one fail to build...
Checked again and again names and everything.
I tried cleaning project and running qmake without any luck. I then drilled down into the project folder and deleted the ui_***.h file and everything started working. Might have had to do with the fact Visual Studio generated that file and I was working on another computer with Qt Creator for the same project.

QGLWidget crashes when added to layout

I am trying to adapt the opengl Es example "Hello GL" featured here - http://qt-project.org/doc/qt-4.8/opengl-hellogl-es.html. I am basically looking for a simple way to get a 3D graphics rendering window into a form made in Qt creator.
The first thing I tried:
Grid layout is a layout I created in Qt Creator.
#include <QProcess>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QTimer>
#include "glwidget.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
GLWidget *glwidget = new GLWidget(); // This is mandatory. No problems here.
QTimer *timer = new QTimer(this); // Need this for the example to work.
timer->setInterval(10); // Also necessary.
ui->gridLayout->addWidget(glwidget);
Which compiles, but then promptly crashes with a segmentation fault.
ui->gridLayout->addWidget(new GLWidget);
Segfaults the same way.
The debugger points me toward line 104 of qgridlayout.h:
inline void addWidget(QWidget *w) { QLayout::addWidget(w); }
Not sure what to make of that. Perhaps the QGLWidget wants to do something before I call ui->setupUi(this)? Perhaps it can't add the widget to the layout for some reason?
Of course if I comment out the line where I am added the widget, the program works flawlessly.
Any ideas for what's going on here?
Edit: I have fixed this. It was problem with order of operations - I called updateui too quickly.
The setupUi function in the generated form class initializes all variables of the form class, so using the variables before the form is initialized is undefined behaviour, since the variables contain garbage.
So the solution is to call:
ui->setupUi(this)
Before any call that uses variables in the ui object.
I'm not sure, but I think the problem is with the following line:
ui->gridLayout->addWidget(GLWidget);
I think, you should write it as follows:
ui->gridLayout->addWidget(glwidget);
// declare glwidget as member of your class
GLWidget *glwidget;
//in constructor use
glwidget = new GLWidget();

Missing some functions in my Qt class [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm starting using Qt for a GUI, but I have some problems with headers/libraries because I'm missing some functions.
Two of them are:
<obj_name>.setModal(true);
<obj_name>.exec();
They should work fine as in the video I'm following (at 6:30).
Because I did exactly what they did, so my clue is his version isn't the same as mine.
I want to know which header should I include.
Here's my code:
void MainWindow::on_actionNew_Window_triggered()
{
MyDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
}
Even with:
#include <QDialog>
Still doesn't work. It says:
C:\QtSDK\teste-build-desktop-Qt_4_8_1_for_Desktop_-MinGW_Qt_SDK__Debug..\teste\mainwindow.cpp:22: error: 'class MyDialog' has no member named 'setModal'.
mydialog.h code:
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QMainWindow>
#include <QDialog>
namespace Ui {
class MyDialog;
}
class MyDialog : public QMainWindow
{
Q_OBJECT
public:
explicit MyDialog(QWidget *parent = 0);
~MyDialog();
private:
Ui::MyDialog *ui;
};
#endif // MYDIALOG_H
It is included in mainwindow.cpp and mydialog.cpp (the header is just the class).
MyDialog is no QDialog. You created it as a "main window" which is no dialog.
To hot-fix this (without recreating the dialog using QtCreator), just rewrite the inheritance in mydialog.h
from:
class MyDialog : public QMainWindow
to:
class MyDialog : public QDialog
In your mydialog.cpp you find the implementation of the constructor of MyDialog which calls the superclass constructor. Since we just changed the superclass, we also have to change this call from:
MyDialog::MyDialog(QWidget *parent) :
QMainWindow(parent)
...
to
MyDialog::MyDialog(QWidget *parent) :
QDialog(parent)
...
You also have to fix your .ui file to morph the whole widget from a main window to a dialog. I'll add how to do so in a few minutes (have to find it out) You don't need to touch the .ui file.
You're trying to call setModal() from MyDialog class, but it inherits from QMainWindow, which has no setModal method. You must inherit from QDialog instead.