概述
来句名言:少壮不努力,老大送快递。
---------------------------------------------------------------------------------------------------那么我们开始吧----------------------------------------------------------------------------------------------
在我们写程序的时候有一些特殊的组件可能QT自带的无法满足要求,那么我们需要自定义一个控件come con来吧。
这是我写的绘画板的头.h
#ifndef DRAWBOARD_H_
#define DRAWBOARD_H_
#include <QWidget>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include "qdebug.h"
class DrawBoard:public QWidget
{
Q_OBJECT
public:
DrawBoard(QWidget *parent=0);
void setBackgrpundPath(QString path);
~DrawBoard();
protected:
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *);
void mouseReleaseEvent(QMouseEvent *);
private:
QPen pen;
enum{DRAW_START=0,DRAW_ING=1,DRAW_END=2};
struct DrawLinePath{
int startX;
int startY;
int endX;
int endY;
};
int DRAW_STATUS = DRAW_END;
QList<QPainterPath>paintPathList;
QList<DrawLinePath> drawLinePathList;
int startX, startY,endX,endY;
QString backgroundPath;
QColor color;
QImage *image;
};
#endif
比较重要的是这几段:
protected:
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *);
void mouseReleaseEvent(QMouseEvent *);
protected:是受保护的意思,重写这几个的意思是重写QWidget组件的四个虚函数,
那么我们再来看看.cpp文件
#include "drawboard.h"
DrawBoard::DrawBoard(QWidget*parent) :QWidget(parent)
{
backgroundPath = "-1";
setAttribute(Qt::WA_StaticContents);//自动缩放
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
image = new QImage(this->width(), this->height(), QImage::Format_Indexed8);
image->fill(qRgba(0, 0, 0, 0));
color == Qt::black;
pen.setWidth(10);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
pen.setStyle(Qt::SolidLine);
}//鼠标抬起事件
void DrawBoard::mouseReleaseEvent(QMouseEvent *event){
repaint();
DRAW_STATUS = DRAW_END;
update();
}//窗体大小改变事件
void DrawBoard::resizeEvent(QResizeEvent *event){
update();
}
void DrawBoard::setBackgrpundPath(QString path){
this->backgroundPath = path;
image->load(path);
update();
}//窗体绘画事件
void DrawBoard::paintEvent(QPaintEvent *event){
//获取一个画笔对象
QPainter painter(this);
painter.setPen(pen);
painter.drawImage(rect(), *image);
switch (DRAW_STATUS){
case DRAW_START:
break;
case DRAW_ING:
case DRAW_END:
for (int i = 0; i < drawLinePathList.size(); i++){
DrawLinePath drawlinePath = drawLinePathList.at(i);
painter.drawLine(drawlinePath.startX, drawlinePath.startY, drawlinePath.endX, drawlinePath.endY);
}
break;
}
}
void DrawBoard::mouseMoveEvent(QMouseEvent *event){
if (event->buttons()&Qt::LeftButton){
endX = event->x();//得到鼠标的X坐标
endY = event->y();//得到鼠标的Y坐标
qDebug() << event->x() << ",y:" << event->y();
DRAW_STATUS = DRAW_ING;
DrawLinePath drawlinePath{ startX, startY, endX, endY };
drawLinePathList.push_back(drawlinePath);
if (((endX - startX) > 1) || ((endY - startY) > 1))
{
repaint();
}
else{
update();
}
startX = endX;
startY = endY;
}
else if (event->buttons()&Qt::RightButton)
{
}
}
void DrawBoard::mousePressEvent(QMouseEvent *event){
DRAW_STATUS = DRAW_START;
if (event->button() == Qt::LeftButton){
qDebug() << event->x() << ",y:" << event->y();
startX = event->x();//得到鼠标的X坐标
startY = event->y();//的到鼠标的Y坐标
}
else if (event->button() == Qt::RightButton)
{
}
}
DrawBoard::~DrawBoard()
{
}
感觉写的很详细了,那么来张 运行图吧:
/------------------end
这次的博客就在这里了,根据你自己的项目可以实现一些信号,和插槽等,根据项目来。
最后
以上就是安静黑猫为你收集整理的QT自定义控件,以绘画板为列的全部内容,希望文章能够帮你解决QT自定义控件,以绘画板为列所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复