13.2、QT::TestQImage操作

前言:

本文操作均为在vs2015+QT5.9.5版本中执行

头文件:testqimage.h

#pragma once

#include <QtWidgets/QWidget>
#include "ui_testqimage.h"

class TestQImage : public QWidget
{
	Q_OBJECT

public:
	TestQImage(QWidget *parent = Q_NULLPTR);
	void paintEvent(QPaintEvent *ev);
private:
	Ui::TestQImageClass ui;
	QImage img;
};

UI文件:testqimage.ui

/********************************************************************************
** Form generated from reading UI file 'testqimage.ui'
**
** Created by: Qt User Interface Compiler version 5.9.5
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_TESTQIMAGE_H
#define UI_TESTQIMAGE_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_TestQImageClass
{
public:

    void setupUi(QWidget *TestQImageClass)
    {
        if (TestQImageClass->objectName().isEmpty())
            TestQImageClass->setObjectName(QStringLiteral("TestQImageClass"));
        TestQImageClass->resize(1280, 720);

        retranslateUi(TestQImageClass);

        QMetaObject::connectSlotsByName(TestQImageClass);
    } // setupUi

    void retranslateUi(QWidget *TestQImageClass)
    {
        TestQImageClass->setWindowTitle(QApplication::translate("TestQImageClass", "TestQImage", Q_NULLPTR));
    } // retranslateUi

};

namespace Ui {
    class TestQImageClass: public Ui_TestQImageClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_TESTQIMAGE_H

主函数:main.cpp

#include "testqimage.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	TestQImage w;
	w.show();
	return a.exec();
}

实现文件:testqimage.cpp

#include "testqimage.h"
#include<QPainter>
#include <QDebug>
TestQImage::TestQImage(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	/*
	img = QImage(1280, 720, QImage::Format_RGBA8888);//宽度以4对齐,也就是必须是四的倍数
	img = QImage(w, h, 图像格式(Format_RGBA8888是一种通用识别的格式));
	*/
	img = QImage(1280, 720, QImage::Format_RGBA8888);
	//填入颜色:(QColor(R, G, B, 透明度))
	img.fill(QColor(255, 0, 0, 200));
//在已对齐情况下遍历设置颜色
   /*
     PS:最好是在构造中处理
	 img.bits()返回指向第一个像素数据的指针。
   */
	uchar *data = img.bits();//获取img像素点(位(bits())),这里最小单位一应该为(字节(Byte));
	//方案一
	for (int i = 0;i<img.width();i++)
	{
		for (int j = 0; j < img.height() / 2; j++)
		{
			/*
			当前画板大小为:1280 * 720 = 921600(Byte)块像素点(PS:这是肉眼可见的);
			像素点为:1280 * 720 * 4 = 3686400(Byte)像素点(PS:这是计算机见到的) ≈ 4(MB);
			   取像素点设置颜色,RGBA需要对每“一块(四位像素点)”单独设置,
			   j * img.width() * 4 + i * 4;
			   高度的开始位置 * 宽度 * 4 + 宽度的开始位置 * 4 = 获取到第一个像素点
			   每当获取四个像素点 = 一块像素;
			   如图所示:□(R)□(G)□(B)□(A) = □//得出一块像素点
			*/
			data[j * img.width() * 4 + i * 4] = 0;
			data[j * img.width() * 4 + i * 4+1] = 255;
			data[j * img.width() * 4 + i * 4+1] = 0;
			data[j * img.width() * 4 + i * 4+1] = 255;
		}
	}
	//方案二
	for (int i = img.width()/2; i < img.width(); i++)
	{		
		for (int j = 0; j < img.height(); j++)
		{
			/*
			void setPixelColor(int x, int y, const QColor &c);
			void setPixelColor(坐标x, 坐标y, 设置颜色RGBA);
			*/
			//设置像素点颜色
			img.setPixelColor(i, j, QColor(0, 0, 0, 255));
			//测试遍历设置颜色过程
			//qDebug() << i << j;
		}
	}

}
void TestQImage::paintEvent(QPaintEvent *ev)
{
	//创建一个画板
	QPainter p(this);
	if (!img.isNull())
	{
		//判断是否为空,不为空就画出来。
		p.drawImage(0, 0, img);
	}
}

ui界面布局样式:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳一航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值