QLabel click event using Qt?

Nicholas picture Nicholas · Aug 14, 2015 · Viewed 20.7k times · Source

I'm new in Qt and have a question.

I have QLabel and QLineEdit objects, and when QLabel text is clicked on, I want to set this text in QLineEdit.

Also I have read that QLabel has not clicked signal.

Can you explain how can I do this and write code for me ?!

Answer

kh25 picture kh25 · Aug 17, 2015

Either style another type of QWidget such as a specific QPushButton to look like a QLabel and use its clicked() signal or inherit QLabel yourself and emit your own clicked() signal.

See this example: https://wiki.qt.io/Clickable_QLabel

If you choose the latter option you can pass the text in the signal. Then connect the necessary signals/slots up between the QLabel and the QLineEdit like so:

QObject::connect(&label, SIGNAL(clicked(const QString& text)),
                 &lineEdit, SLOT(setText(const QString& text)));