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. [Solved] Check if value is part of the list in qcombobox
Forum Updated to NodeBB v4.3 + New Features

[Solved] Check if value is part of the list in qcombobox

Scheduled Pinned Locked Moved General and Desktop
qcombobox
5 Posts 3 Posters 15.0k Views 3 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.
  • M Offline
    M Offline
    Mr. Kibu
    wrote on 15 Feb 2016, 14:19 last edited by Mr. Kibu
    #1

    Hi,

    I have got an editable qcombobox that gets the listbox from a related QSqlRelationalTableModel.
    Now I want to check if the entered value is part of the list after the combobox looses the focus.

    So I installed the eventfilter like this:

    ui->cbVoucherPosItemKey->installEventFilter(this);
    

    And then I catch the focusout-event of the combobox:

    bool BVoucherPosForm::eventFilter(QObject *object, QEvent *event)
    {
        if (event->type() == QEvent::FocusOut)
        {
            if (object == ui->cbVoucherPosItemKey)
            {
                qDebug() << "ComboIndex: "<<ui->cbVoucherPosItemKey->findText(ui->cbVoucherPosItemKey->currentText());
            }
        }
        return false;
    }
    

    I want to get -1 if the value is not part of the list. But I get also -1 if I write the first letters of a string that is in the list (because it is an editable combobox, the value of the list is autocomplete) and push the TAB-button to move the focus (e.g. after typing "abc", the combobox is selecting the string "abcde" in the list, but I get -1 in my example above).

    If I type the correct String in the combobox (e.g. "abcde"), I get the right index in my qDebug message. So I want to get the right index when I type "abc" too.

    My question:

    -- Is there a possibility to wait in the focusout-event for the finished selection in the combobox ??

    or

    -- Is there a smarter way???

    Thank you!

    1 Reply Last reply
    1
    • M Offline
      M Offline
      Mr. Kibu
      wrote on 15 Feb 2016, 20:42 last edited by
      #2

      I found out, that

      qDebug()<<"item: "<<ui->cbVoucherPosItemKey->currentText();
      

      retourns me the right string, when I copy the code to the place in the eventFilter-function where the other qDebug code is standing.
      The problem in my example is, that the values of the list are beginning with capital letters (like "Abcde"). So, how do I use combobox::findtext without case-sensitiv??

      Thank you!

      T 1 Reply Last reply 15 Feb 2016, 21:55
      0
      • M Mr. Kibu
        15 Feb 2016, 20:42

        I found out, that

        qDebug()<<"item: "<<ui->cbVoucherPosItemKey->currentText();
        

        retourns me the right string, when I copy the code to the place in the eventFilter-function where the other qDebug code is standing.
        The problem in my example is, that the values of the list are beginning with capital letters (like "Abcde"). So, how do I use combobox::findtext without case-sensitiv??

        Thank you!

        T Offline
        T Offline
        the_
        wrote on 15 Feb 2016, 21:55 last edited by
        #3

        @Mr.-Kibu

        Hi,

        as you already found out, QComboBox has a method called findText , The second parameter holds the Flags for matching. Default is Qt::MatchExactly | Qt::MatchCaseSensitive. Flags that can be set are defined here:
        http://doc.qt.io/qt-5.5/qt.html#MatchFlag-enum

        So for example if you have a ComboBox that contains "Level" as Text

        qDebug() << combobox->findText("level"); //prints -1, not found
        qDebug() << combobox->findText("level",Qt::MatchContains); //prints the first index that contains "level" 
        

        Hope that helps

        -- No support in PM --

        1 Reply Last reply
        2
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Feb 2016, 22:38 last edited by
          #4

          Hi,

          Isn't QCompleter what you are looking for ?

          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
          2
          • M Offline
            M Offline
            Mr. Kibu
            wrote on 16 Feb 2016, 08:53 last edited by
            #5

            Thank you to the_ and SGaist!

            I realised it like the_ explained. In my case it is the easiest way and it works (in a combobox with a list of values from a related table, I want to know if the entered string is part of the list and if not (index = -1) I open a dialog to add a new value in the related table).
            But I admit, QCompleter is also a smart solution. Maybe in the future...

            1 Reply Last reply
            0

            1/5

            15 Feb 2016, 14:19

            • Login

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