Results 1 to 5 of 5

Thread: Setting default item in a QCombobox

  1. #1
    Join Date
    May 2013
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Setting default item in a QCombobox

    Hello,

    I have a QComboBox that is tied to a model. I want to create a default text placeholder (without making it editable), so I added my placeholder text as the first item in the model. It shows up just fine, but I can't get it to set as the default selection in the QComboBox. At what point can I make the call to setCurrentIndex without the value being overwritten? The technique does work (qcb->setCurrentIndex (qcb->findText ("This is placeholder text..."));, but only if I insert it well after the dialog has been creted and shown... I've tried doing it in the show() slot, but with no success...

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Setting default item in a QCombobox

    If it works when the dialog is shown it might work in showEvent().

    In any case, have you considered setting the placeholder text on the QComboBox line edit?
    See QComboBox::lineEdit() and QLineEdit::setPlaceholderText()

    Cheers,
    _

  3. #3
    Join Date
    May 2013
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Setting default item in a QCombobox

    Unfortunately, setPlaceHolder() works only for editable QComboBoxes. Strangely, if I do it with a non-editable box, I get a seg fault (Qt 4.8.5)...

    ShowEvent() didn't work, either. It get's called before the widgets are shown and updated.

    The only other solution I can come up with is to try refreshing the combo box on a timer triggered in ShowEvent(). It's sloppy, but it may work. I'd *much* rather hook into an event or something, though.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Setting default item in a QCombobox

    The purpose of setCurrentIndex is to change the value, so "without the value being overwritten" makes little sense. Create the combo box, attach its model, and then call setCurrentIndex() (with zero as the argument if you know your placeholder is always the first entry in the model, or use your find if not). Where is the problem?

    If you want something displayed on the combo box when nothing from the model is selected, i.e. currentIndex() returns -1, then that is a different request. I imagine you could do something in the paintEvent() function of a subclass for the case when the QStyleOptionComboBox current text is empty or currentIndex() is -1
    Qt Code:
    1. class ComboBox: public QComboBox
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit ComboBox(QWidget *p = 0): QComboBox(p) {
    6. }
    7. protected:
    8. void paintEvent(QPaintEvent *) {
    9. QStylePainter painter(this);
    10. painter.setPen(palette().color(QPalette::Text));
    11.  
    12. // draw the combobox frame, focusrect and selected etc.
    13. initStyleOption(&opt);
    14. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
    15.  
    16. // draw the icon and text
    17. if (!opt.editable && currentIndex() == -1) // <<< we adjust the text displayed when nothing is selected
    18. opt.currentText = tr("{Select one}");
    19. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
    20. }
    21. };
    To copy to clipboard, switch view to plain text mode 
    The text is displayed but is not returned by currentText(), is not selectable by the user once a valid selection is made, and does not need to appear in your model. You should make the text configurable.

    BTW: It is not strange that a combo box without an editor would return a null pointer when you ask for the editor. If you then proceeded to use the pointer blindly you will crash.
    Last edited by ChrisW67; 13th August 2013 at 22:50.

  5. #5
    Join Date
    May 2013
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Setting default item in a QCombobox

    Still a bit new to Qt - I think I naturally expected the lineEdit to return a valid reference even if the combo box wasn't enabled - a side effect of working ActiveX (typically controls always exist, whether or not they're used)...

    Thanks for the combobox code - that appears to be exactly what I needed!

Similar Threads

  1. How to set Default Text Statement in QComboBox
    By rawfool in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2012, 18:38
  2. Replies: 2
    Last Post: 2nd March 2012, 07:07
  3. Replies: 0
    Last Post: 10th March 2011, 11:44
  4. Setting a widget's QPalette to default?
    By Spectralist in forum Newbie
    Replies: 2
    Last Post: 29th December 2009, 21:47
  5. Setting default icon for all windows / dialogs
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2007, 10:04

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.