QT 5.15: Creating a QPushButton crashes a program - c++

Using latest QT 5.15.0, c++20, cmake 3.17, MSVC 16.7.2
I have a simple QWidget: (all necessary includes defined in precompiled header)
widget.h
#pragma once
#ifndef _WIDGET_H_209323
#define _WIDGET_H_209323
class TestWidget: public QWidget {
Q_OBJECT
public:
explicit TestWidget(QWidget* parent = nullptr);
private:
QVBoxLayout* side_panel_;
private:
auto init_elements() -> void;
};
#endif
widget.cpp:
#include "widget.h"
TestWidget::TestWidget(QWidget* parent) : QWidget(parent) {
init_elements();
}
auto TestWidget::init_elements() -> void {
auto init_main_panel = [this]() {
side_panel_ = new QVBoxLayout();
QPushButton* main_view = new QPushButton("test");
side_panel_->addWidget(main_view);
};
init_main_panel();
this->setLayout(side_panel_);
}
The program compiles fine, but crashes at startup with Process finished with exit code -1073741511 (0xC0000139) and nothing else.
Changing QPushButton to any other widget, for example
QLabel* label = new QLabel(this);
label->setText("test");
side_panel_->addWidget(label);
works as expected, without any problems.
Actually just adding widget.h with QPushButton to CMakeLists.txt add_target, without including it in any other project files crashes the program at startup.
UPDATE:
Tried loading project into Visual Studio and got more meaningful error:
The procedure entry point
?hitButton#QPushButton##MEBA_NAEBVQPoint###Z
could not be located int the dynamic link library
UPDATE 2:
Changing QT version to 5.14.2 fixes this issue. Both versions installed as MSVC precompiled 64-bit (MSVC2017 for 5.14 and MSVC2019 for 5.15) from official QT installer.

Related

How to add a new .ui to project in Visual Studio?

I'm using Qt 6.4 on Visual Studio 2022, I'm trying to understand how to add a new .ui to an existing project.
For example, I have a project with a mainwindow.ui, when I open it on Qt Designer and create a new .ui window from:
[Qt Designer] File -> New -> Main Window -> Create
I added some widgets to this new window and saved it to disk with the name new_window.ui.
How do I include it in my current project? I tried #include "new_window.ui" but It throws a lot of compiler syntax errors.
What I'm trying to achieve is access the widgets from new_window.ui inside of my MainWindow class:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
ui.setupUi(this);
// access the 'ui' from new_window.ui here
// new_window_ui.pushButton-> ...
}
I have seen that this ui generally is a private class member, there's no problem in having the new_window.ui ui public.
What is the correct way to achieve it?
I have been able to include the new ui file by creating it on a new project and copying the generated ui_new_window.h file plus her class header:
// new_window.h
#pragma once
#include "stdafx.h"
#include "ui_new_window.h"
QT_BEGIN_NAMESPACE
namespace Ui { class new_windowClass; };
QT_END_NAMESPACE
class new_window : public QWidget
{
Q_OBJECT
public:
new_window(QWidget *parent = nullptr) : QWidget(parent), ui(new Ui::new_windowClass())
{
ui->setupUi(this);
}
~new_window() { delete ui; };
Ui::new_windowClass *ui;
};

QArrayData error, linking Qt libraries with qmake

I am trying follow the instructions published here:
https://www.linux.org/threads/c-tutorial-create-qt-applications-without-qtcreator.18409/
but in a PC running Windows 10, to build a Qt application createdwith the Atom editor. I have this 3 files in my project right now:
qt_main.cpp
#include <QtWidgets>
#include "mainwidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWidget w;
w.show();
return a.exec();
}
mainwidget.h
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
class QPushButton;
class QTextBrowser;
class MainWidget : public QWidget
{
Q_OBJECT
public:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();
private:
QPushButton* button_;
QTextBrowser* textBrowser_;
};
#endif // MAINWIDGET_H
mainwidget.cpp
#include <QtWidgets>
#include "mainwidget.h"
MainWidget::MainWidget(QWidget *parent) : QWidget(parent)
{
button_ = new QPushButton(tr("Push Me!"));
textBrowser_ = new QTextBrowser();
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(button_,0,0);
mainLayout->addWidget(textBrowser_,1,0);
setLayout(mainLayout);
setWindowTitle(tr("Connecting buttons to processes.."));
}
MainWidget::~MainWidget()
{
delete button_;
delete textBrowser_;
}
I execute this commands, in sequence:
qmake -project
add `QT += widgets` to the qt_main.pro file generated
qmake qt_main.pro
make
After that, the EXE file is generated without error, but when I try run it, I got the error:
"The procedure entry point _ZN10QArrayData10deallocateEPS_jj could not be located in the dynamic link library etc."
I try follow the suggested on the comments for the question:
QArrayData error, linking Qt libraries with CMake
and check my PATH. right now, the only directory containing qt DLLs is the one:
C:\Qt\Qt5.14.0\5.14.0\mingw73_64\bin
(this directory was createwd by the Qt offline installer).
Anyone can give a hint of how to solve this issue?

Qt Heap corruption detected after normal block

i have the following situation:
InspectionTreeWidget header:
class InspectionTreeWidget : public QTreeWidget
{
Q_OBJECT
public:
explicit InspectionTreeWidget(QWidget *parent = 0);
private:
QVBoxLayout *layout_sourcesettings;
QFrame *frame_sourcesettings;
...
InspectionTreeWidget implementation:
InspectionTreeWidget::InspectionTreeWidget(QWidget *parent) :
QTreeWidget(parent)
{
QVBoxLayout *layout_sourcesettings;
layout_sourcesettings= new QVBoxLayout();
frame_sourcesettings=new QFrame(this);
frame_sourcesettings->setLayout(layout_sourcesettings);
...
}
everything works well but when i close the application, i get the error "Heap corruption detected after normal block"
what i'm doing wrong?
Thanks
Edit (Solved):
I don't know if it is a bug or not but the problem was happening because of a sequence in build process:
first i had this code:
InspectionTreeWidget::InspectionTreeWidget(QWidget *parent) :
QTreeWidget(parent)
{
QVBoxLayout* layout_sourcesettings= new QVBoxLayout();
frame_sourcesettings=new QFrame(this);
frame_sourcesettings->setLayout(layout_sourcesettings);
...
}
i did a rebuild and everythink was ok.
then i move the declaration of layout_sourcesettings to the header and i build the project and got not errors compiling but when i close the application i got the heap corruption. But after a rebuild i no longer get the heap corruption...
So my question now is: is this a Qt related bug or everytime i move local declarations to header file i have to do a rebuild?
Thanks

Using OSG's osgViewerQt example with Qt signals/slots

I have already posted this in the OSG mailing list, but the mailing list seems to be a bit slow.
Anyway, I'm trying to modify the osgViewerQt example by adding a new class of my
own that will contain the viewer. The design is:
wrapper.h: Defines class Wrapper. It inherits from
QMainWindow and has a
QDockWidget where the ViewerWidget will be attached.
viewer.h: Defines ViewerWidget class. It's the class from the
example, with a few mods by me.
prueba_qt.cpp: Main function and where a QApplication is created. A Wrapper object is
created here.
The project compiles, but when I execute it, I get an error:
QWidget: Must construct a QApplication before a QPaintDevice*
If I remove the Q_OBJECT line, the signal and the slot from
wrapper.h and compile the files from the terminal using
g++ -IE:/osg-3.0.1/install/include -LE:/osg-3.0.1/install/bin -IC:/Qt64/4.8/include -LC:/Qt64/4.8/bin -losgViewer -lOpenThreads -losgDB -losg -losgGA -losgQt -lQtCore4 -lQtGui4 prueba_qt.cpp
I can execute the app.
Can you please tell me what can I do to make this work? I've struggling all
morning but couldn't find the solution.
Thanks for your time!
PS: SO is Windows 7 64 bits # MingW compiler # Qt 4.8 # OSG 3.0.1
PS2: Here're the files I used in this project, including the pro file from qmake:
wrapper.h
#ifndef Wrapper_hpp
#define Wrapper_hpp
#include "viewer.h"
#include <QtGui/QMainWindow>
#include <QtGui/QDockWidget>
class Wrapper: public QMainWindow {
Q_OBJECT
private:
ViewerWidget* view;
QDockWidget* dock;
public:
Wrapper(void) {
view = new ViewerWidget();
dock = new QDockWidget;
dock->setWidget( view );
dock->setGeometry( 100, 100, 800, 600 );
dock->setAllowedAreas(Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, dock);
dock->show();
}
void Do(void) { view->Do(); }
void Add(void) { view->Add(); }
virtual ~Wrapper(void) {}
public slots:
void MySlot(void) {}
signals:
void MySignal(void);
};
#endif
wrapper.cpp
(This exists only because I read in the Qt forum that moc can only parse cpp files and thus one is needed for the signal/slot mechanism.)
#include "wrapper.h"
Wrapper::Wrapper(void) {
view = new ViewerWidget();
// view->setGeometry( 100, 100, 800, 600 );
dock = new QDockWidget;
dock->setWidget( view );
dock->setGeometry( 100, 100, 800, 600 );
dock->setAllowedAreas(Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, dock);
dock->show();
}
prueba_qt.cpp
#include <QtGui/QApplication>
#include <iostream>
#include "wrapper.h"
int main( int argc, char** argv ) {
osg::ArgumentParser arguments(&argc, argv);
QApplication app(argc, argv);
Wrapper wrap;
wrap.resize(800,600);
wrap.setWindowTitle("Cow");
wrap.showNormal();
wrap.Do();
return app.exec();
}
prueba_qt.pro
######################################################################
# Automatically generated by qmake (2.01a) mar 12. mar 13:45:28 2013
######################################################################
QT += core gui
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += . E:/osg-3.0.1/install/include
LIBS += -LE:/osg-3.0.1/install/bin -losg -lOpenThreads -losgDB -losgGA -losgQt -losgViewer
# Input
HEADERS += viewer.h wrapper.h
SOURCES += prueba_qt.cpp wrapper.cpp
viewer.h: This is quite big, so I uploaded it to pastebin
EDIT #1
I have set OSG_NOTIFY_LEVEL to DEBUG_INFO and got this humongous output. The line with the error is:
FindFileInPath() : trying C:\cygwin\bin\osgPlugQWidget: Must construct a QApplication before a QPaintDevice
EDIT #2
The signal and slot were missing in the code. I have just added them to wrapper.h along with the call to Q_OBJECT.
After a little nap, I revised again the libraries used in the project. The problem was that some of those libraries were in debug mode and some in release mode. When using signals and slots, moc went crazy.
After building OSG debug libraries, I tried again my little example and worked.
So it's done!

ISO C++ forbids declaration of 'QPushButton' with no type in QT Creator

I am running QT Creator on a Linux Ubuntu 9.10 machine. I just got started with QT Creator, and I was going through the tutorials when this error popped up while I was trying to build my project: "ISO C++ forbids declaration of 'QPushButton' with no type". This problem appears in my header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QWidget>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void addContact();
void submitContact();
void cancel();
private:
Ui::MainWindow *ui;
QPushButton *addButton;
QPushButton *submitButton;
QPushButton *cancelButton;
QLineEdit *nameLine;
QTextEdit *addressText;
QMap<QString, QString> contacts;
QString oldName;
QString oldAddress;
};
#endif // MAINWINDOW_H
I think you are simply missing the appropriate header file. Can you try
#include <QtGui/QtGui>
instead, or if you prefer
#include <QtGui/QPushButton>
Actually, forward declaration would be enough, instead of the include:
class QPushButton;
Always prefer forward declarations in headers, and do the include in the .cpp
(faster and less recompilations in larger projects).
You are missing this:
#include <QtGui>
You might also want to check the .pro file.
Do you have an entry like "QT = ..." somewhere? If so, try changing that to "QT += ...". Qt's Core and GUI module are default settings for the QT variable, but CAN be overwritten, which will lead to compiler and/or linker errors.