event事件重写,显示鼠标位置
1 void EventLabel::mouseMoveEvent(QMouseEvent *event) 2 { 3 this->setText(QString("<center><h1>Move:(%1,%2)</h1></center>") 4 .arg(QString::number(event->x()),QString::number(event->y()))); 5 } 6 void EventLabel::mousePressEvent(QMouseEvent *event) 7 { 8 this->setText(QString("<center><h1>Press:(%1,%2)</h1></center>") 9 .arg(QString::number(event->x()),QString::number(event->y()))); 10 } 11 void EventLabel::mouseReleaseEvent(QMouseEvent *event) 12 { 13 QString msg; 14 msg.sprintf("<center><h1>Release: (%d,%d)</h1></center>", 15 event->x(),event->y()); 16 this->setText(msg); 17 }
main函数中代码
1 EventLabel *label = new EventLabel(); 2 3 label->setWindowTitle("MouseEvent Demo"); 4 label->resize(600,400); 5 label->show();