Qt——绘图板源码

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QPainter>
#include<QMouseEvent>
#include<QPixmap>
#include<QActionGroup>
#include<QColorDialog>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
    QPen pen;
private slots:
    void menuCommand(QAction *pAct);
    void on_action_5_triggered();
    void on_action_6_triggered();
protected:
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *ev);
    void mouseReleaseEvent(QMouseEvent *ev);
    void mouseMoveEvent(QMouseEvent *ev);
    QPixmap pix,pixTmp;
    QPoint lastPt, endPt;
    int toolSelected;   

    int isDrawing;
};
#endif // MAINWINDOW_H

 

maindindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mydialog.h"

 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    pix = QPixmap(1200,800);
    pix.fill(Qt::white);
    pixTmp = QPixmap(1200,800);
    pixTmp.fill(Qt::white);
    QActionGroup * m_pCmmdActGrp = new QActionGroup(this);
    m_pCmmdActGrp->addAction(ui->action);
    m_pCmmdActGrp->addAction(ui->action_2);
    m_pCmmdActGrp->addAction(ui->action_3);
    m_pCmmdActGrp->addAction(ui->action_4);
    m_pCmmdActGrp->addAction(ui->erase)    ;
    connect(m_pCmmdActGrp,SIGNAL(triggered(QAction*)),this,SLOT(menuCommand(QAction *)));
    toolSelected=0;
    isDrawing=0;
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::menuCommand(QAction *pAct)
{
    pAct->setCheckable(true);
    pAct->setChecked(true);
    if(pAct==ui->action)
    toolSelected=0;
    else if(pAct==ui->action_2)
         toolSelected=1;
    else if(pAct==ui->action_3)
         toolSelected=2;
    else if(pAct==ui->action_4)
         toolSelected=3;
    else if(pAct==ui->erase)
        toolSelected=4;
}
void MainWindow:: paintEvent(QPaintEvent *)
{
QPainter  painter(this);
    if(toolSelected==0)
    {
    QPainter painter2(&pix);
    painter2.setPen(pen);
    if(isDrawing>=2)
    painter2.drawLine(lastPt,endPt);
    lastPt=endPt;
    pixTmp=pix;
    }
    else if(toolSelected<4)
    {
        if(isDrawing>=2)
        {
            pix=pixTmp;
            QRect obj(lastPt,endPt);
            QPainter painter2(&pix);
            painter2.setPen(pen);
            if(toolSelected==1)
               painter2.drawLine(lastPt,endPt);
            else if(toolSelected==2)
                painter2.drawEllipse(obj);
            else if(toolSelected==3)
                painter2.drawRect(obj);
        }
         if(isDrawing==3)
         {
             isDrawing=0;
             pixTmp=pix;
         }
    }
    else if(toolSelected==4)
    {
        QPen pen2;
        pen2.setColor(Qt::white);
        pen2.setWidth(30);
        QPainter painter2(&pix);
        painter2.setPen(pen2);
        if(isDrawing>=2)
        painter2.drawLine(lastPt,endPt);
        lastPt=endPt;
        pixTmp=pix;
    }
    painter.drawPixmap(0,80,pix);
}
void MainWindow::mousePressEvent(QMouseEvent *ev)
{
  if(ev->button()==Qt::LeftButton)
  {
    lastPt=ev->pos();
    lastPt.setY(lastPt.y()-80);
    isDrawing=1;
  }
}
void MainWindow::mouseMoveEvent(QMouseEvent *ev)
{
  if(isDrawing==0) return;
  if(ev->buttons()==Qt::LeftButton)
  {
      endPt=ev->pos();
      endPt.setY(endPt.y()-80);
      isDrawing=2;
      update();
  }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *ev)
{
    if(ev->button()==Qt::LeftButton)
    {
        endPt=ev->pos();
        endPt.setY(endPt.y()-80);
        isDrawing=3;
        update();
    }
}
void MainWindow::on_action_5_triggered()
{
    QColorDialog *colordialog = new QColorDialog(this);
    colordialog->setCurrentColor(QColor(Qt::black));
    if(QDialog::Accepted==colordialog->exec())
    {
        pen.setColor(colordialog->currentColor());
    }
}
void MainWindow::on_action_6_triggered()
{
 myDialog *dialog = new myDialog(this);
 if(dialog->exec()==QDialog::Accepted)
 {
     pen.setWidth(dialog->setting());
 }
}
dialog.h:
#ifndef MYDIALOG_H
#define MYDIALOG_H

 
#include <QDialog>
#include<QListWidget>

 
namespace Ui {
class myDialog;
}

 
class myDialog : public QDialog
{
    Q_OBJECT

 
public:
    explicit myDialog(QWidget *parent = 0);
    ~myDialog();
int setting();
private slots:
    void on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
private:
    Ui::myDialog *ui;
    int pos;
};

 
#endif // MYDIALOG_H
dialog.cpp:
#include "mydialog.h"
#include "ui_mydialog.h"

 
myDialog::myDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::myDialog)
{
    ui->setupUi(this);
    pos=1;
}
myDialog::~myDialog()
{
    delete ui;
}
void myDialog::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    ui->lineEdit->setText(current->text());

 
    if(ui->lineEdit->text()=="1")
       pos=1;
    else  if(ui->lineEdit->text()=="2")
     pos=2;
    else  if(ui->lineEdit->text()=="3")
       pos=3;
    else  if(ui->lineEdit->text()=="4")
      pos=4;
    else  if(ui->lineEdit->text()=="5")
      pos=5;
    else  if(ui->lineEdit->text()=="6")
       pos=6;
    else  if(ui->lineEdit->text()=="7")
       pos=7;
     else  if(ui->lineEdit->text()=="8")
      pos=8;
     else  if(ui->lineEdit->text()=="9")
      pos=9;
     else  if(ui->lineEdit->text()=="10")
     pos=10;
     else  if(ui->lineEdit->text()=="12")
      pos=12;
     else  if(ui->lineEdit->text()=="14")
      pos=14;
     else  if(ui->lineEdit->text()=="16")
      pos=16;
     else  if(ui->lineEdit->text()=="18")
      pos=18;
     else  if(ui->lineEdit->text()=="20")
        pos=20;
     else  if(ui->lineEdit->text()=="25")
      pos=25;
    else  if(ui->lineEdit->text()=="30")
      pos=30;
     else  if(ui->lineEdit->text()=="40")
      pos=40;
}
int myDialog::setting()
{
    return pos;
}

 
展开阅读全文

没有更多推荐了,返回首页