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
QtWS25 Last Chance

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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on 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.

    saitejS 1 Reply Last reply
    3
    • mrjjM mrjj

      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.

      saitejS Offline
      saitejS Offline
      saitej
      wrote on last edited by saitej
      #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
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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*)));

        saitejS 1 Reply Last reply
        0
        • mrjjM mrjj

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

          saitejS Offline
          saitejS Offline
          saitej
          wrote on 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
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #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. :)

            saitejS 1 Reply Last reply
            2
            • miclandM Offline
              miclandM Offline
              micland
              wrote on last edited by micland
              #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
              • mrjjM 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. :)

                saitejS Offline
                saitejS Offline
                saitej
                wrote on 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.

                mrjjM 1 Reply Last reply
                0
                • saitejS saitej

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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?

                  saitejS 1 Reply Last reply
                  1
                  • mrjjM mrjj

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

                    saitejS Offline
                    saitejS Offline
                    saitej
                    wrote on last edited by
                    #10

                    @mrjj

                    Ya I used new and this indicates main window.

                    mrjjM 1 Reply Last reply
                    0
                    • saitejS saitej

                      @mrjj

                      Ya I used new and this indicates main window.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

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

                      1 Reply Last reply
                      1

                      • Login

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