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. Direct connect a built-in signal from one class to a slot on another class
Forum Updated to NodeBB v4.3 + New Features

Direct connect a built-in signal from one class to a slot on another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 7 Posters 6.7k Views 3 Watching
  • 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #14

    What Qt version/ compiler do you use? I would say an ancient one... then you should also take a look at the Qt version documentation you use. e.g. for Qt5.5: https://doc.qt.io/archives/qt-5.5/qcombobox.html#currentIndexChanged

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    H 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      What Qt version/ compiler do you use? I would say an ancient one... then you should also take a look at the Qt version documentation you use. e.g. for Qt5.5: https://doc.qt.io/archives/qt-5.5/qcombobox.html#currentIndexChanged

      H Offline
      H Offline
      Habibie
      wrote on last edited by
      #15

      @Christian-Ehrlicher said in Direct connect a built-in signal from one class to a slot on another class:

      What Qt version/ compiler do you use? I would say an ancient one... then you should also take a look at the Qt version documentation you use. e.g. for Qt5.5: https://doc.qt.io/archives/qt-5.5/qcombobox.html#currentIndexChanged

      I'm doing all of these on a 64-bit Dell Inspiron-3252 (a rather old) computer running on an uBuntu 21.04 (Hirsute) OS with Qt-5.15.2 (I think this is pretty up-to-date for Qt-5.x) and gcc-10.3.0.

      ~ qmake --version
      QMake version 3.1
      Using Qt version 5.15.2 in /usr/lib/x86_64-linux-gnu
      ~
      ~ g++ -v
      Using built-in specs.
      COLLECT_GCC=g++
      COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper
      OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
      OFFLOAD_TARGET_DEFAULT=1
      Target: x86_64-linux-gnu
      Configured with: ../src/configure -v --with-pkgversion='Ubuntu 10.3.0-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-10-gDeRY6/gcc-10-10.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-gDeRY6/gcc-10-10.3.0/debian/tmp-gcn/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-mutex
      Thread model: posix
      Supported LTO compression algorithms: zlib zstd
      gcc version 10.3.0 (Ubuntu 10.3.0-1ubuntu1)
      
      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #16

        I see that you compiler error does not fit to your pasted code:

        connect(QComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),

        I don't see a variable named QComboBox in your code.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        H 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          I see that you compiler error does not fit to your pasted code:

          connect(QComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),

          I don't see a variable named QComboBox in your code.

          H Offline
          H Offline
          Habibie
          wrote on last edited by
          #17

          @Christian-Ehrlicher said in Direct connect a built-in signal from one class to a slot on another class:

          I see that you compiler error does not fit to your pasted code:

          connect(QComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),

          I don't see a variable named QComboBox in your code.

          You are right and sorry about that.

          Now, I put it exactly like the documentation link you provided and filled the dotted line with ui->lineEdit(QString::number(index)); in the constructor of DialogOne class as shown below.

          #include "dialogone.h"
          #include "ui_dialogone.h"
          
          DialogOne::DialogOne(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::DialogOne)
          {
              ui->setupUi(this);
          
              init();
              connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index){
                  ui->lineEdit(QString::number(index));
              });
          }
          

          The compilation of the above code generates the following error:

          g++ -c -pipe -g -std=gnu++11 -Wall -Wextra -D_REENTRANT -fPIC -DGIT_VERSION=\"\" -DGIT_HASH=\"febc5d8c3a77a4a9039601f638b0ce220c3b3189\" -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../SignalsAndSlots-01 -I. -I../include -I/usr/include/x86_64-linux-gnu/qt5 -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets -I/usr/include/x86_64-linux-gnu/qt5/QtGui -I/usr/include/x86_64-linux-gnu/qt5/QtCore -I. -I. -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -o dialogone.o ../src/dialogone.cpp
          ../src/dialogone.cpp: In constructor ‘DialogOne::DialogOne(QWidget*)’:
          ../src/dialogone.cpp:12:13: error: ‘comboBox’ was not declared in this scope; did you mean ‘QComboBox’?
             12 |     connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index){
                |             ^~~~~~~~
                |             QComboBox
          ../src/dialogone.cpp: In lambda function:
          ../src/dialogone.cpp:13:13: error: ‘class Ui::DialogOne’ has no member named ‘lineEdit’
             13 |         ui->lineEdit(QString::number(index));
                |             ^~~~~~~~
          make: *** [Makefile:566: dialogone.o] Error 1
          
          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #18

            C++ basics missing - you don't have a member named 'comboBox' on your class - so how do you suppose that this works? I would guess your combobox is somewhere in your ui struct.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            H 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              C++ basics missing - you don't have a member named 'comboBox' on your class - so how do you suppose that this works? I would guess your combobox is somewhere in your ui struct.

              H Offline
              H Offline
              Habibie
              wrote on last edited by
              #19

              @Christian-Ehrlicher said in Direct connect a built-in signal from one class to a slot on another class:

              C++ basics missing - you don't have a member named 'comboBox' on your class - so how do you suppose that this works? I would guess your combobox is somewhere in your ui struct.

              Exactly.

              I ONLY have a comboBox in DialogOne class. From there, whenever there is a change in the index (currentIndexChanged), I want it to trigger the external signal indexChanged(int index) (also declared in the header file of DialogOne class). This is done through the old (string) style SIGNAL/SIGNAL connect in the constructor of DialogOne class as shown below and it works just fine.

              #include "dialogone.h"
              #include "ui_dialogone.h"
              
              DialogOne::DialogOne(QWidget *parent) :
                  QDialog(parent),
                  ui(new Ui::DialogOne)
              {
                  ui->setupUi(this);
              
                  init();
                  connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(indexChanged(int)));
              }
              

              However, since in the constructor of MainWindow, I use the new functional style connect, you suggested in one of your posts above to drop your SIGNAL/SLOT macros, change over to New Signal Slot Syntax. TBH, I was so happy and thrill to see such a suggestion mainly because I wasn't able to do it before and someone like you made such a recommendation giving me a hope, especially after I read the documentation link you suggested to read. So, now it looks like that it's not possible, is it? I am still keeping my fingers crossed to hope the answer is yes.

              The documentation link you suggested to read really gives me an impression that we can just drop the external indexChange(in index) signal (declared in the header file of DialogOne class) and use lambda to connect the built-in currentIndexChangedsignal from thecomboBoxdirectly to the twolineEditdeclared in bothMainWindowandDialogTwo` classes. If this can be done, I am all ears.

              JonBJ 1 Reply Last reply
              0
              • H Habibie

                @Christian-Ehrlicher said in Direct connect a built-in signal from one class to a slot on another class:

                C++ basics missing - you don't have a member named 'comboBox' on your class - so how do you suppose that this works? I would guess your combobox is somewhere in your ui struct.

                Exactly.

                I ONLY have a comboBox in DialogOne class. From there, whenever there is a change in the index (currentIndexChanged), I want it to trigger the external signal indexChanged(int index) (also declared in the header file of DialogOne class). This is done through the old (string) style SIGNAL/SIGNAL connect in the constructor of DialogOne class as shown below and it works just fine.

                #include "dialogone.h"
                #include "ui_dialogone.h"
                
                DialogOne::DialogOne(QWidget *parent) :
                    QDialog(parent),
                    ui(new Ui::DialogOne)
                {
                    ui->setupUi(this);
                
                    init();
                    connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(indexChanged(int)));
                }
                

                However, since in the constructor of MainWindow, I use the new functional style connect, you suggested in one of your posts above to drop your SIGNAL/SLOT macros, change over to New Signal Slot Syntax. TBH, I was so happy and thrill to see such a suggestion mainly because I wasn't able to do it before and someone like you made such a recommendation giving me a hope, especially after I read the documentation link you suggested to read. So, now it looks like that it's not possible, is it? I am still keeping my fingers crossed to hope the answer is yes.

                The documentation link you suggested to read really gives me an impression that we can just drop the external indexChange(in index) signal (declared in the header file of DialogOne class) and use lambda to connect the built-in currentIndexChangedsignal from thecomboBoxdirectly to the twolineEditdeclared in bothMainWindowandDialogTwo` classes. If this can be done, I am all ears.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #20

                @Habibie
                You say this works:

                connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(indexChanged(int)));
                

                and it's inside DialogOne::DialogOne().

                Then you say you changed to

                connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index){
                

                still inside DialogOne::DialogOne().

                The compiler says

                ../src/dialogone.cpp:12:13: error: ‘comboBox’ was not declared in this scope

                Why have you changed from ui->comboBox to comboBox?

                H 1 Reply Last reply
                1
                • JonBJ JonB

                  @Habibie
                  You say this works:

                  connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(indexChanged(int)));
                  

                  and it's inside DialogOne::DialogOne().

                  Then you say you changed to

                  connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index){
                  

                  still inside DialogOne::DialogOne().

                  The compiler says

                  ../src/dialogone.cpp:12:13: error: ‘comboBox’ was not declared in this scope

                  Why have you changed from ui->comboBox to comboBox?

                  H Offline
                  H Offline
                  Habibie
                  wrote on last edited by
                  #21

                  @JonB said in Direct connect a built-in signal from one class to a slot on another class:

                  @Habibie
                  You say this works:

                  connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(indexChanged(int)));
                  

                  and it's inside DialogOne::DialogOne().

                  The above uses the old string SIGNAL/SIGNAL connection style.

                  Then you say you changed to

                  connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index){
                  

                  still inside DialogOne::DialogOne().

                  The compiler says

                  ../src/dialogone.cpp:12:13: error: ‘comboBox’ was not declared in this scope

                  Why have you changed from ui->comboBox to comboBox?

                  The above uses a new functional connection with lambda.

                  BTW, with an old style string SIGNAL/SIGNAL connection, if I change from this

                      connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(indexChanged(int)));
                  

                  to this

                      connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(indexChanged(int)));
                  

                  the compilation spits out the following error messages.

                  g++ -c -pipe -g -std=gnu++11 -Wall -Wextra -D_REENTRANT -fPIC -DGIT_VERSION=\"\" -DGIT_HASH=\"febc5d8c3a77a4a9039601f638b0ce220c3b3189\" -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../SignalsAndSlots-01 -I. -I../include -I/usr/include/x86_64-linux-gnu/qt5 -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets -I/usr/include/x86_64-linux-gnu/qt5/QtGui -I/usr/include/x86_64-linux-gnu/qt5/QtCore -I. -I. -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -o dialogone.o ../src/dialogone.cpp
                  ../src/dialogone.cpp: In constructor ‘DialogOne::DialogOne(QWidget*)’:
                  ../src/dialogone.cpp:12:13: error: ‘comboBox’ was not declared in this scope; did you mean ‘QComboBox’?
                     12 |     connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(indexChanged(int)));
                        |             ^~~~~~~~
                        |             QComboBox
                  make: *** [Makefile:566: dialogone.o] Error 1
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #22

                    Hi,

                    That's exactly the point of @JonB, there's no reason to remove the ui-> from your code.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    H 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      That's exactly the point of @JonB, there's no reason to remove the ui-> from your code.

                      H Offline
                      H Offline
                      Habibie
                      wrote on last edited by
                      #23

                      @SGaist said in Direct connect a built-in signal from one class to a slot on another class:

                      Hi,

                      That's exactly the point of @JonB, there's no reason to remove the ui-> from your code.

                      Got it and thank you for pointing it out.

                      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