How to get and set QComboBox text



  • 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?


  • Moderators

    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



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



  • [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 :-)



  • 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");@



  • 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’



  • Have you included <QtGui/QLineEdit>?



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



  • The error message

    bq. invalid use of incomplete type ‘struct BlaFaselClass’

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



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

    Thank you all for all of your responses.


Log in to reply
 

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