qt 用数据画一个图,并表示出来

1.概要

想用数据绘制一个画面,看有相机到播放的本质是啥。

要点

    // 创建一个QImage对象,指定图像的宽度、高度和格式
    QImage image(width, height, QImage::Format_Grayscale8);

    // 将像素数据复制到QImage对象中
    memcpy(image.bits(), pixelData, width * height * sizeof(unsigned char));

    // 将 QImage 转换为 QPixmap
    QPixmap pixmap = QPixmap::fromImage(image);

    // 设置 QLabel 的 pixmap
    ui->label->setPixmap(pixmap.scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation));

2.代码

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    unsigned char pixelData[128*50]; // 示例数据
    for(int i=0;i<(128*50);i++){
        if(i%128<50||i%128>100){
            pixelData[i] = 0x00;
        }else{
            pixelData[i] = 0xff;
        }
    }

    int width = 128; // 图像的宽度
    int height = 50; // 图像的高度

    // 创建一个QImage对象,指定图像的宽度、高度和格式
    QImage image(width, height, QImage::Format_Grayscale8);

    // 将像素数据复制到QImage对象中
    memcpy(image.bits(), pixelData, width * height * sizeof(unsigned char));

    // 假设你已经有了一个 QImage 对象,这里我们创建一个示例 QImage
    //QImage image(100, 100, QImage::Format_ARGB32);

    //image.fill(Qt::blue); // 填充为蓝色,仅作为示例

    // 将 QImage 转换为 QPixmap
    QPixmap pixmap = QPixmap::fromImage(image);

    // 设置 QLabel 的 pixmap
    ui->label->setPixmap(pixmap.scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

 

#include "widget.h"

#include <QApplication>

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

 

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

3.运行效果

相关推荐

  1. html一个烟花特效

    2024-07-11 13:48:03       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 13:48:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 13:48:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 13:48:03       57 阅读
  4. Python语言-面向对象

    2024-07-11 13:48:03       68 阅读

热门阅读

  1. SpringBoot防止重复提交 AOP+自定义注解+redis

    2024-07-11 13:48:03       22 阅读
  2. 在Spring Boot中实现多租户架构的数据隔离

    2024-07-11 13:48:03       21 阅读
  3. LeetCode --- 2119. A Number After a Double Reversal 解题报告

    2024-07-11 13:48:03       19 阅读
  4. sublime使用

    2024-07-11 13:48:03       21 阅读
  5. Linux Conda简介

    2024-07-11 13:48:03       21 阅读
  6. 数据结构笔记之线索二叉树找前驱后继

    2024-07-11 13:48:03       21 阅读
  7. Mybatis之动态sql、缓存、分页、配置数据源

    2024-07-11 13:48:03       17 阅读