鼠标在窗口加入控件之后不能追踪的处理

          在上一篇中,原本的空窗口本来时可以追踪鼠标的,之后我们想加入一些控件,接下来的问题是提示不能进行布局,因为已经有一个布局了。我们通过设置中心控件,使得可以进行布局了。

         但是,当我们布局好之后,又出现问题了:鼠标又不能跟踪了。这时要检查,父窗口和子窗口都要设置:

      this->setMouseTracking(true);

      widget->setMouseTracking(true);

这样就可以进行跟踪了,但是有控件的地方还是跟踪不到的。

头文件:

#ifndef PROROBOTICS_H
#define PROROBOTICS_H

#include <QtGui/QMainWindow>
#include <QStatusBar>
#include <QMouseEvent>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QString>
#include <QGridLayout>
#include <QWidget>

class ProRobotics : public QMainWindow
{
    Q_OBJECT
    
public:
    ProRobotics(QWidget *parent = 0);
    ~ProRobotics();
/**********定义图形控件***********************/
public:
    QGridLayout     *layout;
    QWidget         *widget;
    QPushButton          *motionLabel;
    QPushButton          *measurementLabel;

protected:
    void mouseMoveEvent(QMouseEvent *);
private:
    QStatusBar* m_pStatus;
};

#endif // PROROBOTICS_H
.cpp文件:

#include "prorobotics.h"

ProRobotics::ProRobotics(QWidget *parent)
    : QMainWindow(parent)
{
    this->setMouseTracking(true);                       //父窗口和子窗口都要设置追踪
    setWindowTitle(tr("PROBABILISTIC ROBOTICS DEMO"));
    widget = new QWidget(this);
    layout = new QGridLayout(widget);
    widget->setLayout(layout);
    this->setCentralWidget(widget);

    motionLabel = new QPushButton;
    motionLabel->setText(tr("MOTION MODEL"));
    measurementLabel = new QPushButton;
    measurementLabel->setText(tr("MEASUREMENT MODEL"));

    layout->addWidget(motionLabel,0,0);
    layout->addWidget(measurementLabel,0,1);
    layout->setMargin(15);
    layout->setSpacing(10);
/*********显示鼠标相关的设置*******************/
    widget->setMouseTracking(true);
    m_pStatus = new QStatusBar();
    setStatusBar(m_pStatus);
    m_pStatus->showMessage("application init ok!");
}

void ProRobotics::mouseMoveEvent(QMouseEvent *event)
{
    QPoint pos = event->pos();
    m_pStatus->showMessage(QString("x:%1,y:%2").arg(pos.x()).arg(pos.y()));
}


ProRobotics::~ProRobotics()
{
    
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值