0

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?

valeriot90
  • 65
  • 7
  • 1
    `MyClass` is not a class provided by Qt so without knowing how `MyClass` is implemented it is not possible to tell if there is an error in it. Besides that, your shown code would not compile: `Myclass*` and `new MyClass` don't match. When you post code, post an actual copy and past of a [mcve], otherwise, we can't be sure what is a typo and what is an error. – t.niese Jul 16 '21 at 08:23
  • MyClass, is my class definition. But I've asked if there is a way in visual studio to debug/control where my application is blocked? – valeriot90 Jul 16 '21 at 08:29
  • You theoretically could set some breakpoints in the Qt code (in its event loop) to see why it is still active. Where depends on the actual version of qt that you use. But understanding the internals of Qt isn't that easy. The problem is likely in your `MyClass` implementation, so you would check its code if it is logically correct instead of using breakpoints. – t.niese Jul 16 '21 at 08:33
  • My class in an implementation of Marble kde, that have intensive use of Qt. I have to control if I call correctly the Marble destructor. Currently I use Qt 5.13. Thank you. – valeriot90 Jul 16 '21 at 08:37
  • I have this issue too, when I Run it's ok but when I use debug , my program is crashed in that line `(app.exec())` and didn't say me why this happened. – Parisa.H.R Jul 16 '21 at 09:41
  • Without knowing what is in `MyClass` we can hardly help you... Btw. unless you set `Qt::WA_DeleteOnClose` for your `c` object, you have a leak and your object is not correctly destroyed. – HiFile.app - best file manager Jul 16 '21 at 15:11
  • Your code probably got stuck with main event loop. I would suggest adding a periodical timer which will call a function which will write all your existing widgets. I bet that at least one of them still lives even when you closed your `MyClass` instance. I.e. add this code to your `main()`: `QTimer t; QObject::connect(&t, &QTimer::timeout, []{ qDebug() << qApp->topLevelWidgets(); }); t.start(1000); ` – HiFile.app - best file manager Jul 16 '21 at 15:15
  • I've added Qt::WA_DeleteOnClose and Qt::WA_QuitOnClose but nothing change – valeriot90 Jul 19 '21 at 07:45
  • With this code: QTimer t; QObject::connect(&t, &QTimer::timeout, []{ qDebug() << qApp->topLevelWidgets(); }); t.start(1000); The widgets that remains open are: QSvgWidget Navigation, WebPopupWidget, EclipsesBrowserDialog, GpsInfoPlugin and other that are all attributable to Marble – valeriot90 Jul 19 '21 at 07:55
  • If the class that instantiate MarbleWidget class have a QMainWindow(parent) can be added `void MyClass::closeEvent(QCloseEvent *e){delete c;}` – valeriot90 Jul 19 '21 at 09:42

2 Answers2

0

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.

Mestkon
  • 3,532
  • 7
  • 18
  • The [default behavior](https://doc.qt.io/qt-5.15/qguiapplication.html#quitOnLastWindowClosed-prop) is to quit when the last window (here `MyClass`) closes. – Botje Jul 16 '21 at 11:46
  • I have the same problem: https://stackoverflow.com/questions/59921112/marblewidget-with-qt5-support-example-code-no-longer-exits-properly but the solution doesn't work for me. – valeriot90 Jul 16 '21 at 14:11
0

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();

valeriot90
  • 65
  • 7