PDA

View Full Version : QLabel::clicked()



sabeesh
2nd August 2007, 06:03
Hi,
I am using QT 4.2.2. I have a label in my main window. I want a "QLabel::clicked()" signal for my label, ie, i want to call a function when i click on a lable. How can I do that.

vermarajeev
2nd August 2007, 06:36
Hi,
I am using QT 4.2.2. I have a label in my main window. I want a "QLabel::clicked()" signal for my label, ie, i want to call a function when i click on a lable. How can I do that.

There is no clicked signal in QLabel. What you can do is create your own QLabel class and override mousePressEvent to fire a clicked signal and catch it in your defined slot.

sabeesh
2nd August 2007, 06:45
Hi,
Not clear!!!!!!!!!!!

vermarajeev
2nd August 2007, 06:49
Hi,
Not clear!!!!!!!!!!!


myLabel::myLabel( const QString & text, QWidget * parent )
:QLabel(parent)
{
connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
}
void myLabel::mousePressEvent ( QMouseEvent * event )
{
emit clicked();
}
void myLabel::slotClicked()
{
qDebug()<<"Clicked";
}

sabeesh
2nd August 2007, 07:02
Hi,
When I give this code, qt display some errors. Can you give me a small discreption aout this code. because, i am a new comer in QT!!!!!

marcel
2nd August 2007, 10:46
When I give this code, qt display some errors.

How do you "give" the code?

That is a QLabel subclass.
You have to define your own class, and add those methods to it and also the signal.

That code is no not complete. It is just a suggestion.
You will have to embed it in your application.

Regards

vermarajeev
2nd August 2007, 13:26
Ok, here is the full code

//.h

class myLabel : public QLabel
{
Q_OBJECT
public:
myLabel( const QString & text, QWidget * parent = 0 );
~myLabel(){}

signals:
void clicked();

public slots:
void slotClicked();

protected:
void mousePressEvent ( QMouseEvent * event ) ;

};
#endif

.cpp

myLabel::myLabel( const QString & text, QWidget * parent )
:QLabel(parent)
{
connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
}

void myLabel::slotClicked()
{
qDebug()<<"Clicked";
}

void myLabel::mousePressEvent ( QMouseEvent * event )
{
emit clicked();
}

In your mainWindows.cpp in the constructor add this


MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
myLabel* m_label = new myLabel(this);
}


I have not tested the code so please correct the code if you get any errors
Thanks

jbarrena
16th July 2010, 08:52
Hi there!!

And how is it possible for the Widget to catch the clicked signal from the label???

Thanks for the help!!!

Best regards!

gun4boy
11th October 2010, 06:18
code of vermarajeev works well for me ;)

Sven
11th October 2010, 07:40
Hi there!!

And how is it possible for the Widget to catch the clicked signal from the label???

Thanks for the help!!!

Best regards!



MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
myLabel* m_label = new myLabel(this);
}

If you use this example you can catch it like this:


MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
myLabel* m_label = new myLabel(this);
connect(myLabel, SIGNAL(clicked()), SLOT(myActionWhenMyLabelHasBeenClicked()));
}

lotek
11th October 2010, 11:24
Hi,

only one remark:

The "real click" will be better simulated if you will use the
mouseReleaseEvent(QMouseEvent *e) instead of mousePressEvent(QMouseEvent *e)


regards

me :)

dogbear
27th October 2010, 09:34
I agree with Lotek.
A mouse "click" is traditionally a full press-then-release event series.
Interpreting a click on a mouse press disallows the user from canceling the click when they drag the mouse away from the item, amongst other things.
Just my two-cents.
doggy

gadlol
9th July 2011, 19:21
And how to check which label has been clicked if have many????

Talei
9th July 2011, 21:07
Using above example if You connect multiple instances of myLabel object into one slot then within that slot You can use sender() to determine what myLabel object send the signal. To have full access You can cast it lik:


myLabel *Label = qobject_cast<myLabel *>( this->sender() );
Label->setText( QString( "This sender is: %1" ).arg( this->sender()->objectName() ) );

Label is pointer to the object that send signal.

study24816
1st August 2012, 03:53
thanks to this thread! I'm having the same problem with my project and this works just fine for me. But, in my application, besides emitting the signal after the mouse is clicked/released, I also need to know the coordinates of the clicked point. Is there any way to program this??
I'm new to Qt and is learning :D.

d_stranz
1st August 2012, 04:05
Did you read the docs on QMouseEvent? There are 7 different methods to give you information about the position of the mouse.

If the slot listening for the clicked() signal needs to know the mouse position, then change the clicked signal to send the position as an argument:



// myLabel.h
signals:
void clicked( const QPos & );

// myLabel.cpp:

void myLabel::mouseReleaseEvent( QMouseEvent * event )
{
emit clicked( event->pos() );
}

study24816
1st August 2012, 04:17
I think I've foudn the answer. the pointer *event to QMouseEvent stores the relevant information about the event mousepress/release. I gotta read more about QMouseEvent.

Thanks