1. 程式人生 > >QT event 使用

QT event 使用

QT event 是一種機制,

可以重新實現物件的
mouse(Press/Release/Move/DoubleClick)Event(QMouseEvent *e);

keyEvent

paintEvent

這裡重新實現MainWindow物件跟蹤滑鼠事件,並顯示出來

#include <QMainWindow>
#include <QLabel>
#include <QMouseEvent>
#include <QStatusBar>
class MouseEvent:public QMainWindow
{
    Q_OBJECT
    public
: MouseEvent(QWidget *parent=0); ~MouseEvent(); protected: void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); private: QLabel *mouseStatus; };
#include "MouseEvent.h"
#include <QString>
MouseEvent::MouseEvent(QWidget *parent) :QMainWindow(parent) { mouseStatus=new QLabel(tr(""),this); statusBar()->addPermanentWidget(mouseStatus); resize(400,200); setMouseTracking(true); } MouseEvent::~MouseEvent( ) { } void MouseEvent::mousePressEvent(QMouseEvent *e) { QString
button_str; if(e->button()==Qt::LeftButton) button_str=tr("left click "); if(e->button()==Qt::RightButton) button_str=tr("right click "); QString str="("+QString::number(e->x())+","+QString::number(e->y())+")"; mouseStatus->setText(button_str+tr("Positon")+str); } void MouseEvent::mouseMoveEvent(QMouseEvent *e) { QString str="("+QString::number(e->x())+","+QString::number(e->y())+")"; mouseStatus->setText(tr("Positon")+str); } void MouseEvent::mouseReleaseEvent(QMouseEvent *e) { QString str="("+QString::number(e->x())+","+QString::number(e->y())+")"; mouseStatus->setText(tr("Release Positon")+str); }

mark