嵌入式学习笔记2——控件实现画画

1.使用三个slider控制画笔RGB,使用三个dial控制画刷RGB,并在屏幕用色块显示选取的颜色
例如:
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
pencolor=QColor(value,ui->horizontalSlider_2->value(),ui->horizontalSlider_3->value());//改变R值
QBrush brush(pencolor,Qt::SolidPattern);//给pencolor赋予新值
pp->setBrush(brush);
pp->drawRect(1000,400,50,50);
this->update();
}
void MainWindow::on_dial_valueChanged(int value)
{
brushcolor=QColor(value,ui->dial_2->value(),ui->dial_3->value());//改变R值
QBrush brush(brushcolor,Qt::SolidPattern);//给brushcolor赋予新值
pp->setBrush(brush);
pp->drawRect(1000,710,50,50);
this->update();
}

嵌入式学习笔记2——控件实现画画_第1张图片
2.遇到的错误嵌入式学习笔记2——控件实现画画_第2张图片
解决方法是添加缺少的头文件或删去多余的头文件。

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    int mode;//用来区分四种绘图模式
    int xx,yy;//存左上角坐标
    QPixmap aaaa;
    QPainter *pp;    
    QColor pencolor;
    QColor brushcolor;
    QFont ffff;
    void paintEvent(QPaintEvent *e);
    void mousePressEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
private:
    Ui::MainWindow *ui;

private slots:
    void on_dial_3_valueChanged(int value);
    void on_dial_2_valueChanged(int value);
    void on_horizontalSlider_3_valueChanged(int value);
    void on_horizontalSlider_2_valueChanged(int value);
    void on_dial_valueChanged(int value);
    void on_horizontalSlider_valueChanged(int value);
    void on_radioButton_clicked();
    void on_radioButton_2_clicked();
    void on_radioButton_3_clicked();
    void on_radioButton_4_clicked();
};

#endif // MAINWINDOW_H

*构造函数*
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    aaaa=QPixmap(QSize(1250,1000));//假设这么大,大小自己定
    pp=new QPainter(&aaaa);
    mode=0; //初始化成画线模式
    aaaa.fill(Qt::white);
    ffff=this->font();
    QColor pencolor;
    QColor brushcolor;

    pencolor=QColor(0,0,0);
    brushcolor=QColor(255,255,255);

    ui->spinBox->setRange(1,10);
    ui->spinBox->setValue(1);

    ui->horizontalSlider->setRange(0,255);
    ui->horizontalSlider->setValue(0);
    ui->horizontalSlider_2->setRange(0,255);
    ui->horizontalSlider_2->setValue(0);
    ui->horizontalSlider_3->setRange(0,255);
    ui->horizontalSlider_3->setValue(0);

    ui->dial->setRange(0,255);
    ui->dial->setValue(0);
    ui->dial_2->setRange(0,255);
    ui->dial_2->setValue(0);
    ui->dial_3->setRange(0,255);
    ui->dial_3->setValue(0);

    this->setGeometry(0,0,750,1000);

    QPen ppc(QColor(0,0,0),1,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin);
    pp->setPen(ppc);

    QBrush pc(pencolor,Qt::SolidPattern);
    pp->setBrush(pc);
    pp->drawRect(1000,400,50,50);//显示pen色块区域

    QBrush pb(brushcolor,Qt::SolidPattern);
    pp->setBrush(pb);
    pp->drawRect(1000,710,50,50);//显示brush色块区域
    this->update();
}

MainWindow::~MainWindow()
{
    delete ui;
    delete pp;
}

void MainWindow::paintEvent(QPaintEvent*)
{
    QPainter p(this);
    p.drawPixmap(0,0,1250,1000,aaaa,0,0,1250,1000);//复制画布内容
    p.drawPixmap(1000,400,50,50,aaaa,1000,400,50,50);//复制画笔色块
    p.drawPixmap(1000,710,50,50,aaaa,1000,710,50,50);//复制画栓色块
}

void MainWindow::on_radioButton_clicked()
{
    mode=0;
}

void MainWindow::on_radioButton_2_clicked()
{
    mode=1;
}

void MainWindow::on_radioButton_3_clicked()
{
    mode=2;
}

void MainWindow::on_radioButton_4_clicked()
{
    mode=3;
}


//鼠标按下事件
void MainWindow::mousePressEvent(QMouseEvent *e)
{
    if(e->button()==Qt::LeftButton)
    {
        xx=e->pos().x();
        yy=e->pos().y();
    }
}

//鼠标弹起事件
void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button()==Qt::LeftButton){

        if(mode==0) {//1
            QPen pen(pencolor,ui->spinBox->value(),Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin);
            pp->setPen(pen);
            pp->drawLine(xx,yy,e->pos().x(),e->pos().y());
            this->update();
        }

        if(mode==1) {//4
            QBrush brush(brushcolor,Qt::SolidPattern);
            QPen pen(pencolor,ui->spinBox->value(),Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin);
            pp->setPen(pen);
            if(ui->checkBox->isChecked())
            {
                brush.setStyle(Qt::NoBrush);
            }
            pp->setBrush(brush);
            pp->drawRect(xx,yy,e->pos().x()-xx,e->pos().y()-yy);
            this->update();
        }

        if(mode==2){//0
            QBrush brush(brushcolor,Qt::SolidPattern);
            QPen pen(pencolor,ui->spinBox->value(),Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin);
            pp->setPen(pen);
            if(ui->checkBox->isChecked())
            {
                brush.setStyle(Qt::NoBrush);
            }
            pp->setBrush(brush);
            pp->drawEllipse(xx,yy,e->pos().x()-xx,e->pos().y()-yy);
            this->update();
        }

        if(mode==3){//char
            QPen pen(pencolor,ui->spinBox->value(),Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin);
            pp->setPen(pen);
            QFont font=this->font();//获取当前字体
            font.setPointSize(e->pos().y()-yy);//由鼠标拖拉高度设置字大小
            pp->setFont(font);//pp选择字体
            pp->drawText(xx,yy,ui->lineEdit->text());
            this->update();
        }
    }
}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    pencolor=QColor(value,ui->horizontalSlider_2->value(),ui->horizontalSlider_3->value());
    QBrush brush(pencolor,Qt::SolidPattern);
    pp->setBrush(brush);
    pp->drawRect(1000,400,50,50);
    this->update();
}

void MainWindow::on_dial_valueChanged(int value)
{
    brushcolor=QColor(value,ui->dial_2->value(),ui->dial_3->value());
    QBrush brush(brushcolor,Qt::SolidPattern);
    pp->setBrush(brush);
    pp->drawRect(1000,710,50,50);
    this->update();
}


void MainWindow::on_horizontalSlider_2_valueChanged(int value)
{
    pencolor=QColor(ui->horizontalSlider->value(),value,ui->horizontalSlider_2->value());
    QBrush brush(pencolor,Qt::SolidPattern);
    pp->setBrush(brush);
    pp->drawRect(1000,400,50,50);
    this->update();
}

void MainWindow::on_horizontalSlider_3_valueChanged(int value)
{
    pencolor=QColor(ui->horizontalSlider->value(),ui->horizontalSlider_2->value(),value);
    QBrush brush(pencolor,Qt::SolidPattern);
    pp->setBrush(brush);
    pp->drawRect(1000,400,50,50);
    this->update();
}

void MainWindow::on_dial_2_valueChanged(int value)
{
    brushcolor=QColor(ui->dial->value(),value,ui->dial_3->value());
    QBrush brush(brushcolor,Qt::SolidPattern);
    pp->setBrush(brush);
    pp->drawRect(1000,710,50,50);
    this->update();
}

void MainWindow::on_dial_3_valueChanged(int value)
{
    brushcolor=QColor(ui->dial->value(),ui->dial_2->value(),value);
    QBrush brush(brushcolor,Qt::SolidPattern);
    pp->setBrush(brush);
    pp->drawRect(1000,710,50,50);
    this->update();
}



你可能感兴趣的:(嵌入式学习笔记2——控件实现画画)