Results 1 to 9 of 9

Thread: QComboBox values?

  1. #1
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QComboBox values?

    Hi

    Using a QComboBox to control values in my application. I have a number of these to set various parameters. For examples 1 have a combobox with values 7,9,11,3,15. I want to send these values to my app class. However only the index is sent, i.e for the first value 7 then value 0 . What is the best way of sending the actual comboxbox value instead of the index to my app class

    Best Regards

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QComboBox values?

    I suppose you are using a signal with "int index" parameter. Use one with "QString text" parameter instead and convert it to an integer.
    J-P Nurmi

  3. #3
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox values?

    Thanks

    Yes I am using signal currentIndexChanged. I could use the overloaded currentIndexChanged(QString) but this sends the QString. I want to pass an integer to my applications member function

    Regards
    Last edited by dbrmik; 7th January 2009 at 14:24.

  4. #4
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QComboBox values?

    That's the way it works. The signal currentIndexChanged(int) passes the index to the slot, currentIndexChanged(QString) passes the contents of the combobox at that index as a string. If you want to call a method that takes an int, convert the string with QString::toInt first.

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox values?

    In slot connected to QComboBox::currentIndexChanged(int) call QComboBox::itemData method.

  6. #6
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox values?

    Thanks everyone.

    But I want seperation between my application class and gui class, so I dont want to call any Qt functions from my application class. The bottom line is I dont know how to intercept the signal currentIndexChanged in order to convert my Qstring to an integer. Do I need to implement a new signal?

    Thanks

  7. #7
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QComboBox values?

    In that case you could connect the currentIndexChanged signal to a slot in your GUI class, convert the QString to an int and call the method from your application class. Of course, instead of calling the method directly you could also define and emit a custom signal which you connect to the slot from your application class. You can find an example in the Qt documentation.

    The question is, why don't you want any Qt code in your application class? If that class has a slot, you have Qt code already in there.

  8. The following user says thank you to rexi for this useful post:

    dbrmik (9th January 2009)

  9. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox values?

    Why dont u try as rexi said - convert QString to int in your slot ?

    Still if u want a slot in ur application class with int parameter, then have a look at using QSignalMapper, though this will be lengthy way !

  10. #9
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox values?

    Thanks Rexi, a custom slot in my GUI class to perform the conversion QString to int , then emit the signal to my application class

    Thanks

Similar Threads

  1. Editable QComboBox with QItemDelegate
    By Jmgr in forum Qt Programming
    Replies: 11
    Last Post: 10th December 2008, 09:21
  2. QDataWidgetMapper and QCombobox
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2008, 17:53
  3. QComboBox - Few virtual methods
    By gruszczy in forum Qt Programming
    Replies: 17
    Last Post: 16th July 2008, 16:08
  4. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19:25
  5. using QComboBox as an ItemView
    By EricTheFruitbat in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2007, 16:14

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.