Results 1 to 4 of 4

Thread: access selected value of QcomboBox has problem

  1. #1
    Join Date
    Feb 2016
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Question access selected value of QcomboBox has problem

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

    Qt Code:
    1. for(int i = 1; i < 31; i++)
    2. {
    3. ui->combo->addItem(QString::number(i));
    4. }
    To copy to clipboard, switch view to plain text mode 
    and in QComboBox slot I want to get selected value by

    Qt Code:
    1. int index =ui->combo->itemData( ui->combo->currentText());
    To copy to clipboard, switch view to plain text mode 
    but have error :316: error: no matching function for call to 'QComboBox::itemData(QString)'

    if I use currentIndex instead of currentText return 0 when print it; addItem get Qstring ,

    Qt Code:
    1. void QComboBox::addItem(const QString & text, const QVariant & userData = QVariant())
    To copy to clipboard, switch view to plain text mode 
    and ItemData work with currentIndex,

    I use insertItem and it has sae error ,so how can set value or text and get slected value??
    Last edited by isan; 26th April 2016 at 07:38.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: access selected value of QcomboBox has problem

    1. You have to set the item value :
    Qt Code:
    1. for(int i = 1; i < 31; i++)
    2. {
    3. ui->combo->addItem(QString::number(i),i);
    4. }
    To copy to clipboard, switch view to plain text mode 
    2. To get selected item value :
    Qt Code:
    1. int value = ui->combo->itemValue(ui->combo->currentIndex());
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2016
    Posts
    16
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: access selected value of QcomboBox has problem

    By using below code I can get seleted value in Qstring format.
    Qt Code:
    1. Widget::Widget(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. cbox = new QComboBox;
    5. glayout = new QGridLayout(this);
    6. glayout->addWidget(cbox);
    7. for(int i=0;i<31;i++){
    8. list.append(QString::number(i));
    9. }
    10. cbox->addItems(list);
    11. connect(cbox,SIGNAL(currentTextChanged(QString)),this,SLOT(SLTcurrentText(QString)));
    12. }
    13.  
    14. void Widget:: SLTcurrentText(QString str){
    15. qDebug()<<"CurrentText ::"<<str<<endl;
    16. }
    To copy to clipboard, switch view to plain text mode 
    I should use SIGNAL(currentTextChanged(QString)
    tnx..

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: access selected value of QcomboBox has problem

    That's also an option since your text is just a number.

    Lesiok answered your original question on how to use per-item data.

    Cheers,
    _

Similar Threads

  1. Replies: 5
    Last Post: 17th February 2015, 23:21
  2. Replies: 0
    Last Post: 31st October 2011, 14:11
  3. Replies: 0
    Last Post: 1st September 2011, 15:20
  4. Replies: 2
    Last Post: 28th April 2011, 14:43
  5. QComboBox: how to get selected value?
    By Ivan Khromov in forum Newbie
    Replies: 4
    Last Post: 2nd April 2011, 13:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.