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. Bug with setCurrentText on QComboBox
Forum Updated to NodeBB v4.3 + New Features

Bug with setCurrentText on QComboBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.4k Views 2 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.
  • K Offline
    K Offline
    Kolya
    wrote on last edited by
    #1

    Hi guys,

    I would like to report a potential bug with method setCurrentText on QComboBox.
    I am running Qt Creator 4.8.0 based on Qt 5.12.0 and using Windows 10.

    I have the following implementation

    mLocationComboBox = new QComboBox();
    mLocationComboBox->setEditable(true);
    mLocationComboBox->setMinimumContentsLength(70);
    mLocationComboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
    mLocationComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    
    QStringList recentLocations;
    recentLocations.append(QDir::homePath());
    
    mLocationComboBox->insertItems(0, recentLocations);
    mLocationComboBox->setCurrentIndex(0);
    
    mLocationComboBox->addItem("Here1");
    mLocationComboBox->addItem("Here2");
        
    mLocationComboBox->setCurrentText("Here2");
    auto currentText = mLocationComboBox->currentText(); // "Here2"
    auto currentIndex = mLocationComboBox->currentIndex(); // 0
    

    According to the documentation, currentIndex should be set to index of "Here2" that is 2, while it is set to 0.

    Thank you!

    kshegunovK 1 Reply Last reply
    0
    • K Kolya

      Hi guys,

      I would like to report a potential bug with method setCurrentText on QComboBox.
      I am running Qt Creator 4.8.0 based on Qt 5.12.0 and using Windows 10.

      I have the following implementation

      mLocationComboBox = new QComboBox();
      mLocationComboBox->setEditable(true);
      mLocationComboBox->setMinimumContentsLength(70);
      mLocationComboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
      mLocationComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
      
      QStringList recentLocations;
      recentLocations.append(QDir::homePath());
      
      mLocationComboBox->insertItems(0, recentLocations);
      mLocationComboBox->setCurrentIndex(0);
      
      mLocationComboBox->addItem("Here1");
      mLocationComboBox->addItem("Here2");
          
      mLocationComboBox->setCurrentText("Here2");
      auto currentText = mLocationComboBox->currentText(); // "Here2"
      auto currentIndex = mLocationComboBox->currentIndex(); // 0
      

      According to the documentation, currentIndex should be set to index of "Here2" that is 2, while it is set to 0.

      Thank you!

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      Humor me for a second and run the event loop before you retrieve the index. Something like this:

      mLocationComboBox->setCurrentText("Here2");
      
      QCoreApplication::processEvents();
      
      QString currentText = mLocationComboBox->currentText(); // "Here2"
      int currentIndex = mLocationComboBox->currentIndex(); // 0
      

      What do you get?

      Read and abide by the Qt Code of Conduct

      J.HilkJ 1 Reply Last reply
      0
      • kshegunovK kshegunov

        Humor me for a second and run the event loop before you retrieve the index. Something like this:

        mLocationComboBox->setCurrentText("Here2");
        
        QCoreApplication::processEvents();
        
        QString currentText = mLocationComboBox->currentText(); // "Here2"
        int currentIndex = mLocationComboBox->currentIndex(); // 0
        

        What do you get?

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @kshegunov 😲 don't encourage processEvents() calls!

        mLocationComboBox->setCurrentText("Here2");
        
        QMetaObject::InvokeMethod(this, [=]()->void{
            QString currentText = mLocationComboBox->currentText(); // "Here2"
            int currentIndex = mLocationComboBox->currentIndex(); // 0
        }, Qt::QueuedConnection);
        

        ;-)


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

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

          @J-Hilk @kshegunov

          guys, thank you for the prompt response!
          I did check both suggestions and currentIndex equals to 0.

          J.HilkJ 1 Reply Last reply
          0
          • K Kolya

            @J-Hilk @kshegunov

            guys, thank you for the prompt response!
            I did check both suggestions and currentIndex equals to 0.

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Kolya
            well looking through the code example you posted, that behavior is not unexpected,

            you set the combobox to be editable:

            setEditable(true);

            and according to the docu

            The setter setCurrentText() simply calls setEditText() if the combo box is editable. Otherwise, if there is a matching text in the list, currentIndex is set to the corresponding index.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            3
            • K Offline
              K Offline
              Kolya
              wrote on last edited by
              #6

              @J-Hilk My bad - misinterpreted the documentation :-) Thank you for pointing out!

              1 Reply Last reply
              2
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                @Kolya said in Bug with setCurrentText on QComboBox:

                According to the documentation, currentIndex should be set to index of "Here2" that is 2, while it is set to 0.

                On a side note, you should not expect 2 but 1. In C++ the index are zero based.

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

                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