概述
画板的实现
1、设置画板并用白色填充
//设置画板尺寸
this->setFixedSize(1000,618);
myPix = new QPixmap(this->size());
myPix->fill(Qt::white);
2、重写鼠标按下事件、鼠标移动事件、绘画事件
void MyWnd::mousePressEvent(QMouseEvent *e)
{
startPoint = e->pos();
}
void MyWnd::mouseMoveEvent(QMouseEvent *e)
{
//申请一个画家类
QPainter painter(myPix); //依附于自定义的绘图板
QPen pen;
pen.setColor(color);
pen.setWidth(size_1);
//pen.setStyle();
//将画笔和画家结合起来
painter.setPen(pen);
//开始画线
painter.drawLine(startPoint,e->pos());
//将当前点设为新的起点
startPoint = e->pos();
//实时更新
update();
}
void MyWnd::paintEvent(QPaintEvent *)
{
//申请一个画家,依附于this画图板
QPainter painter(this);
//将自定义的绘制板画到画图板上
painter.drawPixmap(0,0,*myPix);
}
通过按钮改变颜色
//对应的处理函数
void MyWnd::change_Color()
{
color = QColorDialog::getColor(Qt::white, this);
}
//按钮的定义
btn1 = new QPushButton();
btn1->setParent(this); //设置父控件
btn1->resize(140,60);
btn1->move(width()/2-140, height()/2+240);
btn1->setText("更改颜色");
//槽函数
connect(btn1, &QPushButton::clicked, this,&MyWnd::change_Color);
通过按钮和文本框改变画笔粗细
//按钮的定义
btn2 = new QPushButton("更改字体",this);
btn2->move(btn1->width()+width()/2-140, height()/2+240);
btn2->resize(btn1->size());
//文本框的定义
Lin = new QLineEdit("请输入字体尺寸",this);
Lin->resize(btn1->size());
Lin->move(btn1->width()+btn2->width()+width()/2-140, height()/2+240);
//对应的处理函数
void MyWnd::change_size()
{
QString val = Lin->text();
double temp;
temp = val.toDouble();
if(temp<=50 && temp>=0)
{
size_1 = temp;
}
}
//对应的槽函数
connect(btn2, &QPushButton::clicked, this,&MyWnd::change_size);
完整代码
1、mywnd.h
#ifndef MYWND_H
#define MYWND_H
#include <QMainWindow>
#include <QPixmap>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPen>
#include <QPoint>
#include <QPushButton>
#include <QLineEdit>
#include <QColor>
#include <QColorDialog>
#include <QString>
class MyWnd : public QMainWindow
{
Q_OBJECT
signals:
void singal_1();
public slots:
void change_Color();
void change_size();
public:
MyWnd(QWidget *parent = nullptr);
~MyWnd();
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void paintEvent(QPaintEvent *);
private:
QPixmap *myPix; //定义绘制类的指针
QPoint startPoint; //画笔的起始位置
//定义两个按钮指针
QPushButton *btn1;
QPushButton *btn2;
QLineEdit *Lin;
QColor color;
double size_1;
};
#endif // MYWND_H
2、mywnd.cpp
#include "mywnd.h"
void MyWnd::change_Color()
{
color = QColorDialog::getColor(Qt::white, this);
}
void MyWnd::change_size()
{
QString val = Lin->text();
double temp;
temp = val.toDouble();
if(temp<=50 && temp>=0)
{
size_1 = temp;
}
}
MyWnd::MyWnd(QWidget *parent)
: QMainWindow(parent)
{
//设置画板尺寸
this->setFixedSize(1000,618);
myPix = new QPixmap(this->size());
myPix->fill(Qt::white);
btn1 = new QPushButton();
btn1->setParent(this); //设置父控件
btn1->resize(140,60);
btn1->move(width()/2-140, height()/2+240);
btn1->setText("更改颜色");
btn2 = new QPushButton("更改字体",this);
btn2->move(btn1->width()+width()/2-140, height()/2+240);
btn2->resize(btn1->size());
Lin = new QLineEdit("请输入字体尺寸",this);
Lin->resize(btn1->size());
Lin->move(btn1->width()+btn2->width()+width()/2-140, height()/2+240);
connect(btn1, &QPushButton::clicked, this,&MyWnd::change_Color);
connect(btn2, &QPushButton::clicked, this,&MyWnd::change_size);
}
MyWnd::~MyWnd()
{
}
void MyWnd::mousePressEvent(QMouseEvent *e)
{
startPoint = e->pos();
}
void MyWnd::mouseMoveEvent(QMouseEvent *e)
{
//申请一个画家类
QPainter painter(myPix); //依附于自定义的绘图板
QPen pen;
pen.setColor(color);
pen.setWidth(size_1);
//pen.setStyle();
//将画笔和画家结合起来
painter.setPen(pen);
//开始画线
painter.drawLine(startPoint,e->pos());
//将当前点设为新的起点
startPoint = e->pos();
//实时更新
update();
}
void MyWnd::paintEvent(QPaintEvent *)
{
//申请一个画家,依附于this画图板
QPainter painter(this);
//将自定义的绘制板画到画图板上
painter.drawPixmap(0,0,*myPix);
}
最后
以上就是风中帆布鞋为你收集整理的2022/9/3——qt中实现调色板的全部内容,希望文章能够帮你解决2022/9/3——qt中实现调色板所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复