Using QLabel to play grey camera video(setPixmap) causes flickering
-
Hi, everyone.
I am trying to play camera by using QLabel and its setPixmap.
When I tested it using my notebook's camera and found no problem.
But when I use a mono camera (Point Grey's Flea3 GigE camera).it will lead to serious flickering.
I searched a lot over internet.
It seems that using double-buffer method can remove flickering. But in Qt 4, the widget is said to use double buffer in default.
So I wonder what can I do . I don't want to turn to openGL( I am not familiar with it).
The code is from open source project" qt-opencv-multithreaded ":http://code.google.com/p/qt-opencv-multithreaded/
I use its FrameLabel (inherited from QLabel) calss.
@
#ifndef FRAMELABEL_H
#define FRAMELABEL_H#include "Structures.h"
// Qt header files
#include <QtGui>class FrameLabel : public QLabel
{
Q_OBJECTpublic:
FrameLabel(QWidget *parent = 0);
void setMouseCursorPos(QPoint);
QPoint getMouseCursorPos();
private:
MouseData mouseData;
QPoint startPoint;
QPoint mouseCursorPos;
bool drawBox;
bool drawGrid;
QRect *box;protected:
void mouseMoveEvent(QMouseEvent *ev);
void mousePressEvent(QMouseEvent *ev);
void mouseReleaseEvent(QMouseEvent *ev);
void paintEvent(QPaintEvent *ev);
signals:
void newMouseData(struct MouseData mouseData);
void onMouseMoveEvent();
};#endif // FRAMELABEL_H
@
@
#include "FrameLabel.h"FrameLabel::FrameLabel(QWidget *parent) : QLabel(parent)
{
// Initialize variables
startPoint.setX(0);
startPoint.setY(0);
mouseCursorPos.setX(0);
mouseCursorPos.setY(0);
drawBox=false;
// Initialize MouseData structure
mouseData.leftButtonRelease=false;
mouseData.rightButtonRelease=false;
this->setAlignment(Qt::AlignCenter);//this->setBackgroundRole(QPalette::NoRole);
} // FrameLabel constructor
void FrameLabel::mouseMoveEvent(QMouseEvent *ev)
{
// Save mouse cursor position
setMouseCursorPos(ev->pos());
// Update box width and height if box drawing is in progress
if(drawBox)
{
box->setWidth(getMouseCursorPos().x()-startPoint.x());
box->setHeight(getMouseCursorPos().y()-startPoint.y());
}
// Inform main window of mouse move event
emit onMouseMoveEvent();
} // mouseMoveEvent()void FrameLabel::setMouseCursorPos(QPoint input)
{
mouseCursorPos=input;
} // setMouseCursorPos()QPoint FrameLabel::getMouseCursorPos()
{
return mouseCursorPos;
} // getMouseXPos()void FrameLabel::mouseReleaseEvent(QMouseEvent *ev)
{
// Update cursor position
setMouseCursorPos(ev->pos());
// On left mouse button release
if(ev->button()==Qt::LeftButton)
{
// Set leftButtonRelease flag to TRUE
mouseData.leftButtonRelease=true;
if(drawBox)
{
// Stop drawing box
drawBox=false;
// Save box dimensions
mouseData.selectionBox.setX(box->left());
mouseData.selectionBox.setY(box->top());
mouseData.selectionBox.setWidth(box->width());
mouseData.selectionBox.setHeight(box->height());
// Set leftButtonRelease flag to TRUE
mouseData.leftButtonRelease=true;
// Inform main window of event
emit newMouseData(mouseData);
}
// Set leftButtonRelease flag to FALSE
mouseData.leftButtonRelease=false;
}
// On right mouse button release
else if(ev->button()==Qt::RightButton)
{
// If user presses (and then releases) the right mouse button while drawing box, stop drawing box
if(drawBox)
drawBox=false;
else
{
// Set rightButtonRelease flag to TRUE
mouseData.rightButtonRelease=true;
// Inform main window of event
emit newMouseData(mouseData);
// Set rightButtonRelease flag to FALSE
mouseData.rightButtonRelease=false;
}
}
} // mouseReleaseEvent()void FrameLabel::mousePressEvent(QMouseEvent *ev)
{
// Update cursor position
setMouseCursorPos(ev->pos());;
if(ev->button()==Qt::LeftButton)
{
// Start drawing box
startPoint=ev->pos();
box=new QRect(startPoint.x(),startPoint.y(),0,0);
drawBox=true;
}
} // mousePressEvent()void FrameLabel::paintEvent(QPaintEvent *ev)
{
QLabel::paintEvent(ev);
QPainter painter(this);
// Draw box
if(drawBox)
{
painter.setPen(Qt::blue);
painter.drawRect(*box);
}
} // paintEvent()@
In main program,
@
FrameLabel frameLabel;
frameLabel->setPixmap(QPixmap::fromImage(frame));
@Thanks for your great help.