PDA

View Full Version : Click on QLabel



sousadaniel7
1st May 2012, 10:39
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

Spitfire
1st May 2012, 14:02
A way:


MainWindow::MainWindow(QWidget *parent)
{
QLabel* l = new QLabel( "test", this );
l->installEventFilter( this );
}

bool MainWindow::eventFilter( QObject* object, QEvent* event )
{
if( event->type() == QEvent::MouseButtonRelease )
{
qDebug() << "Label action!"; // do what you want here
}

return QMainWindow::eventFilter( object, event );
}


A better way:


MainWindow::MainWindow(QWidget *parent)
{
QPushButton* button = new QPushButton( "test", this );
connect( button, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
}

void MainWindow::buttonClicked( void )
{
qDebug() << "Action!"; // do what you want here
}

sousadaniel7
2nd May 2012, 11:34
A way:


MainWindow::MainWindow(QWidget *parent)
{
QLabel* l = new QLabel( "test", this );
l->installEventFilter( this );
}

bool MainWindow::eventFilter( QObject* object, QEvent* event )
{
if( event->type() == QEvent::MouseButtonRelease )
{
qDebug() << "Label action!"; // do what you want here
}

return QMainWindow::eventFilter( object, event );
}


A better way:


MainWindow::MainWindow(QWidget *parent)
{
QPushButton* button = new QPushButton( "test", this );
connect( button, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
}

void MainWindow::buttonClicked( void )
{
qDebug() << "Action!"; // do what you want here
}


This actually works but that's not what I want. I want each QLabel has a different behavior, but without the need to create a class for each QLabel.

plopes21
2nd May 2012, 12:36
I've done something, I'll send you an email with the solution.
Regards

sousadaniel7
2nd May 2012, 14:18
I've done something, I'll send you an email with the solution.
Regards

Not work. Please help.

Spitfire
2nd May 2012, 14:30
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:



class ClickableLabel : public QLabel
{
Q_OBJECT
public:
ClickableLabel( QWidget* parent = 0 ) : QLabel( parent ) {}

signals:
void clicked( void );

protected:
void mouseReleaseEvent( QMouseEvent* event ) // or double clicked or what you want
{
QLabel::mouseReleaseEvent( event );
emit this->clicked();
}
};

Single class and problem solved.

Added after 7 minutes:


I've done something, I'll send you an email with the solution.
Regards

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.

antweb
1st July 2014, 09:44
Hello I am new to QT. I can implement the signal and slot part but how do i know which label triggered the event?

anda_skoa
1st July 2014, 11:01
Hello I am new to QT. I can implement the signal and slot part but how do i know which label triggered the event?
A lot of options:

- send some kind of identifier as a signal argument
- use QSignalMapper
- use QObject::sender() in the receiving slot
- send a pointer to the label itself as a signal argument

Cheers,
_

antweb
1st July 2014, 11:50
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?

anda_skoa
1st July 2014, 12:22
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,
_