Results 1 to 5 of 5

Thread: QComboBox: how to get selected value?

  1. #1
    Join Date
    Apr 2011
    Location
    Russia, Asbest
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QComboBox: how to get selected value?

    Good day! I'm newbie in Qt, please, help me!
    When I was writing my programs on C# 2008, there have been many ways to get the current value of the ComboBox. In Qt, I create QComboBox and insert items without using UI designer.
    Qt Code:
    1. dataExp = new QComboBox();
    2. codeList = new QComboBox();
    3. apiPrivate = new QComboBox();
    4. // items: codeList
    5. codeList->addItem("PHP", "php");
    6. codeList->addItem("JavaScript", "javascript");
    7. // end of items for codeList
    8. //items: dataExp
    9. dataExp->addItem("Never delete", "N");
    10. dataExp->addItem("10 Minutes", "10M");
    11. dataExp->addItem("1 Hour", "1H");
    12. dataExp->addItem("1 Day", "1D");
    13. dataExp->addItem("1 Month", "1M");
    14. // end of items for dataExp
    15. // items: apiPrivate
    16. apiPrivate->addItem("Public", "0");
    17. apiPrivate->addItem("Private", "1");
    18. // end of items for apiPrivate
    To copy to clipboard, switch view to plain text mode 
    My question: now to get value from selected item?
    I try that:
    Qt Code:
    1. private_stat = apiPrivate->itemText(apiPrivate->currentIndex());
    2. prog_lang = codeList->itemText(codeList->currentIndex());
    3. date_expire = dataExp->itemText(dataExp->currentIndex());
    To copy to clipboard, switch view to plain text mode 
    But returned default value
    Tell me, what to do?

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QComboBox: how to get selected value?

    You can use currentText() or you have the currentIndexChanged(...) signal (with int index or QString text parameter)

  3. #3
    Join Date
    Apr 2011
    Location
    Russia, Asbest
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox: how to get selected value?

    I tried so:
    Qt Code:
    1. void qpastebin::IndexChangedCode() {
    2. codeList->setCurrentIndex(codeList->currentIndex());
    3. }
    4. void qpastebin::IndexChangedExpd() {
    5. dataExp->setCurrentIndex(dataExp->currentIndex());
    6. }
    7. void qpastebin::IndexChangedPriv() {
    8. apiPrivate->setCurrentIndex(apiPrivate->currentIndex());
    9. }
    To copy to clipboard, switch view to plain text mode 
    and slots:
    Qt Code:
    1. private_stat = apiPrivate->itemData(apiPrivate->currentIndex()).toString();
    2. prog_lang = codeList->itemData(codeList->currentIndex()).toString();
    3. date_expire = dataExp->itemData(dataExp->currentIndex()).toString();
    4. connect(codeList, SIGNAL(currentIndexChanged(int)), this, SLOT(IndexChangedCode()));
    5. connect(dataExp, SIGNAL(currentIndexChanged(int)), this, SLOT(IndexChangedExpd()));
    6. connect(apiPrivate, SIGNAL(currentIndexChanged(int)), this, SLOT(IndexChangedPriv()));
    To copy to clipboard, switch view to plain text mode 
    But all the same data from QComboBox only those who are selected by default ...
    Please show an example would be very grateful

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QComboBox: how to get selected value?

    Here you go, i just displayed the changing text in a QLabel:
    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QComboBox>
    4. #include <QVBoxLayout>
    5.  
    6. int main(int argc, char** argv) {
    7.  
    8. QApplication a(argc, argv);
    9.  
    10. QVBoxLayout *layout = new QVBoxLayout(&w);
    11. QLabel *label = new QLabel("Here you will see the selected text from ComboBox", &w);
    12. QComboBox *combo = new QComboBox(&w);
    13. layout->addWidget(label);
    14. layout->addWidget(combo);
    15. combo->addItem("First text");
    16. combo->addItem("Secont text");
    17. combo->addItem("Third text");
    18.  
    19. QObject::connect(combo, SIGNAL(currentIndexChanged(QString)), label, SLOT(setText(QString)));
    20.  
    21. w.show();
    22.  
    23. return a.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Zlatomir for this useful post:

    Ivan Khromov (2nd April 2011)

  6. #5
    Join Date
    Apr 2011
    Location
    Russia, Asbest
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox: how to get selected value?

    Thank you! That helped!

Similar Threads

  1. Replies: 2
    Last Post: 28th April 2011, 15:43
  2. QGraphicsItem always selected
    By prashant in forum Qt Programming
    Replies: 2
    Last Post: 29th September 2009, 12:56
  3. Getting the selected row
    By k12yp70n in forum Newbie
    Replies: 4
    Last Post: 4th June 2009, 17:01
  4. How to know the current tab selected
    By Mrdata in forum Newbie
    Replies: 11
    Last Post: 6th February 2007, 20:46
  5. [QT4] Selected row in a QTreeWidget
    By vfernandez in forum Qt Programming
    Replies: 3
    Last Post: 9th March 2006, 12:54

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.