Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access instance of mainwindow
Forum Update on Monday, May 27th 2025

Access instance of mainwindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 3.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saitej
    wrote on 1 Jun 2016, 07:57 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Jun 2016, 08:04 last edited by
      #2

      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.

      S 1 Reply Last reply 1 Jun 2016, 08:12
      3
      • M mrjj
        1 Jun 2016, 08:04

        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.

        S Offline
        S Offline
        saitej
        wrote on 1 Jun 2016, 08:12 last edited by saitej 6 Jan 2016, 08:13
        #3

        @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*)));
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 1 Jun 2016, 08:17 last edited by
          #4

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

          S 1 Reply Last reply 1 Jun 2016, 08:28
          0
          • M mrjj
            1 Jun 2016, 08:17

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

            S Offline
            S Offline
            saitej
            wrote on 1 Jun 2016, 08:28 last edited by
            #5

            @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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 1 Jun 2016, 08:36 last edited by mrjj 6 Jan 2016, 08:37
              #6

              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. :)

              S 1 Reply Last reply 1 Jun 2016, 11:02
              2
              • miclandM Offline
                miclandM Offline
                micland
                wrote on 1 Jun 2016, 09:36 last edited by micland 6 Jan 2016, 09:36
                #7

                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.

                1 Reply Last reply
                0
                • M mrjj
                  1 Jun 2016, 08:36

                  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. :)

                  S Offline
                  S Offline
                  saitej
                  wrote on 1 Jun 2016, 11:02 last edited by
                  #8

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

                  M 1 Reply Last reply 1 Jun 2016, 11:07
                  0
                  • S saitej
                    1 Jun 2016, 11:02

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

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 1 Jun 2016, 11:07 last edited by
                    #9

                    @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?

                    S 1 Reply Last reply 1 Jun 2016, 11:15
                    1
                    • M mrjj
                      1 Jun 2016, 11:07

                      @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?

                      S Offline
                      S Offline
                      saitej
                      wrote on 1 Jun 2016, 11:15 last edited by
                      #10

                      @mrjj

                      Ya I used new and this indicates main window.

                      M 1 Reply Last reply 1 Jun 2016, 11:29
                      0
                      • S saitej
                        1 Jun 2016, 11:15

                        @mrjj

                        Ya I used new and this indicates main window.

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 1 Jun 2016, 11:29 last edited by
                        #11

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

                        1 Reply Last reply
                        1

                        1/11

                        1 Jun 2016, 07:57

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved