\
#include "mylabel.h"
#include<QMoveEvent>
#include<QDebug>
MyLabel::MyLabel(QWidget *parent) :
QLabel(parent)
{
//设置追踪鼠标
this->setMouseTracking(true);//默认鼠标一进来就有反应
}
void MyLabel::mousePressEvent(QMouseEvent *ev)
{
int i = ev->x();
int j = ev->y();
//sprinf
/*
* QString str = QString("abc %1 ^_^ %2).arg(123).arg("mike");
* str = abc 123 ^_^ mike
*/
if(ev->button() == Qt::LeftButton)
{
qDebug() <<"left";
}
if(ev->button() == Qt::RightButton)
{
qDebug() <<"Right";
}
if(ev->button() == Qt::MidButton)
{
qDebug() <<"Mide";
}
QString text = QString("<center><h1>Mouse Press: (%1, %2)</h1></center>")
.arg(i).arg(j);
this->setText(text);
}
void MyLabel::mouseReleaseEvent(QMouseEvent *ev)
{
QString text = QString("<center><h1>Mouse Release: (%1, %2)</h1></center>")
.arg(ev->x()).arg(ev->y());
this->setText(text);
}
void MyLabel::mouseMoveEvent(QMouseEvent *ev)
{
QString text = QString("<center><h1>Mouse Move: (%1, %2)</h1></center>")
.arg(ev->x()).arg(ev->y());
// this->setText(text);
}
void MyLabel::enterEvent(QEvent *e)
{
QString text = QString("<center><h1>Mouse enter</h1></center>");
this->setText(text);
}
void MyLabel::leaveEvent(QEvent *e)
{
QString text = QString("<center><h1>Mouse leave</h1></center>");
this->setText(text);
}
\
\
\
#include "mywidget.h"
#include "ui_mywidget.h"
#include<QDebug>
#include<QKeyEvent>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
timerId = this->startTimer(1000);//毫秒为单位,这里是指1秒
timerId2 = this->startTimer(500);
}
MyWidget::~MyWidget()
{
delete ui;
}
void MyWidget::keyPressEvent(QKeyEvent *e)
{
qDebug() << (char)e->key();
if(e->key() == Qt::Key_A)
{
qDebug() << "Qt::Key_A";
}
}
void MyWidget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == this->timerId)
{
static int sec = 0;
ui->label->setText(
QString("<center><h1>timer out: %1</h1></center>").arg(sec++)
);
if(5 == sec)
{
this->killTimer(this->timerId);
}
}
else if(e->timerId() == this->timerId2)
{
static int sec = 0;
ui->label_2->setText(
QString("<center><h1>timer out: %1</h1></center>").arg(sec++)
);
}
}
\