在上一篇中,原本的空窗口本来时可以追踪鼠标的,之后我们想加入一些控件,接下来的问题是提示不能进行布局,因为已经有一个布局了。我们通过设置中心控件,使得可以进行布局了。
但是,当我们布局好之后,又出现问题了:鼠标又不能跟踪了。这时要检查,父窗口和子窗口都要设置:
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()
{
}