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. How can I call a particular function when item in combobox changed?

How can I call a particular function when item in combobox changed?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 7.8k 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.
  • A Offline
    A Offline
    amarism
    wrote on last edited by
    #1

    I have taken a combo Box for zooming the image on clicked and combobox have 4 parameter (100%,200%,400%,800%). So, I want to change the image size on click of item inside the combobox.
    I am try this signal and slot code:-

     connect(ui->mcomboBox, SIGNAL(activated(int)), this, SLOT(Zoom100(int)));
    

    this one working but any item I select item it will forward to same function

    I used another signal and slot but it will also not work

     connect(this->ui->mcomboBox, SIGNAL(currentIndexChanged(const Qstring&)),
    	this, SLOT(zoom100(const Qstring&)));
    
    
    void mainWindow::Zoom100(int index)
    {
    }
    
    void mainWindow::Zoom200(int index)
    {
    }
    
    void mainWindow::Zoom400(int index)
    {
    }
    
    void mainWindow::Zoom800(int index)
    {
    }
    

    I am trying to get every item index value.
    Thank you in advance.

    1 Reply Last reply
    0
    • francescmmF Offline
      francescmmF Offline
      francescmm
      wrote on last edited by francescmm
      #2

      You're using the text in the items of the combo box to zoom in or zoom out, so what you need is not the index rather than the text.

      You've to connect the changed text and create a slot that gets a QString. Here there is an example, not elegant but can help you:

      connect(ui->mcomboBox, SIGNAL(currentTextChanged(QString)), this, SLOT(Zoom(QString)));
      
      void mainWindow::Zoom(const QString &text)
      {
          int theZoomValue = text.split("%").constFirst().toInt();
      }
      

      If what you want is indeed the index of the item selected in the combobox, you need the following connect:

      connect(ui->mcomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Zoom(int)));
      

      Remember that the types of the params must match.

      A 1 Reply Last reply
      0
      • francescmmF francescmm

        You're using the text in the items of the combo box to zoom in or zoom out, so what you need is not the index rather than the text.

        You've to connect the changed text and create a slot that gets a QString. Here there is an example, not elegant but can help you:

        connect(ui->mcomboBox, SIGNAL(currentTextChanged(QString)), this, SLOT(Zoom(QString)));
        
        void mainWindow::Zoom(const QString &text)
        {
            int theZoomValue = text.split("%").constFirst().toInt();
        }
        

        If what you want is indeed the index of the item selected in the combobox, you need the following connect:

        connect(ui->mcomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Zoom(int)));
        

        Remember that the types of the params must match.

        A Offline
        A Offline
        amarism
        wrote on last edited by amarism
        #3

        @francescmm Can i get the index of every item on click. If i will get the index then i will used switch case to solve this problem
        I will try this one but its not working

         connect(ui->mcomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Zoom(int)));
        
        francescmmF A 2 Replies Last reply
        0
        • A amarism

          @francescmm Can i get the index of every item on click. If i will get the index then i will used switch case to solve this problem
          I will try this one but its not working

           connect(ui->mcomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Zoom(int)));
          
          francescmmF Offline
          francescmmF Offline
          francescmm
          wrote on last edited by francescmm
          #4

          @amarism I've just edited the topic for that! I misunderstood your question =)

          You also can user the signal QComboBox::activated(int)

          1 Reply Last reply
          0
          • francescmmF Offline
            francescmmF Offline
            francescmm
            wrote on last edited by francescmm
            #5

            I know it sounds silly, but did you remember to declare the method "void Zoom(int value)" in the public/private slot part of the header file?

            Can you paste your whole code so I can see what's happening easily?

            A 1 Reply Last reply
            0
            • francescmmF francescmm

              I know it sounds silly, but did you remember to declare the method "void Zoom(int value)" in the public/private slot part of the header file?

              Can you paste your whole code so I can see what's happening easily?

              A Offline
              A Offline
              amarism
              wrote on last edited by amarism
              #6

              @francescmm yup i will declare the function in .h file

              private slots:
                      void Zoom100(int index);
              
              1 Reply Last reply
              0
              • francescmmF Offline
                francescmmF Offline
                francescmm
                wrote on last edited by
                #7

                Do it this way!

                // Remember the word 'slots'
                private slots:
                   void Zoom100 (int index);
                
                A 1 Reply Last reply
                0
                • francescmmF francescmm

                  Do it this way!

                  // Remember the word 'slots'
                  private slots:
                     void Zoom100 (int index);
                  
                  A Offline
                  A Offline
                  amarism
                  wrote on last edited by
                  #8

                  @francescmm yes i will taken a slot inside the private part

                  1 Reply Last reply
                  0
                  • A amarism

                    @francescmm Can i get the index of every item on click. If i will get the index then i will used switch case to solve this problem
                    I will try this one but its not working

                     connect(ui->mcomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Zoom(int)));
                    
                    A Offline
                    A Offline
                    amarism
                    wrote on last edited by
                    #9

                    @amarism said in How can I call a particular function when item in combobox changed?:

                    connect(ui->mcomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Zoom(int)));

                    now its working thanx

                    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