Qlabel clicked event
-
As we know, we can add a clicked event to a QLabel by:
connect(ui.label1, signal( clicked() ), this, slot(hello()));
Thus the responding function is hello():
void hello(){
//How Can I get the clicked position of mouse
}I do not know how to get the position of the mouse. I mean the co-ordinate values (x,y)
-
There is no click signal for QLabel.
If you need one then you need to reimplement its mousePressEvent or mouseReleaseEvent functions and then from it emit your custom signal.
You can get the click position by QMouseEvent::pos ().
Check "mouseReleaseEvent":http://qt-project.org/doc/qt-5.0/qtwidgets/qlabel.html#mouseReleaseEvent and "qmouseevent":http://qt-project.org/doc/qt-5.0/qtgui/qmouseevent.html#pos for moe details -
As p3c0 says, you'll have to reimplemented one of the mouse events and emit a custom signal. Something like the following:
label.h
@
...
class Label : public QLabel {
Q_OBJECTpublic:
Label(QWidget *parent = 0, Qt::WindowFlags f=0);
Label(const QString &text, QWidget *parent=0, Qt::WindowFlags f=0);
~Label();protected:
void mousePressEvent(QMouseEvent *ev);signals:
void clicked(QPoint pos);
};
...
@label.cpp
@
...
Label::Label(QWidget *parent, Qt::WindowFlags f):QLabel(parent,f){}
Label::Label(const QString &text, QWidget *parent, Qt::WindowFlags f):QLabel(text,parent,f){}
Label::~Label(){}void Label::mousePressEvent(QMouseEvent *ev){
emit clicked(ev->pos());
}
...
@Hope this helps ;o)
-
Thank you very much for you kindly reply.
I am using visual studio 2010 and QT addin. Each time I rebuild my application, the "ui_xxx.h" is modified automatically and my modification is lost (including the declaration of my class "MyLabel label").[quote author="jazzycamel" date="1380876254"]As p3c0 says, you'll have to reimplemented one of the mouse events and emit a custom signal. Something like the following:
label.h
@
...
class Label : public QLabel {
Q_OBJECTpublic:
Label(QWidget *parent = 0, Qt::WindowFlags f=0);
Label(const QString &text, QWidget *parent=0, Qt::WindowFlags f=0);
~Label();protected:
void mousePressEvent(QMouseEvent *ev);signals:
void clicked(QPoint pos);
};
...
@label.cpp
@
...
Label::Label(QWidget *parent, Qt::WindowFlags f):QLabel(parent,f){}
Label::Label(const QString &text, QWidget *parent, Qt::WindowFlags f):QLabel(text,parent,f){}
Label::~Label(){}void Label::mousePressEvent(QMouseEvent *ev){
emit clicked(ev->pos());
}
...
@Hope this helps ;o)[/quote]
-
you shouldn't do your implementation in "ui_xxx.h". This is generated - as i already noticed ;) - dynamically by Qt.
Create a new H- and CPP-file instead, or at least add it to an existing implementation. -
I know you point. I create a MyLabel.h and MyLabel.cpp.
So I have to use it:
MyLabel label; //this sentence should be put in ui_xxx.h, right? -
again...you never edit the "ui_xxx.h" at all. You most probably create a instance there where you call setupUI()
-
Still I am puzzled. in which file should I create an instance of MyLabel? And how can I make the GUI use MyLabel instead of the default QLabel?
[quote author="raven-worx" date="1380892316"]again...you never edit the "ui_xxx.h" at all. You most probably create a instance there where you call setupUI()[/quote]
-
AFAIK, there are 2 ways,
-
Add a QLabel in your form by dragging a QLabel widget from the widgetbox and right click on that QLabel > promote to you subclassed QLabel (you need to set *.h and *.cpp )
-
From you QDialog or QMainWindow include the subclassed QLabel's header file and create an instance there.
-