QImage and QPixmap
-
Hi all,
I learnt the difference between the QImage and QPixmap as
The QPixmap class is an off-screen image representation that can be used as a paint device.The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device.
I have QImage and want to display in QGraphicsView in a fast way.
The other person has shown displaying images 800 updates/sec (I assume it is 800 frames/sec) using QPixmap; as shown in the example below.
The link is
http://www.qtcentre.org/threads/6929-Any-fast-way(-lt-10ms)-to-display-640*480-QImage-on-QGraphicsSceneMy query is I have QImage and to get his performance,
(1) do I need to change QImage to QPixma?
(2)If not necessary, he used QGraphicsPixmapItem* pixmapItem; to update to scene->addItem(pixmapItem);
for my QImage, just add QImage to the scene?The sample program is
main.h
@
#ifndef MAIN_H
#define MAIN_H#include <QtGui>
//#include <QtOpenGL>class ImageSource : QObject
{ Q_OBJECT
public:
ImageSource(QString text = "Hello!",int w = 640,int h = 480) : m_width(w),m_height(h)
{
if (text.length() == 0) text = "Hello!";
// Create pixmaps for each character
for (int i=0;i<text.length();++i)
{
QString imageText(text.length(),' ');
imageText[i] = text.at(i);
QPixmap* pm = new QPixmap(w,h);
pm->fill(Qt::white);
QPainter p(pm);
p.setFont(QFont("Courier"));
p.setPen(Qt::black);
p.drawText(QRectF(0,0,w,h),Qt::AlignCenter,imageText);
dummies.append(pm);
}
current = dummies.length();
}
QPixmap* nextImage()
{
--current;
if (current < 0)
current = dummies.length()-1;
//return dummies.at(3);
return dummies.at(current);
}
int width() {return m_width;}
int height() {return m_height;}
private:
QList<QPixmap* > dummies;
int current;
int m_width;
int m_height;
};class MainWindow : public QMainWindow
{ Q_OBJECT
public:
MainWindow()
{
scene = new QGraphicsScene(this);imageSrc = new ImageSource(); pixmapItem = new QGraphicsPixmapItem(); scene->addItem(pixmapItem); view = new QGraphicsView(); //view->setViewport(new QGLWidget(QGLFormat())); view->setScene(scene); setCentralWidget(view); QTimer* updateTimer = new QTimer(this); QObject::connect(updateTimer,SIGNAL(timeout()),this,SLOT(doUpdate())); updateTimer->start(1); QTimer* fpsTimer = new QTimer(this); QObject::connect(fpsTimer,SIGNAL(timeout()),this,SLOT(showFPS())); fpsTimer->start(1000); } ~MainWindow() {}
protected slots:
void doUpdate()
{
pixmapItem->setPixmap(*imageSrc->nextImage());
++frameCounter;
}
void showFPS()
{
setWindowTitle(QString("FPS: %1").arg(frameCounter));
frameCounter = 0;
}private:
QGraphicsScene* scene;
QGraphicsView* view;
ImageSource* imageSrc;
QGraphicsPixmapItem* pixmapItem;
int frameCounter;
};#endif // MAIN_H
@main.cpp:
@
#include <QtCore>
#include <QtGui>#include "main.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);MainWindow mw; mw.show(); return a.exec();
}
@
pro-file:
@
//QT += opengl
TARGET = FastImages
TEMPLATE = app
SOURCES += main.cpp
HEADERS +=
main.h
@[Edit: Please be sure to wrap code in @ tags for formatting -- mlong]