创建好QT工程后,重载绘制画面函数。
思路:
1.重新定义QT的绘制函数
2.根据void TestRGB::paintEvent(QPaintEvent* ev)函数里的进行思路进行编程
main.cpp
#include "testrgb.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TestRGB w;
w.show();
return a.exec();
}
testrgb.cpp
#include "testrgb.h"
#include <QPainter>
#include <QImage>
static int w = 1280;
static int h = 720;
TestRGB::TestRGB(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
resize(w,h);//窗口大小
}
void TestRGB::paintEvent(QPaintEvent* ev)
{
QImage img(w, h, QImage::Format_RGB888);
auto d = img.bits();
unsigned char r = 255;
for (int j = 0; j < h; j++)
{
r--;
int b = j * w * 3;
for (int i = 0; i < w * 3; i += 3)
{
d[b + i] = r;//R
d[b + i + 1] = 0;//G
d[b + i + 2] = 0;//B
}
}
QPainter p;
p.begin(this);
p.drawImage(0, 0, img);
p.end();
}
testrgb.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_testrgb.h"
class TestRGB : public QWidget
{
Q_OBJECT
public:
TestRGB(QWidget *parent = Q_NULLPTR);
//重载绘制画面函数
void paintEvent(QPaintEvent* ev) override;
private:
Ui::TestRGBClass ui;
};