PDA

View Full Version : QShared



sabeesh
10th October 2007, 08:57
Hi,
I have a code in QT-3, and it is like this,

================================================== ================
class CVideoFrame
{
friend class CVideoDevice;
private:
struct VideoDataShared: public QShared
{
const QImage *RGB;
const QImage *Y, *U, *V;

unsigned long sequence;
unsigned long time_stamp;
} *data;

}

CVideoFrame::CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v)
{
data = new VideoDataShared;
if (data == 0)
return;

data->RGB = rgb;
data->Y = y;
data->U = u;
data->V = v;
data->sequence = 0;
data->time_stamp = 0;
m_Number = number;
}



CVideoFrame::CVideoFrame(const CVideoFrame &f)
{
data = f.data;
if (data != 0)
{
data->ref();
}
}
================================================== ===============
and it is working.

I need to write this cod in QT-4.3 and I try it like this,

================================================== ================

struct Shared
{
Shared() : count(1) {}
void ref() { ++count; }
bool deref() { return !--count; }
uint count;
};


class CVideoFrame
{

friend class CVideoDevice;
private:
struct VideoDataShared: public Shared
{
const QImage *RGB;
const QImage *Y, *U, *V;

unsigned long sequence;
unsigned long time_stamp;
} *data;

}
CVideoFrame::CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v)
{
data = new VideoDataShared;
if (data == 0)
return;

data->RGB = rgb;
data->Y = y;
data->U = u;
data->V = v;
data->sequence = 0;
data->time_stamp = 0;
m_Number = number;
}


CVideoFrame::CVideoFrame(const CVideoFrame &f)
{
data = f.data;
if (data != 0)
{
data->ref();
}
}

================================================== =============
and I can make this program withut error. But my program is not working properly. I think, this section have some error.

CVideoFrame::CVideoFrame(const CVideoFrame &f)
{
data = f.data;
if (data != 0)
{
data->ref();
//I think this line is not working.....
}
}

please help me to solve this problem.

wysota
10th October 2007, 09:47
I don't think you should implement "Shared" yourself. You probably want to use QSharedData.

sabeesh
10th October 2007, 10:35
hi,

please see this document
http://doc.trolltech.com/4.3/porting4.html#qshared

wysota
10th October 2007, 10:40
"If possible, we recommend that you use QSharedData and QSharedDataPointer instead." :)

sabeesh
10th October 2007, 11:42
Hi,

I modify my program like this,


#include <QSharedData>

class CVideoFrame
{
friend class CVideoDevice;
private:
struct VideoDataShared: public QSharedData
{
const QImage *RGB;
const QImage *Y, *U, *V;
unsigned long sequence;
unsigned long time_stamp;
} *data;
}


#include <QtGui>
#include "VideoFrame.h"

CVideoFrame::CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v)
{

data = new VideoDataShared;
if (data == 0)
return;

data->RGB = rgb;
data->Y = y;
data->U = u;
data->V = v;
data->sequence = 0;
data->time_stamp = 0;
m_Number = number;
}


CVideoFrame::CVideoFrame(const CVideoFrame &f)
{
data = f.data;
if (data != 0)
{
data->ref;
}
}


uint CVideoFrame::GetRefCount() const
{
if (data != 0)
return 0;
return data->count;
}

But when i try to make the program, it display an error message like this,

In member function ‘uint CVideoFrame::GetRefCount() const’:
error: ‘struct CVideoFrame::VideoDataShared’ has no member named ‘count’

how can I solve this?
Please help me

wysota
10th October 2007, 11:52
Your class doesn't have a "count" member, so you can't call it. And you don't need it with implicit sharing. You can't just "translate" your application from using Qt3 classes into using Qt4 classes as Qt3 and Qt4 use different concepts when it comes to data sharing. You have to redesign your class.

sabeesh
11th October 2007, 06:44
Hi,
I am not expert in QT. I am trying to learn. I get a program from net and it is QT-3. I try to create in QT-4.3. Please help me to solve my problem. This code is from Qt-3 and I need to recreate it in QT-4.3. Please help me

code in QT-3
================================================== =====

#include <qimage.h>
#include <qshared.h>
class CVideoFrame
{
friend class CVideoDevice;
private:
struct VideoDataShared: public QShared
{
const QImage *RGB;
const QImage *Y, *U, *V;
unsigned long sequence;
unsigned long time_stamp;
} *data;
uint m_Number;
private:
uint GetRefCount() const;
protected:
CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v);
public:
CVideoFrame(const CVideoFrame &);
virtual ~CVideoFrame();
CVideoFrame &operator =(const CVideoFrame &);
const QImage *GetRGB() const;
const QImage *GetY() const;
const QImage *GetU() const;
const QImage *GetV() const;
uint GetNumber() const ;
void SetSequence(unsigned long seq);
unsigned long GetSequence() const;
void SetTimeStamp(unsigned long stamp);
unsigned long GetTimeStamp() const;
};
--------------------------------------------------------------------------------------------------------------------


#include "VideoFrame.h"

CVideoFrame::CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v)
{

qDebug("CREATE DATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");


data = new VideoDataShared;
if (data == 0)
return;

data->RGB = rgb;
data->Y = y;
data->U = u;
data->V = v;
data->sequence = 0;
data->time_stamp = 0;
m_Number = number;
qDebug(" m_Number is %d and number is %d >>>>>>>>>>>>>>>>>>>>",m_Number, number);

}

CVideoFrame::CVideoFrame(const CVideoFrame &f)
{

qDebug("SECOND CONSTRUCTORRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRRRRRRRRRR>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

data = f.data;

qDebug("time_stamp is %ld", data->time_stamp);
qDebug("sequence is %ld", data->sequence);

if (data != 0)
{
data->ref();
}

}


CVideoFrame::~CVideoFrame()
{
/* if (data && data->deref()) {
delete data;
data = 0;
}
*/

}

// private

uint CVideoFrame::GetRefCount() const
{
// if (data != 0)
// return 0;

return 1;
//// return data->count;
}

// public

CVideoFrame &CVideoFrame::operator =(const CVideoFrame &f)
{
//// if (f.data == 0)
//// return *this;
/* f.data->ref();
if (data && data->deref()) {
delete data;
data = 0;
}
data = f.data;
return *this;
*/

}

const QImage *CVideoFrame::GetRGB() const
{
/// qDebug("qDebuggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggg %d",data->RGB);
return data->RGB;
}

const QImage *CVideoFrame::GetY() const
{
/////////// return data->Y;
}

const QImage *CVideoFrame::GetU() const
{
////////////// return data->U;
}

const QImage *CVideoFrame::GetV() const
{
//////////////// return data->V;
}


uint CVideoFrame::GetNumber() const
{
/// qDebug("<<m_Number is ..................%d<<",m_Number);
return m_Number;
}

void CVideoFrame::SetSequence(unsigned long seq)
{
//// data->sequence = seq;
}


/**
\brief Get sequence number

VideoFrames are sequentially numbered as they are fetches from videodevice
or file. The sequence number is a monotonous increasing number.

Note: when fetching frames from a video device, sequence numbers are not
contiguous in case frames are dropped by the capture process.

*/
unsigned long CVideoFrame::GetSequence() const
{
////////////// return data->sequence;
}

void CVideoFrame::SetTimeStamp(unsigned long stamp)
{
//////////////// data->time_stamp = stamp;
}

/**
\brief Get time stamp
\return Time in milliseconds

VideoFrames are time stamped, to make it easier to order/reference them. The
timestamp is measured in milliseconds.

Note: The first video frame of a video sequence is not guaranteed to have a
timestamp of 0! The only guarantee you have is that the timestamp is a monotonous
increasing number.
*/

unsigned long CVideoFrame::GetTimeStamp() const
{
///// return data->time_stamp;
}

================================================== ======

and I try to create it in QT-4 like this,

code in QT-4

================================================== =============

#include <QSharedData>
class CVideoFrame
{
friend class CVideoDevice;
private:
struct VideoDataShared: public QSharedData
{
const QImage *RGB;
const QImage *Y, *U, *V;

unsigned long sequence;
unsigned long time_stamp;
} *data;
uint m_Number ;
private:
uint GetRefCount() const;
protected:
CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v);
public:
uint m_Number ;
CVideoFrame(const CVideoFrame &);
virtual ~CVideoFrame();
CVideoFrame &operator =(const CVideoFrame &);
const QImage *GetRGB() const;
const QImage *GetY() const;
const QImage *GetU() const;
const QImage *GetV() const;
int GetNumber() const ;
void SetSequence(unsigned long seq);
unsigned long GetSequence() const;
void SetTimeStamp(unsigned long stamp);
unsigned long GetTimeStamp() const;
};
------------------------------------------------


#include <QtGui>
#include "VideoFrame.h"

CVideoFrame::CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v)
{
data = new VideoDataShared;
if (data == 0)
return;

data->RGB = rgb;
data->Y = y;
data->U = u;
data->V = v;
data->sequence = 0;
data->time_stamp = 0;
m_Number = number;

}

CVideoFrame::CVideoFrame(const CVideoFrame &f)
{

data = f.data;
if (data != 0)
{
data->ref;
}

}CVideoFrame::~CVideoFrame()
{
/*if ( data && data->deref())
{
delete data;
data = 0;
}
*/


}

// private

uint CVideoFrame::GetRefCount() const
{
// if (data != 0)
// return 0;

return 1;
///////return data->count;
}

// public
CVideoFrame &CVideoFrame::operator =(const CVideoFrame &f)
{

// if (f.data == 0)
// return *this;
// f.data->ref;

///f.data->ref();
/* if (data && data->deref()) {
delete data;
data = 0;
}
*/

// data = f.data;
// return *this;
}

const QImage *CVideoFrame::GetRGB() const
{
return data->RGB;
}

const QImage *CVideoFrame::GetY() const
{
/////////////////////////// return data->Y;
}

const QImage *CVideoFrame::GetU() const
{
////////////////////////////// return data->U;
}

const QImage *CVideoFrame::GetV() const
{
///////////////////////// return data->V;
}


int CVideoFrame::GetNumber() const
{
return m_Number;
}

void CVideoFrame::SetSequence(unsigned long seq)
{
/////////////////////// data->sequence = seq;
}

unsigned long CVideoFrame::GetSequence() const
{
/////////////////////// return data->sequence;
}
void CVideoFrame::SetTimeStamp(unsigned long stamp)
{
//////////////////////// data->time_stamp = stamp;
}

unsigned long CVideoFrame::GetTimeStamp() const
{
///////////////// return data->time_stamp;
}

================================================== =================


and in another function I call the function " frame->GetRGB() ". I think the error is this line.



void CImagePanelRGB::UpdateImage()
{
CVideoFrame *frame = 0;

frame = m_pVideo->GetLatestVideoFrame();
if (frame == 0){
QImage *ImgRGB = new QImage();
}
else {
ImgRGB = frame->GetRGB()->copy(); //////////////check this line
}
delete frame;
update();

}

-----------------------------------------------------------
Please help me to solve this problem

wysota
11th October 2007, 09:18
But what exactly is the problem? If you don't tell us what is wrong with the code (does it compile? does it crash? does it do something you don't want?), we won't be able to help you.

And please use the [code] tags to embed code in your posts.

sabeesh
11th October 2007, 10:30
Hi,
I can make and run the program in QT-4. But I didn't get any output from this code in QT-4. But the old program working in QT-3. When I comment the line

frame->GetRGB()


in QT-3 it also doesn't display the image in screen. So i think that, that line is error. :(

wysota
11th October 2007, 10:37
It seems that ImgRGB is a QImage pointer and QImage::copy() returns the object, not a pointer to it. First of all I suggest you change pointers to QImage to QImage objects as in Qt4 there is no point in having bare QImage pointers. Then make sure you assign proper types (pointers to pointers and objects to objects). Then we can continue.

sabeesh
11th October 2007, 12:09
Hi,
please check this code it is in QT-3


QVector<QImage> m_RGB;
for (u = 0; u < m_Buffers; u++) {
m_RGB.insert(u, new QImage(vid_io_buffer + vid_io_offsets[u], w, h, 32,0,0, QImage::IgnoreEndian));
}
it is working in QT-3. How can I create this in QT-4. I do it like this,


QVector<QImage *> m_RGB;

for (u = 0; u < m_Buffers; u++) {
m_RGB.insert(u, *(new QImage(vid_io_buffer + vid_io_offsets[u], w, h, 32, QImage::Format_RGB32 )));

it doesn't deisplay any error at the time of make, but it doesn't display any image at run time. I think this is the most important part or that program, because when i comment this line in qt-3, then that program crashed at the time of run.
Please help me. please find the error in my code.
:(

}

wysota
11th October 2007, 12:40
Could you please start using the proper tags to embed you code?

Why do you keep using pointers in Qt4 code? Is there a reason for that? I told you there is no point in using pointers to QImage in Qt4, because it's an implicitely shared class! The code you have given won't even compile.