QT学习笔记(6)事件

Qt的事件不同于信号槽机制,更加的灵活

#include "mainwindow.h"
#include 
#include 
#include 

class EventLabel : public QLabel
{
protected:
    void mouseMoveEvent(QMouseEvent*);
    void mousePressEvent(QMouseEvent*);
    void mouseReleaseEvent(QMouseEvent*);
};

void EventLabel :: mouseMoveEvent(QMouseEvent* event)
{
    this -> setText(QString("

Move: (%1,%2)<\h1><\center>"). //arg替换占位符 arg(QString::number(event->x()),QString::number(event->y()))); } void EventLabel :: mousePressEvent(QMouseEvent* event) { this -> setText(QString("

Press:(%1,%2)<\h1>\center>").arg(QString::number(event->x()), QString::number(event->y()))); } void EventLabel :: mouseReleaseEvent(QMouseEvent* event) { QString msg; msg.sprintf("

Release: (%d, %d)

"
,event->x(), event->y()); this->setText(msg); } int main(int argc, char *argv[]) { QApplication a(argc, argv); EventLabel *label = new EventLabel; label -> setWindowTitle("MouseEvent"); label -> resize(300, 300); label -> setMouseTracking(true); label -> show(); return a.exec(); }

你可能感兴趣的:(QT)