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. Get QComboBox reference to connect()
Qt 6.11 is out! See what's new in the release blog

Get QComboBox reference to connect()

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 2 Posters 3.5k Views 1 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.
  • H Offline
    H Offline
    Helson
    wrote on last edited by Helson
    #1

    Hello,
    I have a dialog with a QCombBox and QListWidget.
    In another class I have an array who will fill a QListWidget when combobox changes.
    In this same class, I try create a connect to a slot.

    MyDialog dlg;
    connect(dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&)));
    

    This code above don't work. Even if I put combobox and slot public.
    But, if I put this code inside the dialog class, works fine.

    connect(mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&))); //into dialog class
    

    How I can reference the combobox outside dialog class to connect in a slot?

    My bests regards.

    K 1 Reply Last reply
    0
    • H Helson

      Hello,
      I have a dialog with a QCombBox and QListWidget.
      In another class I have an array who will fill a QListWidget when combobox changes.
      In this same class, I try create a connect to a slot.

      MyDialog dlg;
      connect(dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&)));
      

      This code above don't work. Even if I put combobox and slot public.
      But, if I put this code inside the dialog class, works fine.

      connect(mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&))); //into dialog class
      

      How I can reference the combobox outside dialog class to connect in a slot?

      My bests regards.

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Helson

      What is your error message?
      Is it a compilation or a runtime issue?

      Vote the answer(s) that helped you to solve your issue(s)

      H 1 Reply Last reply
      0
      • K koahnig

        @Helson

        What is your error message?
        Is it a compilation or a runtime issue?

        H Offline
        H Offline
        Helson
        wrote on last edited by
        #3

        @koahnig No error, only the slot don't execute if I put the connect() outside dialog class.

        K 1 Reply Last reply
        0
        • H Helson

          @koahnig No error, only the slot don't execute if I put the connect() outside dialog class.

          K Offline
          K Offline
          koahnig
          wrote on last edited by koahnig
          #4

          @Helson
          So you have a runtime problem.
          The this pointer is different between both cases.

          Try the reference syntax of connect as given in the small example
          Look out for
          ´´´
          Counter a, b;
          QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
          ´´´

          Vote the answer(s) that helped you to solve your issue(s)

          H 1 Reply Last reply
          0
          • K koahnig

            @Helson
            So you have a runtime problem.
            The this pointer is different between both cases.

            Try the reference syntax of connect as given in the small example
            Look out for
            ´´´
            Counter a, b;
            QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
            ´´´

            H Offline
            H Offline
            Helson
            wrote on last edited by Helson
            #5

            @koahnig
            But in this way:
            dlg.mpCourseComboBox
            How I can pass the reference?

            connect(&dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&)));
            

            This give a compilation error:

            error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'QComboBox **' to 'const QObject *'
            
            K 1 Reply Last reply
            0
            • H Helson

              @koahnig
              But in this way:
              dlg.mpCourseComboBox
              How I can pass the reference?

              connect(&dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&)));
              

              This give a compilation error:

              error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'QComboBox **' to 'const QObject *'
              
              K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              @Helson

              The references are meant for the 2nd and 4th parameter of the connect statement.
              The 1st is already a pointer. The reference there yields a pointer to a pointer and that is wrong.
              Please check out the documentation as linked above. There is also a wiki entry. Unfortunately, I do not remember where to find this.

              Vote the answer(s) that helped you to solve your issue(s)

              H 1 Reply Last reply
              0
              • K koahnig

                @Helson

                The references are meant for the 2nd and 4th parameter of the connect statement.
                The 1st is already a pointer. The reference there yields a pointer to a pointer and that is wrong.
                Please check out the documentation as linked above. There is also a wiki entry. Unfortunately, I do not remember where to find this.

                H Offline
                H Offline
                Helson
                wrote on last edited by
                #7

                @koahnig

                connect(&dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), &dlg, SLOT(&dlg.listLoadValues(const int&)));

                This complile but the slot never execute.

                K 1 Reply Last reply
                0
                • H Helson

                  @koahnig

                  connect(&dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), &dlg, SLOT(&dlg.listLoadValues(const int&)));

                  This complile but the slot never execute.

                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  @Helson

                  You are mixing things now. Please checkout the reference syntax of connect as given in the small example

                  Create a small project and use the sections from the example. This shall give you the general understanding of using signals and slots in Qt. Possibly you can check out also the real example.

                  If your problem is small enough you can also post here the relevant code of your classes.

                  Vote the answer(s) that helped you to solve your issue(s)

                  H 1 Reply Last reply
                  0
                  • K koahnig

                    @Helson

                    You are mixing things now. Please checkout the reference syntax of connect as given in the small example

                    Create a small project and use the sections from the example. This shall give you the general understanding of using signals and slots in Qt. Possibly you can check out also the real example.

                    If your problem is small enough you can also post here the relevant code of your classes.

                    H Offline
                    H Offline
                    Helson
                    wrote on last edited by Helson
                    #9

                    @koahnig

                    File1.cpp 
                    Dlg::Dlg(QWidget *parent, const char *name):QDialog(parent)
                      , mpGridLayout(NULL)
                      , mpCourseComboBox(NULL)
                      , mpTurmaListView(NULL) 
                    {
                        setObjectName(name);
                    	setWindowTitle(tr("Envio de Material"));
                    
                        resize(356, 134);
                    
                    	mpGridLayout = new QGridLayout();
                    
                        setLayout(mpGridLayout);	
                    
                    	mpCourseComboBox = new QComboBox();
                    	mpTurmaListView = new QListWidget();
                    
                    	mpGridLayout->addWidget(mpCourseComboBox, 5, 0, 1, 1);
                    	mpGridLayout->addWidget(mpTurmaListView, 22, 0, 1, 1);
                    	
                    
                    connect(mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(loadValuesListView(const int&))); //Its works fine
                    }
                    
                    File1.cpp
                    void Class1::do()
                    {
                    	QList<QString> myArray;
                    	Dlg dlg;
                    
                    fill(myArray);
                    
                     QObject::connect(dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), &dlg, SLOT(loadValuesListView(const int&)));	 //Compile but the slot never execute...
                    dlg.mpCourseComboBox(otherArray); /This fill combobox with one value
                        if(QDialog::Accepted == dlg.exec())
                        {
                    	 
                        }
                    }
                    

                    I'm new on QT, I need finish this project about 1 week... Sorry for my bad english and thank for you attention.

                    K 1 Reply Last reply
                    0
                    • H Helson

                      @koahnig

                      File1.cpp 
                      Dlg::Dlg(QWidget *parent, const char *name):QDialog(parent)
                        , mpGridLayout(NULL)
                        , mpCourseComboBox(NULL)
                        , mpTurmaListView(NULL) 
                      {
                          setObjectName(name);
                      	setWindowTitle(tr("Envio de Material"));
                      
                          resize(356, 134);
                      
                      	mpGridLayout = new QGridLayout();
                      
                          setLayout(mpGridLayout);	
                      
                      	mpCourseComboBox = new QComboBox();
                      	mpTurmaListView = new QListWidget();
                      
                      	mpGridLayout->addWidget(mpCourseComboBox, 5, 0, 1, 1);
                      	mpGridLayout->addWidget(mpTurmaListView, 22, 0, 1, 1);
                      	
                      
                      connect(mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(loadValuesListView(const int&))); //Its works fine
                      }
                      
                      File1.cpp
                      void Class1::do()
                      {
                      	QList<QString> myArray;
                      	Dlg dlg;
                      
                      fill(myArray);
                      
                       QObject::connect(dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), &dlg, SLOT(loadValuesListView(const int&)));	 //Compile but the slot never execute...
                      dlg.mpCourseComboBox(otherArray); /This fill combobox with one value
                          if(QDialog::Accepted == dlg.exec())
                          {
                      	 
                          }
                      }
                      

                      I'm new on QT, I need finish this project about 1 week... Sorry for my bad english and thank for you attention.

                      K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #10

                      @Helson

                      In principle your code looks reasonable. However, without the include files an essential key part is missing.
                      It is important everything is executed within an event loop. Also classes with signals and slots shall inherit from QObject and the Q_OBJECT has to be in those declarations.
                      QObject provides you also the dumpObjectInfo and dumpObjectTree member functions. Those will give you some important information on connections for your instances to the application output.

                      What strikes me a bit strange is that you have not reported some output that a signal or a slot was not found. That should have happened already at the beginning. Also the connects are providing a return value which can be checked and shall tell you also if a connection was successful.

                      What debugger are you using?

                      Vote the answer(s) that helped you to solve your issue(s)

                      H 1 Reply Last reply
                      0
                      • K koahnig

                        @Helson

                        In principle your code looks reasonable. However, without the include files an essential key part is missing.
                        It is important everything is executed within an event loop. Also classes with signals and slots shall inherit from QObject and the Q_OBJECT has to be in those declarations.
                        QObject provides you also the dumpObjectInfo and dumpObjectTree member functions. Those will give you some important information on connections for your instances to the application output.

                        What strikes me a bit strange is that you have not reported some output that a signal or a slot was not found. That should have happened already at the beginning. Also the connects are providing a return value which can be checked and shall tell you also if a connection was successful.

                        What debugger are you using?

                        H Offline
                        H Offline
                        Helson
                        wrote on last edited by
                        #11

                        @koahnig

                        It's only a logic error!
                        I put the connect after change the add values on the dropdown!
                        I solve this changing the code order.

                        Thank you so much!

                        K 1 Reply Last reply
                        0
                        • H Helson

                          @koahnig

                          It's only a logic error!
                          I put the connect after change the add values on the dropdown!
                          I solve this changing the code order.

                          Thank you so much!

                          K Offline
                          K Offline
                          koahnig
                          wrote on last edited by
                          #12

                          @Helson
                          Glad to hear that you have found your problem.

                          Vote the answer(s) that helped you to solve your issue(s)

                          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