PDA

View Full Version : QComboBox: setCurrentIndex() is not working?



Astronomy
3rd September 2010, 09:38
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) :confused: ????

Thank you for any advice,
Astronomy




//in the constructor..

m_ui->comboBoxINTYP->addItem("MOD");
m_ui->comboBoxINTYP->addItem("PEL");
m_ui->comboBoxINTYP->addItem("RHO");

// Another try without success..
// m_ui->comboBoxINTYP->insertItem(0,"MOD");
// m_ui->comboBoxINTYP->insertItem(1,"PEL");
// m_ui->comboBoxINTYP->insertItem(2,"RHO");

// Another try without success..
// m_ui->comboBoxINTYP->insertItem(0,"MOD",QVariant("MOD"));
// m_ui->comboBoxINTYP->insertItem(1,"PEL",QVariant("PEL"));
// m_ui->comboBoxINTYP->insertItem(2,"RHO",QVariant("RHO"));



...reading values out data from a file and call setCurrentIndex()...

if(Ccoma08ctr.Get_INTYP() == "MOD")
{
m_ui->comboBoxINTYP->setCurrentIndex(0);
m_ui->comboBoxINTYP->show();
}
else if(Ccoma08ctr.Get_INTYP() == "PEL")
{
m_ui->comboBoxINTYP->setCurrentIndex(1);
m_ui->comboBoxINTYP->show();
}
else if(Ccoma08ctr.Get_INTYP() == "RHO")
{
cout << "index: RHO ist 2:" << endl;
m_ui->comboBoxINTYP->setCurrentIndex(2);
m_ui->comboBoxINTYP->show();
}


looking at:

void ComaControlFile::on_comboBoxINTYP_currentIndexChan ged(int index)
{
cout << "on_comboBoxINTYP_currentIndexChanged(int index): " << index << endl;

}


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 //???

wysota
3rd September 2010, 09:41
Please provide a minimal compilable example reproducing the problem.

Astronomy
3rd September 2010, 10:14
Hi!

AAArrrggghhh i'm going crazy.. :mad:
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...


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);

ui->comboBox->addItem("MOD");
ui->comboBox->addItem("PEL");
ui->comboBox->addItem("RHO");
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
ui->comboBox->setCurrentIndex(2);
ui->comboBox->show();

}

Astronomy
3rd September 2010, 10:41
Grrrr :mad: 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!!!!

http://www.geekstir.com/img/linuxwars.jpg

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

http://www.thinkgeek.com/tshirts-apparel/unisex/itdepartment/

Lykurg
3rd September 2010, 10:45
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.

Astronomy
3rd September 2010, 10:56
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

totem
3rd September 2010, 13:46
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

vcp
3rd September 2010, 14:30
Hi,

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



cb->setCurrentText( cb->findText("TEXT-TO-FIND"));




if(Ccoma08ctr.Get_INTYP() == "MOD")
{
m_ui->comboBoxINTYP->setCurrentIndex(m_ui->comboBoxINTYP->findText("MOD"));
}


This code will find and display the text.

wysota
3rd September 2010, 15:28
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.

vcp
6th September 2010, 14:24
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!"

wysota
6th September 2010, 15:51
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.

hsechovsky
12th August 2015, 20:49
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.

ChrisW67
12th August 2015, 22:51
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.