#include <QApplication> #include <QMainWindow> #include <QHBoxLayout> #include <QLabel> #include <QMouseEvent> class myLabel: public QLabel { public: myLabel() { this->setAlignment(Qt::AlignCenter); //Default Label Value this->setText("No Value"); //set MouseTracking true to capture mouse event even its key is not pressed this->setMouseTracking(true); }; ~ myLabel(){}; void mouseMoveEvent ( QMouseEvent * event ) { //Show x and y coordinate values of mouse cursor here this->setText("X:"+QString::number(event->x())+"-- Y:"+QString::number(event->y())); }; }; int main(int argc, char **argv) { QApplication app(argc, argv); QMainWindow *window = new QMainWindow(); window->setWindowTitle(QString::fromUtf8("QT - Capture Mouse Move")); window->resize(300, 250); QWidget *centralWidget = new QWidget(window); QHBoxLayout* layout = new QHBoxLayout(centralWidget); myLabel* CoordinateLabel = new myLabel(); layout->addWidget(CoordinateLabel); window->setCentralWidget(centralWidget); window->show(); return app.exec(); }
