Results 1 to 7 of 7

Thread: Populating, signals and comboboxes

  1. #1
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Populating, signals and comboboxes

    Hello
    I have a signal/slot connected to a combobox

    SIGNAL(currentIndexChanged(QString))
    However, when I initially populate the combobox, it responds to this signal, and calls the slot.
    How can I "switch off" the signaling from the combobox while populating it. I only want it to activate when the index is changed due to a user selecting an entry.

    I looked for an "enabled" property, but couldn't find one, thinking to disable it, populate it, then enable it again.

    Thanks.

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Populating, signals and comboboxes

    Try disconnect(qcombobox...), then populate, and connect(qcombobox...) again.
    Or use QStandardItemModel, add items to the model and setModel on the qcombobox.

  3. #3
    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: Populating, signals and comboboxes

    You can create the connection only after the combobox is populated, or you can block signals or disconnect.

    But my advice is to do the first (populate before connection), it's also a good practice to initialize variables as soon as possible.

  4. #4
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Populating, signals and comboboxes

    Thanks. Did it the first way, works well.

  5. #5
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Populating, signals and comboboxes

    Actually, will do

    comboBox->blockSignals(true)

    then populate, then

    comboBox->blockSignals(false)

    Seems more elegant!

  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: Populating, signals and comboboxes

    For me, the most elegant design is the initialization of that variable (combobox in your case) in the class constructor (before making the connection).
    I gave you more alternatives, for you to choose what is more appropriate for you.

    Like the blockSignals method looks more appropriate if you need to add some entry in combobox at run-time (if user selects/check something... the combobox might have more/less entries).

    Anyway this is just my opinion, you know the design of the rest of your application, and you choose what fits best.

  7. The following 2 users say thank you to Zlatomir for this useful post:

    saa7_go (12th August 2010), ShamusVW (12th August 2010)

  8. #7
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Populating, signals and comboboxes

    Yes, thank you for all your advise.
    You got me going on the right track.

    What I have now done, is not disconnect/connect/block/unblock the signal at all.
    I have set my initial connection in the constructor using

    SIGNAL(activated(QString))

    which responds only to user interaction. Previously I used

    SIGNAL(currentIndexChanged(QString))

    which responds to the combobox changing programmatically as well, i.e. populating it, which I didn't want.

Similar Threads

  1. Mapping between two comboboxes
    By codeman in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2009, 17:32
  2. Replies: 2
    Last Post: 11th August 2009, 16:01
  3. QTableView populating question
    By onefootswill in forum Newbie
    Replies: 8
    Last Post: 7th August 2008, 22:29
  4. size comboboxes
    By mattia in forum Newbie
    Replies: 2
    Last Post: 14th November 2007, 14:55
  5. comboboxes get stuck
    By illuzioner in forum Qt Programming
    Replies: 2
    Last Post: 30th March 2006, 01:03

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.