Results 1 to 7 of 7

Thread: recuperate index item selected in comboBox

  1. #1
    Join Date
    Dec 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default recuperate index item selected in comboBox

    Hi , i am newbie here
    i added new comboBox to my gui like that
    Qt Code:
    1. QComboBox *list;
    2. list=new QComboBox(dockWidgetContents_8);
    3. list->setObjectName(QString::fromUtf8("list"));
    4. list->setGeometry(QRect(0, 40, 69, 22));
    5. list->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    where i created my model and i add to my list like that
    Qt Code:
    1. void MainWindow::createModel()
    2. {
    3. model = new QStandardItemModel( 0,0,this );
    4. model->setHorizontalHeaderLabels( QStringList() << "liste" );
    5. }
    6.  
    7. void MainWindow::createModelItems()
    8. {
    9. QStringList choiceList, optionList;
    10. QStandardItem *pItem;
    11.  
    12.  
    13.  
    14. model->appendRow( pItem );
    15. }
    16. void MainWindow::itemAded( QStandardItem *pItem )
    17. {
    18. model->appendRow( pItem );
    19. }
    To copy to clipboard, switch view to plain text mode 
    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

  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: recuperate index item selected in comboBox

    QComboBox has 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:
    Qt Code:
    1. void MainWindow::createModelItems()
    2. {
    3. QStringList choiceList, optionList;
    4. QStandardItem *pItem;
    5.  
    6. //don't forget to initialize the pItem pointer here
    7.  
    8. model->appendRow( pItem );
    9. }
    To copy to clipboard, switch view to plain text mode 
    And QComboBox also has 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)

  3. #3
    Join Date
    Dec 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recuperate index item selected in comboBox

    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
    Qt Code:
    1. connect(this,SIGNAL( currentIndexChanged(int)),my_class ,SLOT(addwidget(const Vector3 &)));
    To copy to clipboard, switch view to plain text mode 
    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))
    Last edited by johan07; 25th December 2012 at 19:17.

  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: recuperate index item selected in comboBox

    You didn't read the documentation for signals and slots, 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).

  5. #5
    Join Date
    Dec 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recuperate index item selected in comboBox

    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 ?
    Last edited by johan07; 25th December 2012 at 23:14.

  6. #6
    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: recuperate index item selected in comboBox

    Quote Originally Posted by johan07 View Post
    ...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 or maybe QListWidget 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.

  7. #7
    Join Date
    Dec 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recuperate index item selected in comboBox

    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

Similar Threads

  1. Replies: 1
    Last Post: 7th August 2012, 12:41
  2. Replies: 2
    Last Post: 4th December 2010, 08:09
  3. How to get index of a combobox delegate selection
    By vieraci in forum Qt Programming
    Replies: 12
    Last Post: 21st July 2009, 16:37
  4. ComboBox Completer Index Problem
    By majatu in forum Qt Programming
    Replies: 3
    Last Post: 7th June 2009, 14:30
  5. NUll character output for combobox index
    By mdskpr778 in forum Qt Programming
    Replies: 2
    Last Post: 8th October 2008, 18:22

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.