- 论坛徽章:
- 0
|
6可用积分
我想在qt4下使用定时器连续播放bmp图像,可是只播放了第一幅后就不动了,貌似timerEvent只被调用了一次,这是为什么啊?
具体问题描述:将按钮openimage的clicked信号和槽openimage()关联,在openimage()里调用myTimerId = startTimer(40)启动定时器,在类Mywindow里重载void timerEvent(QTimerEvent *event),timerEvent用来连续播放图像,关键步骤及代码如下:
首先打开图像文件temp.dat(temp.dat为多幅256*256的图像塑像数据文件):
QFile file1("temp.dat",this);
然后定位、读取、显示:
file1.open(QIODevice::ReadWrite);
file1.seek(65536*framenum);//显示一帧,framenum为当前显示的帧号
//读图像数据 256*256,bmp的文件头已经读到buf的前1078个字节里了
qint64 lineLength=file1.read( (char *)(buf+107 , (qint64)65536 );
QImage image;
image.loadFromData ( buf,66614,"BMP" );
QPixmap pm;
pm.convertFromImage(image,0);
imagelabel->setPixmap(pm);
repaint();
framenum++;//显示下一帧
源码附上,qt新手,代码比较混乱,忘海涵!(代码是qt3移植到qt4的)
/*mywindow.h*/
#ifndef MYWINDOW_H
#define MYWINDOW_H
#include <qwidget.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qpixmap.h>
#include <qslider.h>
#include <qimage.h>
#include <q3filedialog.h>
#include <qscrollbar.h>
//Added by qt3to4:
#include <QTimerEvent>
#include <QHideEvent>
class QT;
class Mywindow:public QWidget
{
Q_OBJECT
public:
Mywindow(QWidget *parent=0,const char *name=0);
~Mywindow();
uchar buf[66614];
int totalnum;
int framenum;
int myTimerId;
protected:
void timerEvent(QTimerEvent *event);
private slots:
void loadImage();
void openimage();
private:
QImage image;
QPixmap pm;
QPixmap pmScaled;
QPushButton *openbutton;
QPushButton *closebutton;
QPushButton *buttonthree;
QPushButton *buttonfour;
QLabel *imagelabel;
QLabel *tiplabel;
QLabel *totalnumlabel;
QScrollBar *scrollBar;
QSlider *bright_slider;
int pickx, picky;
int clickx, clicky;
};
#endif
/*mywindow.cpp*/
#include <mywindow.h>
#include <qnamespace.h>
#include <qapplication.h>
#include <qpainter.h>
#include <qstring.h>
#include <qmessagebox.h>
//Added by qt3to4:
#include <Q3VBoxLayout>
#include <Q3HBoxLayout>
#include <QLabel>
//Added by qt3to4:
#include <QTimerEvent>
Mywindow::Mywindow(QWidget * parent,const char * name)
Widget(parent,name)
{
Q3VBoxLayout *rightLayout=new Q3VBoxLayout;
totalnumlabel=new QLabel("totalnum :",this);
totalnumlabel->setMinimumSize(15,80);
rightLayout->addWidget(totalnumlabel);
tiplabel=new QLabel("tips ",this);
tiplabel->setMinimumSize(15,80);
rightLayout->addWidget(tiplabel);
imagelabel=new QLabel(" ",this);
imagelabel->setMinimumSize(400,400);
rightLayout->addWidget(imagelabel);
Q3VBoxLayout *leftLayout=new Q3VBoxLayout;
openbutton=new QPushButton("showimage",this);
openbutton->setMaximumSize(openbutton->sizeHint());
leftLayout->addWidget(openbutton); //open pic
QLabel *imagelabel=new QLabel(this);
//QObject::connect(openbutton,SIGNAL(clicked()),this,SLOT(loadImage()));
QObject::connect(openbutton,SIGNAL(clicked()),this,SLOT(openimage()));
closebutton=new QPushButton("close",this);
closebutton->setMaximumSize(closebutton->sizeHint());
leftLayout->addWidget(closebutton); //close
QObject::connect(closebutton,SIGNAL(clicked()),qApp,SLOT(quit()));
buttonthree=new QPushButton("stop",this);
//QObject::connect(buttonthree,SIGNAL(clicked()),this,SLOT(hideEvent(QHideEvent *)));
buttonthree->setMaximumSize(buttonthree->sizeHint());
leftLayout->addWidget(buttonthree); //analysis
buttonfour=new QPushButton("buttonfour",this);
buttonfour->setMaximumSize(buttonfour->sizeHint());
leftLayout->addWidget(buttonfour); //change size of pic
bright_slider=new QSlider(Qt::Horizontal, this, "bright_slider" ;
//bright_slider->setTickmarks(QSlider::Above);
bright_slider->setGeometry(0,20,20,10);
leftLayout->addWidget(bright_slider); //change bright
//connect(bright_slider,SIGNAL(valueChanged(int)),this,SLOT(setValue(int)));
Q3HBoxLayout *mainLayout=new Q3HBoxLayout(this);
mainLayout->setMargin(11);
mainLayout->setSpacing(6);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
totalnum=0;
framenum=0;
myTimerId = 0;
memset(buf,0,66614);
QFile file("header.dat" ;//bmp的头文件
file.open(QIODevice::ReadWrite);
file.read( (char *)buf, (qint64)1078 );//readline不对!
file.close();
}
Mywindow::~Mywindow()
{
}
void Mywindow: penimage( )
{
QFile file1("temp.dat",this);//temp.dat为保存的多幅图像像素数据 256*256
file1.open(QIODevice::ReadWrite);
totalnum=file1.size()/65536; //文件长度
QString str;
str.setNum(totalnum);
totalnumlabel->setText(str);
file1.close();
myTimerId = startTimer(40);
}
void Mywindow::timerEvent(QTimerEvent *event)
{
if (event->timerId() == myTimerId)
{
QString str;
str.setNum(framenum);
tiplabel->setText(str);//当前帧
QFile file1("temp.dat",this);
file1.open(QIODevice::ReadWrite);
file1.seek(65536*framenum);//显示一帧
//读图像数据 256*256
qint64 lineLength=file1.read( (char *)(buf+107 , (qint64)65536 );
file1.close();
if(lineLength<=0)
{
perror("read data!" ;
exit(0);
}
//QImage image(buf, 256, 256, QImage::Format_Indexed ;
bool ok = false;
QImage image;
ok = image.loadFromData ( buf,66614,"BMP" );
if(ok==FALSE)
{
perror("load data!" ;
exit(0);
}
ok=pm.convertFromImage(image,0);
imagelabel->setPixmap(pm);
repaint();
framenum++;
if(framenum=totalnum)
{
killTimer ( myTimerId );
myTimerId=0;
}
}
else
{
QWidget::timerEvent(event);
}
}
void Mywindow::loadImage()
{
QFile file1("temp.dat",this);
file1.open(QIODevice::ReadWrite);
file1.seek(65536*framenum);
qint64 lineLength=file1.read( (char *)(buf+107 , (qint64)65536 );
if(lineLength<=0)
{
perror("read data!" ;
exit(0);
}
//QImage image(buf, 256, 256, QImage::Format_Indexed ;
bool ok = false;
QImage image;
ok = image.loadFromData ( buf,66614,"BMP" );
if(ok==FALSE)
{
perror("load data!" ;
exit(0);
}
ok=pm.convertFromImage(image,0);
imagelabel->setPixmap(pm);
repaint();
framenum++;
}
image.pro
######################################################################
# Automatically generated by qmake (2.01a) ??? ?? 6 15:29:04 2009
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += mywindow.h
SOURCES += main.cpp mywindow.cpp
#The following line was inserted by qt3to4
QT += qt3support
[ 本帖最后由 xiaoyan124 于 2009-5-6 21:46 编辑 ] |
|