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?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 8.0k 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 5 Sept 2018, 06:29 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
    • F Offline
      F Offline
      francescmm
      wrote on 5 Sept 2018, 06:32 last edited by francescmm 9 May 2018, 06:38
      #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 5 Sept 2018, 06:37
      0
      • F francescmm
        5 Sept 2018, 06:32

        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 5 Sept 2018, 06:37 last edited by amarism 9 May 2018, 06:40
        #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)));
        
        F A 2 Replies Last reply 5 Sept 2018, 06:37
        0
        • A amarism
          5 Sept 2018, 06:37

          @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)));
          
          F Offline
          F Offline
          francescmm
          wrote on 5 Sept 2018, 06:37 last edited by francescmm 9 May 2018, 06:38
          #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
          • F Offline
            F Offline
            francescmm
            wrote on 5 Sept 2018, 06:42 last edited by francescmm 9 May 2018, 06:43
            #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 5 Sept 2018, 06:43
            0
            • F francescmm
              5 Sept 2018, 06:42

              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 5 Sept 2018, 06:43 last edited by amarism 9 May 2018, 06:53
              #6

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

              private slots:
                      void Zoom100(int index);
              
              1 Reply Last reply
              0
              • F Offline
                F Offline
                francescmm
                wrote on 5 Sept 2018, 06:45 last edited by
                #7

                Do it this way!

                // Remember the word 'slots'
                private slots:
                   void Zoom100 (int index);
                
                A 1 Reply Last reply 5 Sept 2018, 06:55
                0
                • F francescmm
                  5 Sept 2018, 06:45

                  Do it this way!

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

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

                  1 Reply Last reply
                  0
                  • A amarism
                    5 Sept 2018, 06:37

                    @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 5 Sept 2018, 09:24 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

                    1/9

                    5 Sept 2018, 06:29

                    • Login

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