After initializing class member app crashes with SIGSEGV [duplicate] - c++

This question already has an answer here:
C++ - segmentation fault when inheritance
(1 answer)
Closed 6 years ago.
I have Qt application with Application class:
class Application : public QApplication {
public:
Application(int argc, char** argv);
~Application();
};
everything works fine until I add
private:
const QString NAME = "QtImageViewer";
after that app crashes 9 of 10 times when I try to run it
main looks like
int main(int argc, char** argv)
{
Application app(argc, argv);
app.setApplicationName("QtImageViewer");
MainWindow mainWindow;
mainWindow.show();
return (app.exec());
}
I'd like to know what's wrong because I have no idea.
Edit:
GDB backtrace
Valgrind

Fixed it, same problem as here.
My Application class constructor signature is Application(int argc, char** argv); but QApplication has QApplication(int &argc, char **argv)
Changed Application(int argc, char** argv); to Application(int& argc, char** argv); and everything works.
Thanks to all for help.

I'd comment, but I don't have enough reputation at the time of this writing. For the record, I cloned your repository you linked to. I notice you don't seem to be using Qt Creator but are probably just using cmake. My Qt 5 installation isn't where yours is, so I just threw everything into Qt Creator and built and ran the project from there.
The program runs and closes just fine, even with the change you suggest breaks it for you. (Built and run on Ubuntu 16 with Qt 5.5, g++ 5.4.0.)
Additionally, I may be misunderstanding Igor's comment, but since Application is made on the stack, no additional heap deallocation is introduced to ~Application().

Related

QEventloop canot be used without QApplication

I'm currently developing a Console Unit Test Client with Google Test.
The Tests involves a step where a binary file is downloaded from a server.
To download this file I thought about using Qt.
Now I took the example code from
https://wiki.qt.io/Download_Data_from_URL
This gives me the error QEventloop canot be used without QApplication
I found this thread QEventLoop: Cannot be used without QApplication
However this works for a UI application.
My googletest main is predefined
GTEST_API_ int main(int argc, char **argv) {
printf("Running main() from %s\n", __FILE__);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Where would be a good place to add the QApplication?
Could you help me out?
thx for your help :)

In Qt main function, how does QApplication learn about Mainwindow?

Looking at a simplest Qt Widget sample application that you can find from almost every Qt tutorial:
#include "notepad.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Notepad w;
w.show();
return a.exec();
}
There is one thing puzzles me. There are two major variables a and w here. a.exec() starts Qt's main loop, which suppose to interact with the main GUI component w. However, both of them live on stack and I don't see any code pass w somehow to a. So how does a be aware of the existence of w?
Does the constructor of w initializes a static data structure that a can access to check the top-level widgets?
Qt preprocess your code and build the real c++ code before compiling, its at this moment QApplication wrap all Q object in the main.cpp file and build the rest of the code from it.

QTcpSocket not emit any error or connected signals

I have a small project which send some data over network using QTcpSocket. The server works fine but the client(code here) seems does nothing. If I set breakpoint at tcpSocket.connectToHost("127.0.0.1",port); it does jump in, but not any slots I defined.
I can't figure out what's wrong. I think the environment is ok because I can build 2 working examples from Qt GUI Programming
Any ideas are appreciated.
You do not have a QApplication instance and thus no event loop which does all the event / signal&slot handling.
So you at least need a QCoreApplication instance like this in main.cpp:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Client client;
client.connectToServer();
return a.exec();
}

Qt 5.1 QApplication Without Display - QXcbConnection: Could not connect to display

I'm using Qt5.1 and I'm trying to create a QApplication without a display. I need to draw text with QPainter, so I need to use QApplication (or QGuiApplication), otherwise I get a segfault.
The application worked fine in Qt4.8, but fails in Qt5.1 on a headless version of Ubuntu with the error:
"QXcbConnection: Could not connect to display".
In Qt 4.8, I was able to use the following constructor with GUIenabled = false to create a QApplication that did not require a display:
QApplication::QApplication ( int & argc, char ** argv, bool GUIenabled )
In Qt5.1, the constructor for QApplication no longer has the GUIenabled flag.
I scanned the source code briefly, and there does seem to be a flag in the QApplication constructor, but it is undocumented as to what options can be used in that flag. Using "false" does not work.
How can I create a QApplication without a display? Is there an alternative method to telling QApplication GUIenabled = false? Alternatively, can I create a QCoreApplication that will not segfault when drawing text with QPainter on a QImage?
Yes, that's a Qt 3 (?) thing that is gone in Qt 5. Try running your application with the -platform offscreen command line option instead.
Note that you don't need QApplication or linking to QtWidgets to just draw upon a QImage, using QGuiApplication (and linking to QtGui) is sufficient.
If you want to create an app without GUI, you need to use QCoreApplication instead of QApplication.
Just hit this same issue. Really annoying that it at least isn't a compile error. My solution was just to use pointers and heap objects like,
QCoreApplication* app = 0;
Display* display = XOpenDisplay(NULL);
if (display)
{
XCloseDisplay(display);
app = new QApplication(argc, argv);
qobject_cast<QApplication*>(app)->setQuitOnLastWindowClosed(false);
}
else
{
app = new QCoreApplication(argc, argv);
}
return app->exec();

What is "QApplication app(argc, argv)" trying to do?

#include <QtGui/QApplication>
#include <QtDeclarative>
#include "qmlapplicationviewer.h"
int main(int argc, char **argv) {
QApplication app(argc, argv);
QmlApplicationViewer viewer;
viewer.setMainQmlFile("app/native/assets/main.qml");
viewer.showFullScreen();
return app.exec();
}
My C++ is a bit rusty. Can someone please explain to me what is "QApplication app(argc, argv)" trying to do ?
Is it trying to declare a function which takes in 2 arguments (argc and argv) and return a variable of type QApplication ?
The line
QApplication app(argc, argv);
creates a new instance of type QApplication and invokes the constructor of this class. In your example, the variable app now stores this instance. It is somewhat (semantically) a shorthand of this:
QApplication app = QApplication(argc, argv);
Here's a quote from Qt Docs:
The QApplication class manages the GUI application's control flow and
main settings.
QApplication contains the main event loop, where all events from the
window system and other sources are processed and dispatched. It also
handles the application's initialization, finalization, and provides
session management. In addition, QApplication handles most of the
system-wide and application-wide settings.
For any GUI application using Qt, there is precisely one QApplication
object, no matter whether the application has 0, 1, 2 or more windows
at any given time. For non-GUI Qt applications, use QCoreApplication
instead, as it does not depend on the QtGui library.
The QApplication object is accessible through the instance() function
that returns a pointer equivalent to the global qApp pointer.
So, the line
QApplication app(argc, argv);
creates an instance of the QApplication class.
QApplication is a Qt class that contains the main event loop.
When you write QApplication app(argc, argv);
you are creating a object app of this class, by calling its constructor with argc and argv
When int main(int argc, char **argv) is called while running the program, int argc is intialized to contain the number of arguments passed while running the program. char **argv contains an array of arguments passed to the program when executing it.
char* argv[0] will contain (point to) the name of the program, while the subsequent elements will point to the other arguments passed.
argc and argv are in turn, passed to the constructor of QApplication, so that one can pass Qt specific arguments when running your program.
For an example of such arguments try running ./yourProgramName --help in a terminal window
app() is not a function, it is a constructor call.
If you come from C# or Java or something, imagine it as
QApplication app = new QApplication( argc, argv );
Just that app would be a pointer this way, while it actually is the object itself if it is created like in your example.
In short, Qt needs a QApplication instance to run so signals&slots are processed (if you are using them) and events like painting etc are handled