Hello!
Some can tell me or provide a code that allows click on top of a QLabel and then do a certain action?
Regards,
Daniel Sousa
Printable View
Hello!
Some can tell me or provide a code that allows click on top of a QLabel and then do a certain action?
Regards,
Daniel Sousa
A way:
Code:
{ l->installEventFilter( this ); } { { qDebug() << "Label action!"; // do what you want here } }
A better way:
Code:
{ connect( button, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) ); } void MainWindow::buttonClicked( void ) { qDebug() << "Action!"; // do what you want here }
I've done something, I'll send you an email with the solution.
Regards
So, what's the problem with event filter?
You can recognise which label triggered the event and do something based on that.
Or, create ClickableLabel, a class that will emitt a 'clicked()' signal which you can connect to a slot that does something.
Each label can be connected to different slot depending on what you need it to do:
Single class and problem solved.Code:
{ Q_OBJECT public: signals: void clicked( void ); protected: { emit this->clicked(); } };
Added after 7 minutes:
Don't be shy. Show your solution to everyone so others may benefit in a future.
Or if it's a bad idea you may get some feedback and learn something.
Hello I am new to QT. I can implement the signal and slot part but how do i know which label triggered the event?
Thanks for the quick response.
Like i have a number of QLabels and i want to see through which i got the click, can i differentiate on the basis of their names or should i check out the mouse position at that time.
Can you please tell how i can do that?
You don't have to check mouse position since each label will only emit its signal when it is clicked and not when another label is clicked.
The rest is one of the option I've listed above.
Cheers,
_