Results 1 to 18 of 18

Thread: create a Class inherits from two QObject subclasses

  1. #1
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default create a Class inherits from two QObject subclasses

    Hi,

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

    Eg:
    Qt Code:
    1. class MyClass:: public Class1 , protected QThread
    2. {
    3. .......
    4. }
    To copy to clipboard, switch view to plain text mode 

    Please help me.
    Last edited by jpn; 28th December 2007 at 12:45. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    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:
    Attached Images Attached Images
    Last edited by marcel; 28th December 2007 at 12:25.

  3. #3
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    Hi,
    I am not very clear with your answer. Can you please give an sample code for it ?

  4. #4
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    Hi,
    How can I solve this problem?
    Please help me

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    Qt Code:
    1. class MyClass : public Class1
    2. {
    3. Q_OBJECT
    4.  
    5. protected:
    6. MyThread *mThread;
    7.  
    8. public:
    9. MyClass(QObject* = NULL);
    10. ~MyClass();
    11.  
    12. //expose QThread API
    13. void start() {
    14. mThread->start();
    15. }
    16. .. you can add other QThread methods and slots here
    17. }
    To copy to clipboard, switch view to plain text mode 

    Here MyThread is a QThread subclass.

  6. The following user says thank you to marcel for this useful post:

    sabeesh (28th December 2007)

  7. #6
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    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.
    Last edited by jpn; 31st December 2007 at 08:19. Reason: missing [quote] tags

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: create a Class inherits from two QObject subclasses

    You're dereferencing an uninitialized pointer. Do you have something like
    Qt Code:
    1. mThread = new MyThread(this);
    To copy to clipboard, switch view to plain text mode 
    anywhere in your code?
    J-P Nurmi

  9. #8
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    Hi
    My code is like this


    Qt Code:
    1. {
    2. protected:
    3. QThread *mThread;
    4. ........
    5.  
    6. public:
    7. void start() {
    8. mThread->start();
    9. }
    10. virtual void run();
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    and in my .cpp file the code is like this

    Qt Code:
    1. void CTest::StartCapture (int DeviceNum ){
    2.  
    3. capture = cvCaptureFromCAM( DeviceNum );
    4.  
    5. if( !capture ) {
    6. fprintf( stderr, "ERROR: capture is NULL \n" );
    7. getchar();
    8. }
    9. mThread->start();
    10.  
    11. }
    12.  
    13.  
    14. void CWebCamViewer::run() {
    15.  
    16. while( true ) {
    17. // Get one frame
    18. qApp->processEvents();
    19. ImgFrame = cvQueryFrame( capture );
    20. if( !ImgFrame ) {
    21. fprintf( stderr, "ERROR: frame is null...\n" );
    22. getchar();
    23. break;
    24. }
    25. ReturnImg = IplImageToQImage(ImgFrame);
    26. hflip = ReturnImg->transformed(QMatrix().scale(1,-1));
    27. hflip = hflip.scaled(320,240, Qt::IgnoreAspectRatio, Qt::FastTransformation);
    28. Picture->setPixmap(QPixmap::fromImage(hflip));
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 



    Please help me to solve that problum.
    Last edited by jpn; 31st December 2007 at 09:01. Reason: PS. the closing tag is [/code] not [\code]

  10. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: create a Class inherits from two QObject subclasses

    Could you answer my question?
    J-P Nurmi

  11. #10
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    Hi,
    No, I didn't give a line like this
    Qt Code:
    1. mThread = new MyThread(this);
    To copy to clipboard, switch view to plain text mode 
    in my program

  12. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: create a Class inherits from two QObject subclasses

    So most likely mThread is an uninitialized pointer which points to 0xcdcdcdd1. Don't you think you should have
    Qt Code:
    1. mThread = new QThread(this);
    To copy to clipboard, switch view to plain text mode 
    somewhere?
    J-P Nurmi

  13. #12
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    Hi
    When I give the command in my constructor
    Qt Code:
    1. mThread = new QThread(this);
    To copy to clipboard, switch view to plain text mode 

    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'
    Last edited by jpn; 31st December 2007 at 09:43. Reason: missing [code] tags

  14. #13
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: create a Class inherits from two QObject subclasses

    Ahh, indeed. QThread will be non-abstract in Qt 4.4 but until that you will have to implement QThread::run().
    J-P Nurmi

  15. #14
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    Hi,
    In my code I had implement the run(). Please see the code , which one i previous declare.

    in .h file
    Qt Code:
    1. virtual void run();
    To copy to clipboard, switch view to plain text mode 

    and in .cpp file

    Qt Code:
    1. void CTest::run() {
    2. .....
    3. .....
    4. }
    To copy to clipboard, switch view to plain text mode 

    is it correct?

  16. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: create a Class inherits from two QObject subclasses

    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:
    Qt Code:
    1. class Foo : public QObject
    2. {
    3. protected:
    4. virtual void run() { ... }
    5.  
    6. QThread* mThread;
    7. };
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. protected:
    4. virtual void run() { ... }
    5. };
    6.  
    7. class Bar : public QObject
    8. {
    9. protected:
    10. MyThread* mThread;
    11. };
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  17. The following user says thank you to jpn for this useful post:

    sabeesh (1st January 2008)

  18. #16
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    Hi,
    Its my mistake. Sorry.

    I create a class like this
    Qt Code:
    1. class MyThread : public QThread
    2. {
    3.  
    4. protected:
    5. virtual void run(){
    6. while(true){
    7. qDebug()<<"Run";
    8. }
    9. }
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    and in .h file
    Qt Code:
    1. QThread *mThread;
    To copy to clipboard, switch view to plain text mode 

    and give the command in my constructor like this

    Qt Code:
    1. mThread = new QThread(this);
    To copy to clipboard, switch view to plain text mode 


    but the error is like this

    Qt Code:
    1. .\Test.cpp(70) : error C2259: 'QThread' : cannot instantiate abstract class
    2. due to following members:
    3. 'void QThread::run(void)' : is abstract
    4. c:\qt\4.3.0\include\qtcore\qthread.h(87) : see declaration of 'QThread::run'
    To copy to clipboard, switch view to plain text mode 


    Please help me

  19. #17
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    QThread is abstract. You must instantiate your QThread subclass:
    Qt Code:
    1. mThread = new MyThread(this);
    To copy to clipboard, switch view to plain text mode 

  20. The following user says thank you to marcel for this useful post:

    sabeesh (1st January 2008)

  21. #18
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: create a Class inherits from two QObject subclasses

    Hi,
    Ohhhh, It's my mistake. Sorry.
    Thankyou for your help

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.