PDA

View Full Version : recuperate index item selected in comboBox



johan07
23rd December 2012, 12:54
Hi , i am newbie here
i added new comboBox to my gui like that


QComboBox *list;
list=new QComboBox(dockWidgetContents_8);
list->setObjectName(QString::fromUtf8("list"));
list->setGeometry(QRect(0, 40, 69, 22));
list->setModel(model);

where i created my model and i add to my list like that


void MainWindow::createModel()
{
model = new QStandardItemModel( 0,0,this );
model->setHorizontalHeaderLabels( QStringList() << "liste" );
}

void MainWindow::createModelItems()
{
QStringList choiceList, optionList;
QStandardItem *pItem;



model->appendRow( pItem );
}
void MainWindow::itemAded( QStandardItem *pItem )
{
model->appendRow( pItem );
}

i have no problem with that, i can add easy in my combobox
the problem that now i want when i select (or clic ) on item i send the information of this item (its index or it's name) to another function , i have no problem with the slot , my problem is in the signal how i recuperate the index of the ittem selected

Zlatomir
23rd December 2012, 14:51
QComboBox has currentIndexChanged (http://qt-project.org/doc/qt-4.8/qcombobox.html#currentIndexChanged) signals that can send you both index or text. So connect a slot to one of those signals.

//not related to your problem, this can be a stripped version of your code, but i just want to point it out:


void MainWindow::createModelItems()
{
QStringList choiceList, optionList;
QStandardItem *pItem;

//don't forget to initialize the pItem pointer here

model->appendRow( pItem );
}

And QComboBox also has addItem (http://qt-project.org/doc/qt-4.8/qcombobox.html#addItem) and addItems member functions - the usage of a model there might complicate your design - especially if the combobox is "static" (created once and not modified during run-time)

johan07
25th December 2012, 20:08
QComboBox has currentIndexChanged signals that can send you both index or text. So connect a slot to one of those signals.
hi , i tried it but it does not work
i declared addwidget(const Vector3 &) as slot in my class "my_class"
and in my code i made ike that

connect(this,SIGNAL( currentIndexChanged(int)),my_class ,SLOT(addwidget(const Vector3 &)));
BUT when i select an item in the combobox nothing it's happend , it does not enter to the addwidget(note: i did not make any code in currentIndexChanged(int))

Zlatomir
25th December 2012, 21:28
You didn't read the documentation for signals and slots (http://doc.qt.digia.com/qt/signalsandslots.html), please do so.

But lets take this problem logically: how is Qt (or anyone else) supposed to call your slot with a const Vector3&? From where to take that reference?
Simple answer: It can't, so what you have to do is: create a slot that will have an int (the index) as parameter and based on that index you call (or code) your functionality that can take the Vector3 reference (that correspond with the index) from whatever place you stored it... (or similar with the version that passes the QString, if that fits your needs better).

johan07
25th December 2012, 23:33
hi , thanks , i forget that
it work now but there is somthing not good

if we want select two or three items in one time , the signal currentIndexChanged(int) , do not work ?

Zlatomir
26th December 2012, 00:59
...if we want select two or three items in one time , the signal currentIndexChanged(int) , do not work ?
I don't understand your question - the combobox can select only one item - if you want multiple selection you can take a look at QListView (http://doc.qt.digia.com/qt/qlistview.html) or maybe QListWidget (http://doc.qt.digia.com/qt/qlistwidget.html) those show more than one item, the selection model can be changed so that user can select multiple items and also have functionality to get to the items that are selected.

johan07
26th December 2012, 19:52
hi,
ok , that man with combobox we can't drag the mouse to select more than one itme
i will see what i must to do behind this problem