qt中文字图片跟随鼠标移动

//未添加注释部分请自行查阅qt中帮助文档(F1)

mainwindow.h中:

#include <QMainWindow>

#include <QMouseEvent>

class MainWindow : public QWidget{  

Q_OBJECTpublic: MainWindow(QWidget *parent = 0);  

~MainWindow();  

void mouseMoveEvent(QMouseEvent * event);  

void paintEvent(QPaintEvent *);

private: int x,y;  

QString str;  

QPoint p;

};

mainwindow.cpp中:

#include "mainwindow.h"

#include <QPainter>

MainWindow::MainWindow(QWidget *parent) : QWidget(parent){  

setPalette(QPalette(Qt::white));  

setAutoFillBackground(true);  

setMinimumSize(400,400);  

str = "西北农林科技大学";  

setMouseTracking(true); //若没有此项则需点击鼠标才能触发mouseMoveEvent事件}void MainWindow::mouseMoveEvent(QMouseEvent * event){ 

x=event->pos().x();  

y=event->pos().y();  

update();

}

void MainWindow::paintEvent(QPaintEvent *){

QPainter paint(this);

paint.setPen(Qt::green);

p=QPoint(x,y);

paint.setBrush(Qt::yellow);

paint.drawEllipse(p,50,50);

paint.drawText(x,y,str);

}

MainWindow::~MainWindow(){}

你可能感兴趣的:(qt)