主要目的获取当前鼠标的的点击位置
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
protected:
void mousePressEvent(QMouseEvent *e);
private:
int x,y;
QLabel *label1;
QLabel *label2;
};
#endif // WIDGET_H
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
x=0;
y=0;
resize(200,200);
label1=new QLabel(tr("%1").arg(x));
label2=new QLabel(tr("%1").arg(y));
QHBoxLayout *layout=new QHBoxLayout;
layout->addWidget(label1);
layout->addWidget(label2);
setLayout(layout);
}
void Widget::mousePressEvent(QMouseEvent *e)
{
x=e->pos().x();
y=e->pos().y();
label1->setText(tr("%1").arg(x));
label2->setText(tr("%1").arg(y));
}
Widget::~Widget()
{
}
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}