QApplication Program Execution Segmentation - c++

Could anybodyy help me with this problem? I am trying to run an application using the cmakefiles. on the main file of my program I get a segmentation fault when the program gets to the line of code to execute the QAppication. Here is the fragment code below:
int main(int argc, char** argv)
{
bool viewing;
parse_command_line( argc, argv );
#ifdef _GRAPHICS_
glutInit(&argc, argv); // note the code runs correctly when this line is excluded and the glutInit was initialized in another class named Viewer (See class Viewer instantiated below), however for my specific application I need to initialize the glutInit in the main program
#endif
if( viewing )
{
#ifdef _GRAPHICS_
QApplication application(argc, argv);
Viewer *viewer = new Viewer( 0, exp, argc, argv );
Interface *render = new Interface( 0, exp, viewer );
render->show();
return application.exec(); //this line causes the segmentation fault
delete viewer;
delete render;
#endif
}
}

When glutInit is called inside Viewer, application and viewer receive all the command line arguments. When you call it before, like you do, glutInit will eat all the parameters it understands, so the other objects might miss some arguments.
Possible solutions: do the glutInit (after application creation), or make a copy of argc/argv.

Related

app.exec() does not return on window close

I have a simple Qt application in Visual Studio:
int main(int argc, char* argv[]){
QApplication app(argc, argv);
Myclass* c = new MyClass;
c->show();
int ret = app.exec();
return ret;
}
but my application does not return when I close the windows (in debug mode, app.exec() does not return).
The process and sub process Qt Qtwebengineprocessd remains in execution.
How can I control/debug in Visual Studio why/where my application is blocked?
According to the Qt documentation for exec you have to call QApplication::exit(code) or QApplication::quit() for exec to return. You should probably call one of these functions when you detect a QCloseEvent in your MyClass widget.
If the class that instantiate MarbleWidget class have a QMainWindow(parent) can be added void MyClass::closeEvent(QCloseEvent *e){delete c;}
If there are no QMainWindows or QDialBox add in the main:
app.closeAllWindows()
and in the destructor:
QApplication::quit();

Qt Segmentation fault at exec()

I have a very strange problem while trying to run a QProcess in a class HmiApplication, which is derived from QApplication.
The application throws a SIGSEGV in line 6 of main.cpp. This occurs only if line 11 of hmiapplication.cpp is commented out (If I don't qDebug() the stdout of the QProcess).
For the sake of simplicity and clarity, I didn't handle any return values while creating the QProcess.
main.cpp
#include "hmiapplication.h"
int main(int argc, char **argv)
{
HmiApplication hmi(argc, argv);
return hmi.exec(); // LINE 6 - SIGSEGV
}
hmiapplication.h
#ifndef HMIAPPLICATION_H
#define HMIAPPLICATION_H
#include <QApplication>
#include <QProcess>
class HmiApplication : public QApplication
{
Q_OBJECT
public:
HmiApplication(int argc, char **argv);
virtual ~HmiApplication();
private:
QProcess *macFinder = nullptr;
};
#endif // HMIAPPLICATION_H
hmiapplication.cpp
#include "hmiapplication.h"
HmiApplication::HmiApplication(int argc, char **argv) : QApplication(argc, argv)
{
macFinder = new QProcess(this);
macFinder->start("arping", QStringList() << "-c 2" << "192.168.1.1");
macFinder->waitForReadyRead();
QString ret(macFinder->readAllStandardOutput());
ret = ret.mid(ret.indexOf('[') + 1, 17);
qDebug() << ret; // LINE 11
}
HmiApplication::~HmiApplication()
{
}
EDIT:
If I add QVector<Camera*> cameras; to the header and
for(quint8 i = 0; i < 10; i++) {
Camera *cam = new Camera(i);
cameras.append(cam);
}
to the source file, it doesn't matter whether or not I remove the qDebug() line and will throw a segmentation fault in both cases.
Camera is a derived class of QLabel and is perfectly working without the QProcess mentionened above.
The QApplication constructor accepts its first parameter by reference...
QApplication::QApplication(int &argc, char **argv)
With the documentation also warning that...
The data referred to by argc and argv must stay valid for the entire
lifetime of the QApplication object. In addition, argc must be greater
than zero and argv must contain at least one valid character string.
However, you pass argc by value to the HmiApplication. Hence the QApplication constructor receives a non-const reference to the local copy which will go out of scope at the end of the HmiApplication ctor leading to undefined behaviour later on.
Change the signature of your constructor to...
HmiApplication::HmiApplication(int &argc, char **argv)

QTGraphicsView allways instantly closes after running outside from main method

I want to create a file that contains and manages the entire UI outside of the main.cpp and its main function.
To start I used the code from this answer, which worked fine
from inside the main method.
To prevent information loss I show the code below as well:
#include ...
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item(QPixmap("c:\\test.png"));
scene.addItem(&item);
view.show();
return a.exec();
}
I tried to outsource this code into an own
class, but after executing the shown code the just created window
disappeared again within a few milliseconds without a warning or an error.
To check if I made a mistake within my own class I tried to use this code
from an own function within my main.cpp
void initUI(QApplication * application){
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item(QPixmap(application->applicationDirPath() + "\\graphics\\Theme1\\background.png"));
scene.addItem(&item);
view.show();
}
But the same problem ocurs here. Why does the shown code needs to be executed within the main method, and how could you outsource it?
In case this information helps: I'm running on windows with Qt Creator 3.6.1
based on Qt 5.6.0 (MSVC 2013, 32 bit)
Edit:
Here is my main method
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
initUI(&a);
return a.exec();
}
Edit 2:
The error does not occur when I add an
application->exec();
Can somebody explain why this happens? What is the difference between
the call of application->exec() in the initUI-method and the a.exec() call
within the main method?
The destructors of scene and view are called once you exit initUI, as it is put on the stack. You need to put it on the heap in order to survive after exiting the init function. Of course you should take care of dangling pointers.

Qt Gui Unit Test: Must construct a QApplication before a QPaintDevice

I managed to write a "Gui Unit Test" using this tutorial. However, when I compile my code I get this:
PASS : testUnits::initTestCase()
PASS : testUnits::toUpper()
QFATAL : testUnits::testGui() QWidget: Must construct a QApplication before a QPaintDevice
FAIL! : testUnits::testGui() Received a fatal error.
Unknown file(0) : failure location
Totals: 2 passed, 1 failed, 0 skipped
********* Finished testing of testUnits *********
why is it giving me: Must construct a QApplication before a QPaintDevice ?
it works now, here is my Gui unit test function definition:
void testUnits::testGui(){
QLineEdit lineEdit;
QTest::keyClicks(&lineEdit,"hellow world");
QCOMPARE(lineEdit.text(), QString("hello world"));
}
and here is how my main() looked like before fixing the problem :
int main(int argc, char *argv[])
{
testUnits testString;
QTest::qExec(&testString, argc, argv);
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
So, when I compile the code, it execute the unit test before constructing the QApplication.
Therefore I put my QTest::qExec() after QApplication and now it works and the main() looks like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
testUnits testString;
QTest::qExec(&testString, argc, argv);
return a.exec();
}
It's better to not define your own main manually. All of the test logic, including the required preconditions, shall be made within the test object (the QObject subclass with the test* slots) itself. See the QTestLib manual for details.
If you do this, then simply using the QTEST_MAIN will do the right thing and save you some time.

Valid OpenGL context

How and at what stage is a valid OpenGL context created in my code? I'm getting errors on even simple OpenGL code.
From the posts on comp.graphics.api.opengl, it seems like most newbies burn their hands on their first OpenGL program. In most cases, the error is caused due to OpenGL functions being called even before a valid OpenGL context is created. OpenGL is a state machine. Only after the machine has been started and humming in the ready state, can it be put to work.
Here is some simple code to create a valid OpenGL context:
#include <stdlib.h>
#include <GL/glut.h>
// Window attributes
static const unsigned int WIN_POS_X = 30;
static const unsigned int WIN_POS_Y = WIN_POS_X;
static const unsigned int WIN_WIDTH = 512;
static const unsigned int WIN_HEIGHT = WIN_WIDTH;
void glInit(int, char **);
int main(int argc, char * argv[])
{
// Initialize OpenGL
glInit(argc, argv);
// A valid OpenGL context has been created.
// You can call OpenGL functions from here on.
glutMainLoop();
return 0;
}
void glInit(int argc, char ** argv)
{
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(WIN_POS_X, WIN_POS_Y);
glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT);
glutCreateWindow("Hello OpenGL!");
return;
}
Note:
The call of interest here is glutCreateWindow(). It not only creates a window, but also creates an OpenGL context.
The window created with glutCreateWindow() is not visible until glutMainLoop() is called.