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 get and set QComboBox text
Forum Updated to NodeBB v4.3 + New Features

How to get and set QComboBox text

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 17.2k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    sayem.bd
    wrote on last edited by
    #1

    Hi, I am an absolute beginner in Qt. I am trying to make a "QComboBox":http://developer.qt.nokia.com/doc/qt-4.7/qcombobox.html which is editable, i.e., users can enter text there manually. Now after doing some searching I learned that QComboBox uses "QLineEdit":http://developer.qt.nokia.com/doc/qt-4.7/qlineedit.html to handle text editing and a pointer to it can be obtained by calling the lineEdit() method of the QComboBox class.

    Now I want to set the current text of the QComboBox. To do that, I am trying to use the following piece of code -

    @QLineEdit *ldt = ui->comboBox->lineEdit();
    ldt->setText("My Text");@

    but it's giving me an error. How can I change the current text of the combo box?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      welcome to the forum.

      You may add items to your QComboBox by using "addItem":http://doc.qt.nokia.com/4.7-snapshot/qcombobox.html#addItem-2

      The currently selected text you can obtain with "currentText":http://doc.qt.nokia.com/4.7-snapshot/qcombobox.html#currentText-prop

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sayem.bd
        wrote on last edited by
        #3

        Thank you :-).

        Yes, I can obtained the current text with currentText, and after obtaining the text I want to append some letters/digits with it, and then set it as the current text. I don't know how can I do that.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          [quote author="sayem.bd" date="1318187650"]I am trying to use the following piece of code -

          @QLineEdit *ldt = ui->comboBox->lineEdit();
          ldt->setText("My Text");@

          but it's giving me an error. How can I change the current text of the combo box?[/quote]

          Could you be a bit more verbose here, please? The actual error message would be quite helpful for an analysis :-)

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sayem.bd
            wrote on last edited by
            #5

            Well well, something really interesting might have happened here before because today I am writing the same code but there is absolutely no errors, and the text is being set properly.

            [quote author="Volker" date="1318196909"]
            Could you be a bit more verbose here, please? The actual error message would be quite helpful for an analysis :-)[/quote]

            Since I couldn't generate the error today, I couldn't give you the exact description, but it was something like @Illegal use of type 'struct QLineEdit'........@ whenever I tried to use this statement -

            @ldt->setText("My Text");@

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sayem.bd
              wrote on last edited by
              #6

              Sorry guys, I am again having the same problem. I don't have a single clue what is happening here. Just a few moments ago I wrote the same code and it worked for me, but now it's again giving me errors. The first error message is as follows -

              bq. mainwindow.cpp:29: error: invalid use of incomplete type ‘struct QLineEdit’

              When I double click on this error message the cursor points to the line -

              @ldt->setText("My Text");@

              and the second error message is -

              bq. qapplication.h:393: error: forward declaration of ‘struct QLineEdit’

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                Have you included <QtGui/QLineEdit>?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sayem.bd
                  wrote on last edited by
                  #8

                  [quote author="Lukas Geyer" date="1318227161"]Have you included <QtGui/QLineEdit>?[/quote]

                  No, I didn't. You are absolutely right, this is what causing the problems. Thank you very much.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    The error message

                    bq. invalid use of incomplete type ‘struct BlaFaselClass’

                    almost always indicates that you are missing the respective include file.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sayem.bd
                      wrote on last edited by
                      #10

                      Volker: Thank you for this info, I will certainly try to remember this from now on........

                      Thank you all for all of your responses.

                      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