Results 1 to 18 of 18

Thread: create a Class inherits from two QObject subclasses

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

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
  •  
Qt is a trademark of The Qt Company.