Results 1 to 13 of 13

Thread: QComboBox: setCurrentIndex() is not working?

  1. #1
    Join Date
    May 2009
    Location
    Vienna
    Posts
    91
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QComboBox: setCurrentIndex() is not working?

    Hello,

    Can i simply select strings with setCurrentIndex(int...) from a QComboBox and ->show()? or do i have to add a QStandardItemModel item?

    In a widget i have a QComboBox with 3 strings at position 0,1,2
    when i want to select a string, the ComboBox remains at item in position 0 := "MOD"

    I found out that somewhat triggers the signal currentIndexChanged() 2 or 3 times, depending of setCurrentIndex(1) or setCurrentIndex(2) ????

    Thank you for any advice,
    Astronomy


    Qt Code:
    1. //in the constructor..
    2.  
    3. m_ui->comboBoxINTYP->addItem("MOD");
    4. m_ui->comboBoxINTYP->addItem("PEL");
    5. m_ui->comboBoxINTYP->addItem("RHO");
    6.  
    7. // Another try without success..
    8. // m_ui->comboBoxINTYP->insertItem(0,"MOD");
    9. // m_ui->comboBoxINTYP->insertItem(1,"PEL");
    10. // m_ui->comboBoxINTYP->insertItem(2,"RHO");
    11.  
    12. // Another try without success..
    13. // m_ui->comboBoxINTYP->insertItem(0,"MOD",QVariant("MOD"));
    14. // m_ui->comboBoxINTYP->insertItem(1,"PEL",QVariant("PEL"));
    15. // m_ui->comboBoxINTYP->insertItem(2,"RHO",QVariant("RHO"));
    To copy to clipboard, switch view to plain text mode 


    ...reading values out data from a file and call setCurrentIndex()...
    Qt Code:
    1. if(Ccoma08ctr.Get_INTYP() == "MOD")
    2. {
    3. m_ui->comboBoxINTYP->setCurrentIndex(0);
    4. m_ui->comboBoxINTYP->show();
    5. }
    6. else if(Ccoma08ctr.Get_INTYP() == "PEL")
    7. {
    8. m_ui->comboBoxINTYP->setCurrentIndex(1);
    9. m_ui->comboBoxINTYP->show();
    10. }
    11. else if(Ccoma08ctr.Get_INTYP() == "RHO")
    12. {
    13. cout << "index: RHO ist 2:" << endl;
    14. m_ui->comboBoxINTYP->setCurrentIndex(2);
    15. m_ui->comboBoxINTYP->show();
    16. }
    To copy to clipboard, switch view to plain text mode 

    looking at:
    Qt Code:
    1. void ComaControlFile::on_comboBoxINTYP_currentIndexChanged(int index)
    2. {
    3. cout << "on_comboBoxINTYP_currentIndexChanged(int index): " << index << endl;
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    setCurrentIndex(1) gives:
    on_comboBoxINTYP_currentIndexChanged(int index): 0 //ok this is the constructor
    index: PEL ist 1:
    on_comboBoxINTYP_currentIndexChanged(int index): 1 //this is me.. ok
    on_comboBoxINTYP_currentIndexChanged(int index): 0 //????
    setCurrentIndex(2) gives:
    on_comboBoxINTYP_currentIndexChanged(int index): 0 //ok this is the constructor
    index: RHO ist 2:
    on_comboBoxINTYP_currentIndexChanged(int index): 2 //this is me.. ok
    on_comboBoxINTYP_currentIndexChanged(int index): 1 //???
    on_comboBoxINTYP_currentIndexChanged(int index): 0 //???

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QComboBox: setCurrentIndex() is not working?

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2009
    Location
    Vienna
    Posts
    91
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox: setCurrentIndex() is not working?

    Hi!

    AAArrrggghhh i'm going crazy..
    I just created a test case in a new QT-Project... and there it works.. so i have to find out the different "of a drag drop from the designer " to my app..
    Maybe..., i have the QComboBox in a widget, which is embedded into another widget in a QScrollArea... so that this makes problems?
    i'll look again.

    regards Astronomy

    working test case with a Button that simulates setCurrentIndex(), which is similar to my if(..)else{..}app...
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8.  
    9. ui->comboBox->addItem("MOD");
    10. ui->comboBox->addItem("PEL");
    11. ui->comboBox->addItem("RHO");
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::on_pushButton_clicked()
    20. {
    21. ui->comboBox->setCurrentIndex(2);
    22. ui->comboBox->show();
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2009
    Location
    Vienna
    Posts
    91
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox: setCurrentIndex() is not working?

    Grrrr there was some problem with the QT Designer.
    I've Added just a second QCombobox with another name, added all function calls to it.. and it's working like it has to be!

    But Thank you very much for your offer to check my codesnipet.
    I would not had the idea to add a 2nd QCombobox

    thanx Astronomy

    PS:
    well, all guys out there, you know..
    you are coding... you believe the code has to work, the approach looks quite easy... and nope, you spend hours at the Docu, the Forum, see that other forums-code snipes that are working.. you are asking yourself, if you should post.. *g*

    But that's the way it gonna be, have a nice day!
    and!!!!



    i think i've found the T-Shirt here...

    http://www.thinkgeek.com/tshirts-app.../itdepartment/

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QComboBox: setCurrentIndex() is not working?

    I think the problem was that with a slot name like "on_comboBoxINTYP_currentIndexChanged" Qt makes an auto connection to it and you probably have made this connection again.

  6. #6
    Join Date
    May 2009
    Location
    Vienna
    Posts
    91
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox: setCurrentIndex() is not working?

    Hmm... possible
    but i'm sure the only "connection" i've done, was my update Button which triggered setcurrentindex() (-:

    But i'm glad it was found.
    greetz Astronomy

  7. #7
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox: setCurrentIndex() is not working?

    Quote Originally Posted by Lykurg View Post
    I think the problem was that with a slot name like "on_comboBoxINTYP_currentIndexChanged" Qt makes an auto connection to it and you probably have made this connection again.
    Never heard of this Qt feature before, thank you for pointing this out

  8. #8
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Thanks
    5
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox: setCurrentIndex() is not working?

    Hi,

    To you select text and display it in currentText, use the code below:

    Qt Code:
    1. cb->setCurrentText( cb->findText("TEXT-TO-FIND"));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. if(Ccoma08ctr.Get_INTYP() == "MOD")
    2. {
    3. m_ui->comboBoxINTYP->setCurrentIndex(m_ui->comboBoxINTYP->findText("MOD"));
    4. }
    To copy to clipboard, switch view to plain text mode 

    This code will find and display the text.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QComboBox: setCurrentIndex() is not working?

    Quote Originally Posted by vcp View Post
    This code will find and display the text.
    Not very efficient way of doing that. Especially if you have >1000 items and the one you're looking for is the last one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Thanks
    5
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox: setCurrentIndex() is not working?

    I agree, but from the standpoint of someone who needs to implement a solution(especially if it depends on your employment, for example.) ,
    it will work. Giving space to think of a solution more charm.
    That is, "Doing work first!"

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QComboBox: setCurrentIndex() is not working?

    Quote Originally Posted by vcp View Post
    I agree, but from the standpoint of someone who needs to implement a solution(especially if it depends on your employment, for example.) ,
    it will work. Giving space to think of a solution more charm.
    That is, "Doing work first!"
    Yes... I have spent really lot of time at work correcting things after people with attitude similar to yours.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Apr 2015
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox: setCurrentIndex() is not working?

    Quote Originally Posted by wysota View Post
    Yes... I have spent really lot of time at work correcting things after people with attitude similar to yours.
    I hope you accept my question related to your disscussion with a time distance. The solution suggested by vcp is very important for the case, when you want to use combobox for update of the item in a database record. Very often case, when you need to limit the choice of data values by combobox and want to have a single field for the update of the item. It is one of the most frequent and needed cases in db applications. I think vcp found relatively good solution. When you are so sceptic to it, please can you advice more efficient and simpler solution? Thanks in advance for you reply, Hynek.

  13. #13
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QComboBox: setCurrentIndex() is not working?

    If there are only a few fixed items then that approach, or the OP's if-else cascade is quite fine.

    If the combobox contains 10000 items then a linear search is probably not the quickest option. The more times you have to do this search the more noticeable/significant the slowdown may become. A list like that would likely have to be sorted in order to be usable by a human, and there are faster ways to search in a sorted list (binary search for example). For an unsorted list it may be an option to keep a separate hash map of text to index. As always there is a set of tradeoffs that are partly application specific.

Similar Threads

  1. QComboBox::insertSeparator(int index) is not working
    By osiris81 in forum Qt Programming
    Replies: 2
    Last Post: 14th July 2011, 18:43
  2. breakpoint inside QComboBox subclass not working
    By yodasoda in forum Qt Programming
    Replies: 6
    Last Post: 4th June 2010, 17:02
  3. QComboBox
    By bismitapadhy in forum Qt Programming
    Replies: 7
    Last Post: 15th July 2009, 06:01
  4. setCurrentIndex() doesn't display cursor(bounding rect)
    By DIMEDROLL in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2008, 06:24
  5. Replies: 1
    Last Post: 13th June 2008, 18:58

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.