Handle Mouse Move Event in QT - QMouseEvent

Summary : This code shows how to handle mouse move event in Qt. X and Y coordinate values of mouse cursor are updated here each time Mouse Move Event is captured.
 Qt Version:  4.7.4
Date Added: 2011-06-18
#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();
}
Output :
Handle Mouse Move Event in QT - QMouseEvent


Similar Examples
Handle Mouse Double Click Event in QT
Handle Mouse Release Event in QT
Capture Mouse Right Click Event in Qt
Capture Mouse Left Click Event in Qt

Examples About Some Qt Classes
QLcdNumberQTimer
QPushButtonQMessageBox
QTabWidgetQtConcurrent
QLabelQList
QKeyEventQSlider
QFontDialogQSpinBox
QTableWidgetQTextBrowser
Qt StylesheetsQCheckBox
QComboBoxQCloseEvent
QProgressBarQProgressDialog
QTreeWidgetQSignalMapper



Recommend This : Bookmark and Share
 
Want any example ? Feel free to tell us:
(Max. 2000 characters)
Please note that we do not guarantee to respond.Thanks.

C++
OpenSceneGraph
Fundamentals
QT
Boost
wxWidgets
Java
Python



Copyright 2011 - 2013 | All Rights Reserved
All trademarks are properties of their respective owners.
Contact| Privacy Policy| About Us