QT界面开发——图像的优化实时显示

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/wangduanqiugao/article/details/83040182

本望断秋高描述QT界面开发——图像的优化实时显示;主要在于图像显示的QLabel类,并使用paintEvent(QPaintEvent *event)优化刷新界面,此类可提供主窗口调用创建,设置图像显示位置,以及界面刷新频率。

图像显示的QLabel类:ShowPicWidget.h

#ifndef SHOWPICWIDGET_H
#define SHOWPICWIDGET_H

#include <QtGui>
#include <QQueue>
#include <QLabel>
#include <QPainter>
#include <QPicture>

class ShowPicWidget : public QLabel
{
	Q_OBJECT

public:
	ShowPicWidget(QWidget *parent = 0);
	~ShowPicWidget();

	int width;
	int height;
	const uchar * data;   //图像信息

protected:
	void paintEvent(QPaintEvent *event);
};

#endif // SHOWPICWIDGET_H

图像显示的QLabel类:ShowPicWidget.cpp

#include "ShowPicWidget.h"

ShowPicWidget::ShowPicWidget( QWidget *parent)
	: QLabel(parent)
{

}

ShowPicWidget::~ShowPicWidget()
{

}

void ShowPicWidget::paintEvent(QPaintEvent *event)
{
	//先调用父类的paintEvent
	QLabel::paintEvent(event);

	QPainter painter(this);
	QImage image(data, width, height, QImage::Format_RGB888);
	painter.drawImage(QRect(0, 0, width, height), image);
}

主窗口调用创建、设置图像显示位置:

#include "ShowPicWidget.h"

ShowPicWidget  * m_ShowPicLab;     //图片显示

m_ShowPicLab = new ShowPicWidget(this);

m_ShowPicLab->setFixedSize(640,480);

m_ShowPicLab->setVisible(TRUE);

m_ShowPicLab->setGeometry(10,115,640,480);

图像界面的频率优化显示:

void QMainWidget::showEvent(QShowEvent *)
{

    if (m_nTimerId == 0)
       {
          //刷新频率
          m_nTimerId = startTimer(100)
       }
}

​void QMainWidget::timerEvent(QTimerEvent *e)
{
	int id = e->timerId();

	if (id == m_nTimerId)
	{
		m_ShowPicLab->width = 640;
                m_ShowPicLab->height = 480;

		m_ShowPicLab->data = NULL; //图像信息
		m_ShowPicLab->update();
	}
}

猜你喜欢

转载自blog.csdn.net/wangduanqiugao/article/details/83040182
今日推荐