Qt22双缓冲机制
- 知识点
- drawwidget
- drawwidget.h
- drawwidget.cpp
- mainwindow
- mainwindow.h
- mainwindow.cpp
- main.cpp
- 运行图
知识点
双缓冲就是在内存区申请一块缓存;然后显卡直接从这块内存读取数据.。
这样就不用鼠标边画,经过IO来读取这个环节;
鼠标驱动 — 内存缓冲区— 显卡
drawwidget
drawwidget.h
#ifndef DRAWWIDGET_H
#define DRAWWIDGET_H
#include <QMainWindow>
#include <QtGui>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QColor>
#include <QPixmap>
#include <QPoint>
#include <QPainter>
#include <QPalette>
#include <QPen>
class drawWidget : public QWidget
{
Q_OBJECT
public:
explicit drawWidget(QWidget* parent = 0);
public:
void mousePressEvent(QMouseEvent*);
void mouseMoveEvent(QMouseEvent*);
void paintEvent(QPaintEvent*);
void resizeEvent(QResizeEvent*);
public slots:
void setStyle(int);
void setWidth(int);
void setColor(QColor);
void clear();
private:
QPixmap* pix;
QPoint startPos;
QPoint endPos;
int style;
int weight;
QColor color;
};
#endif // DRAWWIDGET_H
drawwidget.cpp
#include "drawwidget.h"
drawWidget::drawWidget(QWidget *parent)
: QWidget(parent)
{
setAutoFillBackground(true);
setPalette(QPalette(Qt::white));
pix = new QPixmap(size());
pix->fill(Qt::white);
setMinimumSize(600,400);
}
void drawWidget::mousePressEvent(QMouseEvent * e)
{
startPos = e->pos();
}
void drawWidget::mouseMoveEvent(QMouseEvent *e)
{
QPainter *painter = new QPainter;
QPen pen;
pen.setStyle((Qt::PenStyle)style);
pen.setWidth(weight);
pen.setColor(color);
painter->begin(pix);
painter->setPen(pen);
painter->drawLine(startPos,e->pos());
painter->end();
startPos = e->pos();
update();
}
void drawWidget::paintEvent(QPaintEvent *)
{
//把图片内存的数据 绘制到窗体控件中
QPainter painter(this);
painter.drawPixmap(QPoint(0,0),*pix);
}
void drawWidget::resizeEvent(QResizeEvent *event)
{
if(height()>pix->height() || width()>pix->width())//如果图片尺寸发生改变
{
QPixmap *newPix = new QPixmap(size());
newPix->fill(Qt::white);
QPainter p(newPix);
p.drawPixmap(QPoint(0,0),*pix);
pix = newPix;
}
QWidget::resizeEvent(event);
}
void drawWidget::setStyle(int s)
{
style = s;
}
void drawWidget::setWidth(int w)
{
weight = w;
}
void drawWidget::setColor(QColor c)
{
color = c;
}
void drawWidget::clear()
{
QPixmap *clearPix = new QPixmap(size());
clearPix->fill(Qt::white);
pix = clearPix;
update();
}
mainwindow
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QToolButton>
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include <QToolBar>
#include <QColorDialog>
#include "drawwidget.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void createToolBar();
signals:
public slots:
void ShowStyle();
void ShowColor();
private:
drawWidget *drawwidget;
QLabel* styleLabel;
QComboBox* styleComboBox;
QLabel* widthLabel;
QSpinBox* widthSpinBox;
QToolButton* colorBtn;
QToolButton* clearBtn;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow{parent}
{
drawwidget = new drawWidget;
setCentralWidget(drawwidget);
createToolBar();
setMinimumSize(600,400);
ShowStyle();
drawwidget->setWidth(widthSpinBox->value());
drawwidget->setColor(Qt::black);
}
MainWindow::~MainWindow()
{
}
void MainWindow::createToolBar()
{
QToolBar* toolBar = addToolBar("Tool");
styleLabel = new QLabel(tr("线型风格:"));
styleComboBox = new QComboBox;
styleComboBox->addItem(tr("SolidLine"),static_cast<int>(Qt::SolidLine));
styleComboBox->addItem(tr("DashLine"),static_cast<int>(Qt::DashLine));
styleComboBox->addItem(tr("DotLine"),static_cast<int>(Qt::DotLine));
styleComboBox->addItem(tr("DashDotLine"),static_cast<int>(Qt::DashDotLine));
styleComboBox->addItem(tr("DashDotDotLine"),static_cast<int>(Qt::DashDotDotLine));
connect(styleComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),this,&MainWindow::ShowStyle);
widthLabel = new QLabel(tr("线宽:"));
widthSpinBox =new QSpinBox;
connect(widthSpinBox,static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),drawwidget,&drawWidget::setWidth);
colorBtn = new QToolButton;
QPixmap pixmap(20,20);
pixmap.fill(Qt::black);
colorBtn->setIcon(QIcon(pixmap));
connect(colorBtn,&QToolButton::clicked,this,&MainWindow::ShowColor);
clearBtn = new QToolButton;
clearBtn->setText(tr("清除"));
connect(clearBtn,&QToolButton::clicked,drawwidget,&drawWidget::clear);
toolBar->addWidget(styleLabel);
toolBar->addWidget(styleComboBox);
toolBar->addWidget(widthLabel);
toolBar->addWidget(widthSpinBox);
toolBar->addWidget(colorBtn);
toolBar->addWidget(clearBtn);
}
void MainWindow::ShowStyle()
{
drawwidget->setStyle(styleComboBox->itemData(styleComboBox->currentIndex(),Qt::UserRole).toInt());
}
void MainWindow::ShowColor()
{
QColor color =QColorDialog::getColor(static_cast<int>(Qt::black));
if(color.isValid())
{
drawwidget->setColor(color);
QPixmap p(20,20);
p.fill(color);
colorBtn->setIcon(QIcon(p));
}
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QFont>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFont font("ZYSong18030",12);
a.setFont(font);
MainWindow w;
w.show();
return a.exec();
}