一、前言
Qt刚开始,先写一个简单的小程序。实现如下功能:在界面上显示图片,并通过鼠标拖动 。效果如下:
二、实现思路
1. QLable类
Qt中的 QLable类,不仅可以显示文本,还可以显示图片。我们这里首先创建三个QLable对象,并将他们添加到数组里面。
for(i = 0;i < 3;i++)
{
lable = new QLabel("",this);
lable->setGeometry(50,100+(150*i),100,100);
lable->installEventFilter(this);
lable_group.push_back(lable);
}
2.paintEvent
使用绘图时间绘图,绘图事件不用手动调用,通过重写父类虚函数之后会自动调用。这里我们重写该函数。
void MainWindow::paintEvent(QPaintEvent *event)
{
int i;
pa = new QPainter(this);
QPixmap zhan (":/new/prefix1/1.png");
pa->drawPixmap(lable_group[0]->x(),lable_group[0]->y(),100,100,zhan);
QPixmap kun (":/new/prefix1/2.png");
pa->drawPixmap(lable_group[1]->x(),lable_group[1]->y(),100,100,kun);
QPixmap du (":/new/prefix1/3.png");
pa->drawPixmap(lable_group[2]->x(),lable_group[2]->y(),100,100,du);
this->update();
}
3.eventFilter
另外,我们要拖动图标,要判断鼠标的按下,移动,松开。通过时间过滤器来进行判断以及操作。
bool MainWindow::eventFilter(QObject *obj, QEvent *evt)
{
QLabel* btn;
btn = qobject_cast<QLabel*>(obj);
static QPoint lastPnt;
static bool isHover = false;
if(evt->type() == QEvent::MouseButtonPress) //鼠标按下
{
QMouseEvent* e = static_cast<QMouseEvent*>(evt);
if(btn->rect().contains(e->pos()) && (e->button() == Qt::LeftButton))
{
lastPnt = e->pos();
isHover = true;
}
}else if(evt->type() == QEvent::MouseMove && isHover){ //鼠标移动
QMouseEvent* e = static_cast<QMouseEvent*>(evt);
int dx = e->pos().x() - lastPnt.x();
int dy=e->pos().y()-lastPnt.y();
btn->move(btn->x()+dx,btn->y()+dy);
}else if(evt->type() == QEvent::MouseButtonRelease && isHover){ //鼠标释放
isHover = false;
}
return false;
}
三、代码
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QList>
#include <QPushButton>
#include <QWidget>
#include <QMouseEvent>
#include <QDebug>
#include <QLabel>
#include <QPainter>
#include <QPixmap>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QLabel* lable;
QList<QLabel*> lable_group;
QPainter *pa;
int p_flag;
int p_num;
public slots:
bool eventFilter(QObject *obj, QEvent *evt);
protected:
// 声明事件函数
virtual void paintEvent(QPaintEvent * event);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
int i;
for(i = 0;i < 3;i++)
{
lable = new QLabel("",this);
lable->setGeometry(50,100+(150*i),100,100);
lable->installEventFilter(this);
lable_group.push_back(lable);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event)
{
int i;
pa = new QPainter(this);
QPixmap zhan (":/new/prefix1/1.png");
pa->drawPixmap(lable_group[0]->x(),lable_group[0]->y(),100,100,zhan);
QPixmap kun (":/new/prefix1/2.png");
pa->drawPixmap(lable_group[1]->x(),lable_group[1]->y(),100,100,kun);
QPixmap du (":/new/prefix1/3.png");
pa->drawPixmap(lable_group[2]->x(),lable_group[2]->y(),100,100,du);
this->update();
}
bool MainWindow::eventFilter(QObject *obj, QEvent *evt)
{
QLabel* btn;
btn = qobject_cast<QLabel*>(obj);
static QPoint lastPnt;
static bool isHover = false;
if(evt->type() == QEvent::MouseButtonPress) //鼠标按下
{
QMouseEvent* e = static_cast<QMouseEvent*>(evt);
if(btn->rect().contains(e->pos()) && (e->button() == Qt::LeftButton))
{
lastPnt = e->pos();
isHover = true;
}
}else if(evt->type() == QEvent::MouseMove && isHover){ //鼠标移动
QMouseEvent* e = static_cast<QMouseEvent*>(evt);
int dx = e->pos().x() - lastPnt.x();
int dy=e->pos().y()-lastPnt.y();
btn->move(btn->x()+dx,btn->y()+dy);
}else if(evt->type() == QEvent::MouseButtonRelease && isHover){ //鼠标释放
isHover = false;
}
return false;
}
四、最后
水平有限,解释可能有误,请多指教。