Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How to set default text in QcomboBox

    General and Desktop
    6
    11
    18224
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Mashpy last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • C
        ChrisW67 last edited by

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

        1 Reply Last reply Reply Quote 0
        • M
          Mashpy last edited by

          This code is not working -

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

          also

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

          1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators last edited by

            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.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • G
              goblincoding last edited by

              [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.

              http://www.goblincoding.com

              1 Reply Last reply Reply Quote 0
              • D
                DerManu last edited by

                [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.

                1 Reply Last reply Reply Quote 0
                • G
                  goblincoding last edited by

                  [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!

                  http://www.goblincoding.com

                  1 Reply Last reply Reply Quote 0
                  • M
                    Mashpy last edited by

                    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

                    1 Reply Last reply Reply Quote 0
                    • G
                      goblincoding last edited by

                      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.

                      http://www.goblincoding.com

                      1 Reply Last reply Reply Quote 0
                      • C
                        ChrisW67 last edited by

                        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.

                        1 Reply Last reply Reply Quote 0
                        • P
                          poortipant last edited by

                          This post is deleted!
                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post