Search code examples
qtqtgui

Creating a QApplication inside of a method in non-qt application


I have a larger application and I want to create GUI dialog for one specific subtask. I decided to use Qt but I am not familiar with it so I am doing this thing probably the wrong way.

My quick and dirty prototype:

void ManualFeatureMatcher::matchFeatures() {
    int argc = 0;
    char* argv[1] = {NULL};
    QApplication a(argc, argv);
    Widget w;
    w.show();
    a.exec();
}

This works fine the first time the function is called but when I call it again I get these errors:

    (app:8540): Gtk-CRITICAL **: IA__gtk_widget_realize: assertion `GTK_WIDGET_ANCHORED (widget) || GTK_IS_INVISIBLE (widget)' failed

    (app:8540): Gtk-CRITICAL **: IA__gtk_widget_realize: assertion `GTK_WIDGET_ANCHORED (widget) || GTK_IS_INVISIBLE (widget)' failed

    (app:8540): Gtk-CRITICAL **: IA__gtk_container_add: assertion `GTK_IS_CONTAINER (container)' failed

    (app:8540): Gtk-CRITICAL **: IA__gtk_widget_realize: assertion `GTK_WIDGET_ANCHORED (widget) || GTK_IS_INVISIBLE (widget)' failed

What is the correct way of accomplishing this?


Solution

  • Solved.

    I have made smart pointer to QApplication a member of the class and made sure it is created only once.

    Instead of pure Widgets i use QDialogs now so I do not need to run the QApplication loop because as @Archie said in the comment: Modal dialogs contain its own loop inside.

    More details on blog