Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Access instance of mainwindow

    General and Desktop
    3
    11
    3337
    Loading More Posts
    • 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.
    • saitej
      saitej last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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.

        saitej 1 Reply Last reply Reply Quote 3
        • saitej
          saitej @mrjj last edited by saitej

          @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 Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by

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

            saitej 1 Reply Last reply Reply Quote 0
            • saitej
              saitej @mrjj last edited by

              @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 Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion last edited by mrjj

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

                saitej 1 Reply Last reply Reply Quote 2
                • micland
                  micland last edited by micland

                  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 Reply Quote 0
                  • saitej
                    saitej @mrjj last edited by

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

                    mrjj 1 Reply Last reply Reply Quote 0
                    • mrjj
                      mrjj Lifetime Qt Champion @saitej last edited by

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

                      saitej 1 Reply Last reply Reply Quote 1
                      • saitej
                        saitej @mrjj last edited by

                        @mrjj

                        Ya I used new and this indicates main window.

                        mrjj 1 Reply Last reply Reply Quote 0
                        • mrjj
                          mrjj Lifetime Qt Champion @saitej last edited by

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

                          1 Reply Last reply Reply Quote 1
                          • First post
                            Last post