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) :confused: ????
Thank you for any advice,
Astronomy
Code:
//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()...
Code:
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:
Code:
void ComaControlFile::on_comboBoxINTYP_currentIndexChanged(int index)
{
cout << "on_comboBoxINTYP_currentIndexChanged(int index): " << index << endl;
}
setCurrentIndex(1) gives:
Quote:
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:
Quote:
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 //???
Re: QComboBox: setCurrentIndex() is not working?
Please provide a minimal compilable example reproducing the problem.
Re: QComboBox: setCurrentIndex() is not working?
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...
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
){
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();
}
Re: QComboBox: setCurrentIndex() is not working?
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-app.../itdepartment/
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.
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
Re: QComboBox: setCurrentIndex() is not working?
Quote:
Originally Posted by
Lykurg
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
Re: QComboBox: setCurrentIndex() is not working?
Hi,
To you select text and display it in currentText, use the code below:
Code:
cb->setCurrentText( cb->findText("TEXT-TO-FIND"));
Code:
if(Ccoma08ctr.Get_INTYP() == "MOD")
{
m_ui->comboBoxINTYP->setCurrentIndex(m_ui->comboBoxINTYP->findText("MOD"));
}
This code will find and display the text.
Re: QComboBox: setCurrentIndex() is not working?
Quote:
Originally Posted by
vcp
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.
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!"
Re: QComboBox: setCurrentIndex() is not working?
Quote:
Originally Posted by
vcp
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.
Re: QComboBox: setCurrentIndex() is not working?
Quote:
Originally Posted by
wysota
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.
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.