October 9, 2011

sayem.bd sayem.bd
Lab Rat
33 posts

How to get and set QComboBox text

 

Hi, I am an absolute beginner in Qt. I am trying to make a QComboBox [developer.qt.nokia.com] which is editable, i.e., users can enter text there manually. Now after doing some searching I learned that QComboBox uses QLineEdit [developer.qt.nokia.com] 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 –

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

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

9 replies

October 9, 2011

koahnig koahnig
Mad Scientist
2112 posts

welcome to the forum.

You may add items to your QComboBox by using addItem [doc.qt.nokia.com]

The currently selected text you can obtain with currentText [doc.qt.nokia.com]

October 9, 2011

sayem.bd sayem.bd
Lab Rat
33 posts

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.

October 9, 2011

Volker Volker
Robot Herder
5428 posts

sayem.bd wrote:
I am trying to use the following piece of code –

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

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

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

October 10, 2011

sayem.bd sayem.bd
Lab Rat
33 posts

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.

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

Since I couldn’t generate the error today, I couldn’t give you the exact description, but it was something like

  1. Illegal use of type 'struct QLineEdit'........
whenever I tried to use this statement –

  1. ldt->setText("My Text");

October 10, 2011

sayem.bd sayem.bd
Lab Rat
33 posts

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 –

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 –

  1. ldt->setText("My Text");

and the second error message is –

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

October 10, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

Have you included <QtGui/QLineEdit>?

October 10, 2011

sayem.bd sayem.bd
Lab Rat
33 posts
Lukas Geyer wrote:
Have you included <QtGui/QLineEdit>?

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

October 10, 2011

Volker Volker
Robot Herder
5428 posts

The error message

invalid use of incomplete type ‘struct BlaFaselClass’

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

October 10, 2011

sayem.bd sayem.bd
Lab Rat
33 posts

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

Thank you all for all of your responses.

 
  ‹‹ how to do for de aplication recognize the gamePAD USB???      Problem related to paint event? ››

You must log in to post a reply. Not a member yet? Register here!