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.
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.
Hi,
Not clear!!!!!!!!!!!
Qt Code:
{ connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) ); } { emit clicked(); } void myLabel::slotClicked() { qDebug()<<"Clicked"; }To copy to clipboard, switch view to plain text mode
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!!!!!
How do you "give" the code?When I give this code, qt display some errors.
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
Ok, here is the full code
//.h
Qt Code:
{ Q_OBJECT public: ~myLabel(){} signals: void clicked(); public slots: void slotClicked(); protected: }; #endifTo copy to clipboard, switch view to plain text mode
.cppQt Code:
{ connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) ); } void myLabel::slotClicked() { qDebug()<<"Clicked"; } { emit clicked(); }To copy to clipboard, switch view to plain text mode
In your mainWindows.cpp in the constructor add this
Qt Code:
{ myLabel* m_label = new myLabel(this); }To copy to clipboard, switch view to plain text mode
I have not tested the code so please correct the code if you get any errors
Thanks
Hi there!!
And how is it possible for the Widget to catch the clicked signal from the label???
Thanks for the help!!!
Best regards!
code of vermarajeev works well for me![]()
If you use this example you can catch it like this:MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
myLabel* m_label = new myLabel(this);
}
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
myLabel* m_label = new myLabel(this);
connect(myLabel, SIGNAL(clicked()), SLOT(myActionWhenMyLabelHasBeenClicked()));
}
Jake123 (27th January 2013)
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![]()
Last edited by lotek; 11th October 2010 at 10:35.
Jake123 (27th January 2013)
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
And how to check which label has been clicked if have many????
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:
Qt Code:
myLabel *Label = qobject_cast<myLabel *>( this->sender() );To copy to clipboard, switch view to plain text mode
Label is pointer to the object that send signal.
In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
The advance of computerization however, has not yet wiped out nations and ethnic groups.
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.
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:
Qt Code:
// myLabel.h signals: void clicked( const QPos & ); // myLabel.cpp: { emit clicked( event->pos() ); }To copy to clipboard, switch view to plain text mode
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
study24816 (1st August 2012)
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
Bookmarks