您的位置:首页 > 编程语言 > Qt开发

Qt简单程序1 QMousePressEvent

2009-06-18 22:27 330 查看
主要目的获取当前鼠标的的点击位置

#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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: