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

[solved] Combobox UserRole ?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.3k Views 1 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.
  • D Offline
    D Offline
    deleted28
    wrote on last edited by
    #1

    How can i get the enum values of Parity ?
    I expect to get 0, 2 and 3
    Problem is, i do not understand the USerrole stuff.

    @ ui->cb_test->addItem("NO", QSerialPort::NoParity);
    ui->cb_test->addItem("EVEN", QSerialPort::EvenParity);
    ui->cb_test->addItem("ODD", QSerialPort::OddParity);@

    @void MainWindow::on_cb_test_currentIndexChanged(int index)
    {
    qDebug() << index;
    qDebug() << ui->cb_test->itemData(index, 0);
    }@

    thank you

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

      Hi,

      @qDebug() << ui->cb_test->itemData(index, 0);@

      You are using the wrong role here. By default itemData gets the data from Qt::UserRole so what you put in using addItem. Here you are requesting the data from Qt::DisplayRole

      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
      0
      • D Offline
        D Offline
        deleted28
        wrote on last edited by
        #3

        do you have a suggestion how i can solve my problem ?

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

          @qDebug() << ui->cb_test->itemData(index);@

          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
          0
          • D Offline
            D Offline
            deleted28
            wrote on last edited by
            #5

            the output of the above is:
            @QVariant(int, 0)
            QVariant(int, 2)
            QVariant(int, 3) @

            I want to use this combobox to set QSerialPort parity:

            @myQSerielPort->setParity(ui->cb_test->itemData(index); @

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

              And that's the correct output.

              If you want to retrieve the value you have to convert the QVariant back to your enum

              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
              0
              • D Offline
                D Offline
                deleted28
                wrote on last edited by
                #7

                Ahhhhh :), OK thank you

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

                  You're welcome,

                  Since you have everything working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

                  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
                  0

                  • Login

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