PDA

View Full Version : create a Class inherits from two QObject subclasses



sabeesh
28th December 2007, 12:13
Hi,

How can I create a Class inherits from two QObject subclasses in Qt 4.3 ?

Eg:

class MyClass:: public Class1 , protected QThread
{
.......
}

Please help me.

marcel
28th December 2007, 12:19
It is not possible to inherit from 2 QObject classes. Instead inherit only the first one and make your class to be a wrapper around QThread - it doesn't have that many methods anyway. Create a QThread member and expose its API through your class.

EDIT: look at this... eclipse even tells you you can't inherit from two QObject's:

sabeesh
28th December 2007, 12:28
Hi,
I am not very clear with your answer. Can you please give an sample code for it ?

sabeesh
28th December 2007, 12:32
Hi,
How can I solve this problem?
Please help me

marcel
28th December 2007, 12:34
class MyClass : public Class1
{
Q_OBJECT

protected:
MyThread *mThread;

public:
MyClass(QObject* = NULL);
~MyClass();

//expose QThread API
void start() {
mThread->start();
}
.. you can add other QThread methods and slots here
}


Here MyThread is a QThread subclass.

sabeesh
31st December 2007, 07:30
Hi,
When I try this, at the time of running the program, Qt display an error like this


First-chance exception at 0x67015e8a (QtCored4.dll) in Test.exe: 0xC0000005: Access violation reading location 0xcdcdcdd1.
Unhandled exception at 0x67015e8a (QtCored4.dll) in Test.exe: 0xC0000005: Access violation reading location 0xcdcdcdd1.

Please help me to solve this problem.

jpn
31st December 2007, 08:18
You're dereferencing an uninitialized pointer. Do you have something like

mThread = new MyThread(this);
anywhere in your code?

sabeesh
31st December 2007, 08:58
Hi
My code is like this




{
protected:
QThread *mThread;
........

public:
void start() {
mThread->start();
}
virtual void run();

}



and in my .cpp file the code is like this




void CTest::StartCapture (int DeviceNum ){

capture = cvCaptureFromCAM( DeviceNum );

if( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
}
mThread->start();

}


void CWebCamViewer::run() {

while( true ) {
// Get one frame
qApp->processEvents();
ImgFrame = cvQueryFrame( capture );
if( !ImgFrame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
ReturnImg = IplImageToQImage(ImgFrame);
hflip = ReturnImg->transformed(QMatrix().scale(1,-1));
hflip = hflip.scaled(320,240, Qt::IgnoreAspectRatio, Qt::FastTransformation);
Picture->setPixmap(QPixmap::fromImage(hflip));
}
}





Please help me to solve that problum.

jpn
31st December 2007, 09:01
Could you answer my question?

sabeesh
31st December 2007, 09:04
Hi,
No, I didn't give a line like this


mThread = new MyThread(this);

in my program

jpn
31st December 2007, 09:07
So most likely mThread is an uninitialized pointer which points to 0xcdcdcdd1. Don't you think you should have

mThread = new QThread(this);
somewhere?

sabeesh
31st December 2007, 09:30
Hi
When I give the command in my constructor


mThread = new QThread(this);


then Qt return an error like this


.\Test.cpp(70) : error C2259: 'QThread' : cannot instantiate abstract class
due to following members:
'void QThread::run(void)' : is abstract
c:\qt\4.3.0\include\qtcore\qthread.h(87) : see declaration of 'QThread::run'

jpn
31st December 2007, 09:41
Ahh, indeed. QThread will be non-abstract in Qt 4.4 but until that you will have to implement QThread::run().

sabeesh
31st December 2007, 09:59
Hi,
In my code I had implement the run(). Please see the code , which one i previous declare.

in .h file


virtual void run();


and in .cpp file



void CTest::run() {
.....
.....
}


is it correct?

jpn
31st December 2007, 10:23
A friendly advise would be to learn C++ before trying to use a comprehensive toolkit written in C++. Using Qt sure requires some knowledge of C++.

What you have shown above declares a method "virtual void run()" in a class which has QThread pointer as a member. That's not at all same than implementing QThread::run().

Notice the difference between:


class Foo : public QObject
{
protected:
virtual void run() { ... }

QThread* mThread;
};

and


class MyThread : public QThread
{
protected:
virtual void run() { ... }
};

class Bar : public QObject
{
protected:
MyThread* mThread;
};

sabeesh
31st December 2007, 11:14
Hi,
Its my mistake. Sorry.

I create a class like this


class MyThread : public QThread
{

protected:
virtual void run(){
while(true){
qDebug()<<"Run";
}
}

};


and in .h file



QThread *mThread;



and give the command in my constructor like this



mThread = new QThread(this);



but the error is like this




.\Test.cpp(70) : error C2259: 'QThread' : cannot instantiate abstract class
due to following members:
'void QThread::run(void)' : is abstract
c:\qt\4.3.0\include\qtcore\qthread.h(87) : see declaration of 'QThread::run'




Please help me

marcel
31st December 2007, 11:25
QThread is abstract. You must instantiate your QThread subclass:


mThread = new MyThread(this);

sabeesh
31st December 2007, 12:04
Hi,
Ohhhh, It's my mistake. Sorry.
Thankyou for your help