How to set default text in QcomboBox



  • How to set a default text to QComboBox. For Eg., "Select an option", as shown below

    Before :

    |Select a Catagory |

    On Click :

    |Option 1 |
    |Option 2 |
    |Option 3 |

    "Select a Catagory" will not return any value.



  • Seems this should work
    @
    myCombo->lineEdit()->setPlaceHolderText("Some text");
    @



  • This code is not working -

    @ ui->comboBox->lineEdit()->setPlaceHolderText("Some text");@

    also

    @ ui->comboBox->lineEdit->setPlaceHolderText("Some text");@


  • Moderators

    only editable combobox es have lineedits.
    You just have to insert an additional item in your combo box with your default text and handle it's value accordingly in your application logic.



  • [quote]
    myCombo->lineEdit()->setPlaceHolderText("Some text");
    [/quote]

    This won't work, lineEdit() returns const (from the docs): lineEdit() const : QLineEdit *

    However, you could set a new QLineEdit:

    @
    QLineEdit* lineEdit = new QLineEdit;
    lineEdit->setPlaceHolderText( "Your text" );
    yourCombo->setLineEdit( lineEdit ); // takes ownership
    @

    However, as raven-worx mentioned, only editable combo boxes have line edits, so I agree that your best bet may simply be to cater for your placeholder text in a relevant slot.



  • [quote author="goblincoding" date="1366955513"]This won't work, lineEdit() returns const (from the docs): lineEdit() const : QLineEdit *
    [/quote]

    No, that const means that ::lineEdit() doesn't modify members of QComboBox. It has nothing to do with the return value, the returned QLineEdit can be modified.



  • [quote author="DerManu" date="1366956766"][quote author="goblincoding" date="1366955513"]This won't work, lineEdit() returns const (from the docs): lineEdit() const : QLineEdit *
    [/quote]

    No, that const means that ::lineEdit() doesn't modify members of QComboBox. It has nothing to do with the return value, the returned QLineEdit can be modified.
    [/quote]

    Oh crap, my bad...apologies (sorry Chris)...I see now that I misunderstood the documentation layout I quoted, didn't realise that

    lineEdit() const : QLineEdit *

    and

    QLineEdit * QComboBox::lineEdit() const

    is equivalent. Thanks for the correction!



  • goblincoding @

    @ QLineEdit* lineEdit = new QLineEdit;
    lineEdit->setPlaceHolderText( "Your text" );
    yourCombo->setLineEdit( lineEdit ); @

    your code is not working.

    this is my project source code.
    https://github.com/Mashpy/TechnologyBasic-Software

    and please put this database in your localhost -
    http://technologybasic.com/desktop-apps/Database.sql.gz



  • I'm sorry Mashpy, but I don't have time to look at that right now.

    Did you note the previous posts? I misinterpreted the doc segment I quoted (i.e. my previous post was incorrect, please see the correction by DerManu) so unless your combo box is editable, it won't help adding a line edit (or editing the existing one).

    I still think that you should consider raven-worx's suggestion.



  • bq. your code is not working.

    Define "not working." Not compiling? Not linking? Not functioning as expected? How is it functioning? Is your QComboBox editable or not?

    BTW: It's
    @
    myCombo->lineEdit()->setPlaceholderText("Some text"); // No capital H on holder
    @
    Typo was my fault. As raven-worx says, this will only work on editable combo boxes. The alternative is as raven-worx suggests.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.