1.每一个控件都有
bool event(QEvent *event); bool eventFilter(QObject *watched, QEvent *event); //事件过滤器,就是什么控件想捕获怎样的事件
ui->label->installEventFilter(this); //label控件安装过滤器
ui->label->setMouseTracking(true); //设置鼠标跟踪
bool Widget::eventFilter(QObject *watched, QEvent *event)
{
if(watched == ui->label)
{
static int t = 0;
QMouseEvent *env = (QMouseEvent *)(event); //鼠标事件
if(event->type() == QEvent::MouseMove) //移动事件
{
ui->label->setText(QString("%1").arg(t++));
return true;
}
}
return QWidget::eventFilter(watched,event);
}
2.重绘控件在布局中的位置和大小
void Widget::paintEvent(QPaintEvent* e)
{
//_curve->update();
ui->label_show->setGeometry((ui->groupBox->width() - ui->label_show->height())/2,
(ui->groupBox->height() - ui->label_show->height()+9)/2,
ui->label_show->height(),
ui->label_show->height());
}