請教各位大大,如何將目前螢幕滑鼠移動的座標位置(X,Y) 即時顯示在自己所建立新Form 上的Label 上?
在書裡所寫的都是只針對自己所建立的Form 內的物件重寫相關Event(如下),但是只要滑鼠移出了this Form 就沒有作用了,
能否請各位大大指導一下,感恩不盡~~~
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QMouseEvent>
class EventLabel : public QLabel
{
protected:
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
};
void EventLabel::mouseMoveEvent(QMouseEvent *event)
{
QString msg;
msg.sprintf("<center><h1>Move: (%d, %d)</h1></center>", event->x(), event->y());
this->setText(msg);
}
void EventLabel::mousePressEvent(QMouseEvent *event)
{
QString msg;
msg.sprintf("<center><h1>Press: (%d, %d)</h1></center>", event->x(), event->y());
this->setText(msg);
}
void EventLabel::mouseReleaseEvent(QMouseEvent *event)
{
QString msg;
msg.sprintf("<center><h1>Release: (%d, %d)</h1></center>", event->x(), event->y());
this->setText(msg);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
EventLabel *label = new EventLabel;
label->setWindowTitle("MouseEvent Demo");
label->resize(300, 200);
label->show();
return app.exec();
}