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. QVector<QComboBox*> signal and slot problem
Forum Update on Monday, May 27th 2025

QVector<QComboBox*> signal and slot problem

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.2k 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.
  • K Offline
    K Offline
    kd188
    wrote on last edited by
    #1

    Hi All,

    Basic Aim - Once the callback function(SLOT) is called i want to know which of the combobox inside my vector is being triggered.

    @QVector<QComboBox* > test = new QComboBox();
    for (int i = 0 ; i< abc.size(); i++)
    {
    ...
    test.at(i)->additem("abc");
    ...

    connect (test[i],SIGNAL(currentIndexChange(QString)),this,SLOT(comboboxChecked(QString))@
    }

    So, basically I have a qvector of comboboxes(test) and i'm adding multiple comboboxes in my GUI, my connect statement also works fine but i'm only able to get the change of the index inside the combobox which the string of the changed index. What I also want to get is the id/index of combobox in which the value is being changed inside my callback function. (So, in short i want to get the counter value of the loop i.e "i" in this case inside my callback function)

    Is there any way of doing this. Please help. Thanks in advance.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Hi kd188
      Welcome to devnet.
      Take a look to "QSignalMapper Class":https://qt-project.org/doc/qt-5/qsignalmapper.html

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Seamus Boyle
        wrote on last edited by
        #3

        You can add the "i" from the for loop as a dynamic property
        @
        comboBox->setProperty("i", i);
        @

        To get the sender combobox from inside the slot:
        @
        if (QComboBox *c = qobject_cast<QComboBox *>(sender())) {
        bool ok;
        int i = c->getProperty("i").toInt(&ok);
        if (ok) {
        qDebug() << "I'm a combo and am feeling ok";
        }
        }
        @

        That would do what you want, but if the slot has access to the QVector you don't need to set the property, just lookup the index using the pointer.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kd188
          wrote on last edited by
          #4

          [quote author="Seamus Boyle" date="1393395066"]You can add the "i" from the for loop as a dynamic property
          @
          comboBox->setProperty("i", i);
          @

          To get the sender combobox from inside the slot:
          @
          if (QComboBox *c = qobject_cast<QComboBox *>(sender())) {
          bool ok;
          int i = c->getProperty("i").toInt(&ok);
          if (ok) {
          qDebug() << "I'm a combo and am feeling ok";
          }
          }
          @

          That would do what you want, but if the slot has access to the QVector you don't need to set the property, just lookup the index using the pointer.[/quote]

          Hi Seamus Boyle,

          Since my signal is currentIndexChange(QString). It's only sending me the QString in my callback function so even though my QVector is global, i would only be able to access just the QString.
          My main purpose is the get the changed Index's String + the number of combobox

          So in my callback function what I want is
          //where String points to the text inside combobox and id is the combobox reference
          @void comboboxChecked(QString test, int id)@

          but currently my callback function is
          @void comboboxChecked(QString test)@

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kd188
            wrote on last edited by
            #5

            [quote author="qxoz" date="1393388801"]Hi kd188
            Welcome to devnet.
            Take a look to "QSignalMapper Class":https://qt-project.org/doc/qt-5/qsignalmapper.html[/quote]

            Hi qxoz,

            Thanks for replying. I have tried to use the signal mapper with my comboboxes like this

            @MyClass::MyClass(QWidget pParent):QWidget(pParent),
            m_window(new QDialog())
            {
            signalMap = new QSignalMapper(this);
            QVector<QComboBox
            > test = new QComboBox();
            createLayout();
            }
            void createLayout()
            {
            for (int i = 0 ; i< abc.size(); i++)
            {
            ...
            bool check;
            check = connect (test[i],SIGNAL(currentIndexChange(QString)),signalMap,SLOT(map());
            signalMap->setMapping(test[i],i);
            ...
            }
            }@

            in this case my check variable is false. So, I guess my first connect statement is not working.

            How should I use signalMapper so that i can get the string changed inside my combobox as well as the id referencing to it?
            In this case what i want to get inside my callback is the "i" variable inside my loop(to refer the index of the combobox) and the QString which is changed inside the combobox.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Seamus Boyle
              wrote on last edited by
              #6

              The qobject_cast of sender() returns a QComboBox. It does not matter what signal you use.

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qxoz
                wrote on last edited by
                #7

                In case not using QSignalMapper (as Seamus Boyle wrote)
                @void comboboxChecked(QString)
                {
                if (QComboBox *c = qobject_cast<QComboBox *>(sender()))
                {
                //now you got combo box that emit signal. And you can get any data you want
                ... = c->currentIndex();
                ... = c->lineEdit->text();
                }
                }@

                in case using QSignalMapper
                @
                void createLayout()
                {
                bool check;
                for (int i = 0 ; i< abc.size(); i++)
                {
                ...
                check = connect (test[i],SIGNAL(currentIndexChange(QString)),signalMap,SLOT(map());
                signalMap->setMapping(test[i], i);
                ...
                }
                connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(some_custom_slot(int)));
                }@
                then in slot
                @void some_custom_slot(int id)
                {
                if (QComboBox *c = qobject_cast<QComboBox *>(signalMapper->mapping(id)))
                {
                //now you got combo box that emit signal. And you can get any data you want
                ... = c->currentIndex();
                ... = c->lineEdit->text();
                }
                }
                @

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kd188
                  wrote on last edited by
                  #8

                  Hi qxoz, Seamus

                  Thanks for the help. I got the point now. :) Thanks!

                  1 Reply Last reply
                  0

                  • Login

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