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;
        w.show();
    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?


  • Qt Champions 2016

    hi
    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

    http://doc.qt.io/qt-5/signalsandslots.html

    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

     connect(ui->tableWidget,SIGNAL(itemChanged(QTableWidgetItem*)),w.con,SLOT(updateparameterList(QTableWidgetItem*)));
    

  • Qt Champions 2016

    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;

    connect(ui->tableWidget,SIGNAL(itemChanged(QTableWidgetItem*)),this->con,SLOT(updateparameterList(QTableWidgetItem*)));



  • @mrjj

    Ya ..

    Mainwindow.h

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    serial_conn *con;
    ...................................
    ...................................
    }
    

    Mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
     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


  • Qt Champions 2016

    Ok
    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)

    signals:
    MyitemChanged(QTableWidgetItem*);

    and u hook up
    connect(ui->tableWidget,SIGNAL(itemChanged(QTableWidgetItem*)),this,SIGNAL(MyitemChanged(QTableWidgetItem*)));
    (inside other class. NOTICE the signal->signal)

    then in mainwindow
    connect(OTHER,SIGNAL(MyitemChanged(QTableWidgetItem*)),this->con,SLOT(updateparameterList(QTableWidgetItem*)));

    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.

    connect(this->tab,SIGNAL(MyitemChanged(QTableWidgetItem*)),this->con,SLOT(updateparameterList(QTableWidgetItem*)));
    

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


  • Qt Champions 2016

    @saitej
    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.


  • Qt Champions 2016

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.