当前位置: 首页 > article >正文

Qt22双缓冲机制

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();
}

运行图

在这里插入图片描述


http://www.kler.cn/a/287501.html

相关文章:

  • 云消息队列 Kafka 版 V3 系列荣获信通院“云原生技术创新标杆案例”
  • LabVIEW实车四轮轮速信号再现系统
  • C++(二十一)
  • Android 12.0 息屏休眠后立即启动屏保功能实现
  • AIGC时代:如何快速搞定Spring Boot+Vue全栈开发
  • lwip单网卡多ip的实现
  • 基于Video-Retalking-Gradio的AI数字人
  • 数学基础 -- 线性代数之正交矩阵
  • 解决 Linux 上的 SSH 登录缓慢问题
  • OpenHarmony 实战开发——应用HAP包签名
  • Node.js sqlite3的db.close方法:正确关闭数据库连接
  • 5G寻呼的 方式
  • Codeforces Round 968 (Div. 2 ABCD1D2题) 视频讲解
  • ElasticSearch常用DSL命令
  • 产品需求过程管理重要性
  • 大语言模型之Qwen2技术报告阅读笔记
  • boot跳转APP,概率性串口失效问题。
  • 【Spring Boot-IDEA创建spring boot项目方法】
  • 微积分复习笔记 Calculus Volume 1 - 1.3Trigonometric Functions
  • 基于yolov5的煤矿传送带异物检测系统python源码+onnx模型+评估指标曲线+精美GUI界面
  • free命令
  • Leetcode-有效的数独
  • 《软件工程导论》(第6版)第5章 总体设计 复习笔记
  • 【论文阅读】Single-Stage Visual Query Localization in Egocentric Videos
  • 【云原生】Mysql 集群技术
  • 【王树森】Few-Shot Learning (2/3): Siamese Network 孪生网络(个人向笔记)