Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set default text in QcomboBox
QtWS25 Last Chance

How to set default text in QcomboBox

Scheduled Pinned Locked Moved General and Desktop
11 Posts 6 Posters 21.0k Views
  • 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 Offline
    M Offline
    Mashpy
    wrote on last edited by
    #1

    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
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mashpy
        wrote on last edited by
        #3

        This code is not working -

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

        also

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

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            goblincoding
            wrote on last edited by
            #5

            [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
            0
            • D Offline
              D Offline
              DerManu
              wrote on last edited by
              #6

              [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
              0
              • G Offline
                G Offline
                goblincoding
                wrote on last edited by
                #7

                [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
                0
                • M Offline
                  M Offline
                  Mashpy
                  wrote on last edited by
                  #8

                  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
                  0
                  • G Offline
                    G Offline
                    goblincoding
                    wrote on last edited by
                    #9

                    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
                    0
                    • C Offline
                      C Offline
                      ChrisW67
                      wrote on last edited by
                      #10

                      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
                      0
                      • P Offline
                        P Offline
                        poortipant
                        wrote on last edited by
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved