Important: Please read the Qt Code of Conduct -

Access instance of mainwindow

  • I need to access the instance of the mainwindow created in the main.cpp file in a different class.

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    MainWindow w;;
    return a.exec();

    I need to access w in a different class and I am not able to do it using extern. Is there any other way?

  • Lifetime Qt Champion

    Why do you need to access w ?
    You will not be able to
    w->ui->somewidget as its private.

    The normal way of talking to mainwindow from other class is via signals and slots

    So "other class" can send mainwindow signal to perform tasks.

  • @mrjj

    I need to access a property con of the mainwindow which inturn has a slot updateparameterList .

    I have a tablewidget


  • Lifetime Qt Champion

    but what is w.con ??
    and does "con" have a slot called
    updateparameterList ?

    and u dont need w;
    if you can call connect in any of main windows function then
    "this" will be the same as w;


  • @mrjj

    Ya ..


    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = 0);
    serial_conn *con;


    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
     serial_conn *con = new serial_conn();

    updateparameterList is a slot of con.

    This tablewidget is not in the main window. Its in a different class so I cannot use this->con

  • Lifetime Qt Champion

    Since UI is private you cannot directly connect from mainwindow
    You must connect signal to new public signal inside the Other class
    then connect main to new new signal.

    so in tableWidget Owner class (lets call it OTHER), you define new public signal ( just in h. it has no body)


    and u hook up
    (inside other class. NOTICE the signal->signal)

    then in mainwindow

    so you surface the signals from the widgets and connect from main.
    That is how u hook it up in the right way , keeping encapsulation and not leaking details
    to rest of the program. :)

  • According to your initial question you can access your QApplication instance a from everywhere using the macro qApp(). QApplication inherits QGuiApplication which provides some access methods like focusWindow()and allWindows(). These methods could help you find your MainWindow...
    But this is a not a clean solution and you should try to pass the required instances to the mentioned "other class" instead of using any global refences.

  • @mrjj

    I have an object of "other" say "tab" in the mainwindow.


    The application gives an error "The program has unexpectedly finished." when it reaches it.

  • Lifetime Qt Champion

    well this->tab or this->con might be NULL/dangling pointer
    as to only reason to crash at the connect line;

    did u new this->tab like u did con?
    It must point to allocated object.

    Also, "this" is mainwindow ? or did u move con around?

  • @mrjj

    Ya I used new and this indicates main window.

  • Lifetime Qt Champion

    should work then.
    I never seen it crash on a connect unless bad pointers.

Log in to reply