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. access selected value of QcomboBox problem
Qt 6.11 is out! See what's new in the release blog

access selected value of QcomboBox problem

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.7k 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.
  • I Offline
    I Offline
    isan
    wrote on last edited by isan
    #1

    I set a QcomboBox in gui widget and ,I add item

    for(int i = 1; i < 31; i++)
            {
                ui->combo->addItem(QString::number(i));
            }
    

    and in QComboBox slot I want to get selected value by

    int index =ui->combo->itemData( ui->combo->currentText());
    

    but have error :316: error: no matching function for call to 'QComboBox::itemData(QString)'
    how can get slected value??

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

      Hi,

      You should use currentIndex not currentText. itemData expects an int.

      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
      • I Offline
        I Offline
        isan
        wrote on last edited by
        #3

        addItem get Qstring ,

        void QComboBox::addItem(const QString & text, const QVariant & userData = QVariant())
        

        and when i use current index instead currentText return 0
        so how can I set 1 to 31 number to comboBox and get selected value correctly??

        1 Reply Last reply
        0
        • yuvaramY Offline
          yuvaramY Offline
          yuvaram
          wrote on last edited by
          #4

          Hi,
          By using below code you can get seleted value in Qstring format.
          Widget::Widget(QWidget *parent)
          : QWidget(parent)
          {
          cbox = new QComboBox;
          glayout = new QGridLayout(this);
          glayout->addWidget(cbox);
          QStringList list;
          for(int i=0;i<31;i++){
          list.append(QString::number(i));
          }
          cbox->addItems(list);
          connect(cbox,SIGNAL(currentTextChanged(QString)),this,SLOT(SLTcurrentText(QString)));
          }

          void Widget:: SLTcurrentText(QString str){
          qDebug()<<"CurrentText ::"<<str<<endl;
          }

          Yuvaram Aligeti
          Embedded Qt Developer
          : )

          1 Reply Last reply
          3
          • I Offline
            I Offline
            isan
            wrote on last edited by isan
            #5

            tnx it's work

            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