Results 1 to 9 of 9

Thread: subclassing QGraphicsView on ui

  1. #1
    Join Date
    Feb 2016
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default subclassing QGraphicsView on ui

    Hi,

    According to the Qt 5.5 documentation, "You can also provide your own custom scene interaction, by creating a subclass of QGraphicsView, and reimplementing the mouse and key event handlers." I am using Qt Creator to create a QGraphicsView object by dragging the "Graphics View" icon to the graphical representation of my ui (centralWidget). I would like to write some handlers to interact with mouse events on this QGraphicsView object, but I am unable to subclass an object. Do I use the "Signals and Slots Editor"? Or is there a better way? Thanks.

    ced

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: subclassing QGraphicsView on ui

    No. Right click the graphics view (you can click in the built UI or in the list of objects) and select promote to Fill in the name of the derived object and the name of the header file for the derived object. Promote, close, done.

  3. The following user says thank you to Radek for this useful post:

    ced (4th February 2016)

  4. #3
    Join Date
    Feb 2016
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: subclassing QGraphicsView on ui

    I did this...promoted the QGraphicsView object in my form to "MyGraphicsView" and complled. Got the following error "... mygraphicsview.h: No such file or directory". Do I add a new class called mygraphicsview? Should the header file start like this?

    class MyGraphicsView : public QGraphicView
    {....
    }

  5. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: subclassing QGraphicsView on ui

    You need a header file for your MyGraphicsView class. The name of the header file is mygraphicview.h by default but it can be changed in the promote dialog. The header will be included in your UI header file made from your .ui file. You need not include it yourself. In fact, it suffice if you creade an empty MyGraphicsView header

    file mygraphicsview.h
    Qt Code:
    1. class MyGraphicsView : public QGraphicsView
    2. {
    3. };
    To copy to clipboard, switch view to plain text mode 
    It should compile and run until you add some MyGraphicsView specialities not offered by QGraphicsView.

  6. #5
    Join Date
    Feb 2016
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: subclassing QGraphicsView on ui

    I could only get it to compile after adding a class "MyGraphicsView" with the following header file:

    Qt Code:
    1. #ifndef MYGRAPHICSVIEW_H
    2. #define MYGRAPHICSVIEW_H
    3.  
    4. #include<QGraphicsView>
    5.  
    6. class MyGraphicsView : public QGraphicsView
    7. {
    8.  
    9. public:
    10. MyGraphicsView(QWidget*);
    11. };
    12.  
    13. #endif // MYGRAPHICSVIEW_H
    To copy to clipboard, switch view to plain text mode 

    and .cpp file:

    Qt Code:
    1. #include "mygraphicsview.h"
    2.  
    3. MyGraphicsView::MyGraphicsView(QWidget *parent) : QGraphicsView(parent)
    4. {
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ced; 7th February 2016 at 01:58.

  7. #6
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: subclassing QGraphicsView on ui

    Maybe. It depends on the ctor searched by the complier when compiling the source file made from your UI. I recommend overloading the QGraphicsView ctor precisely:

    file mygraphicsview.h (add default value)
    Qt Code:
    1. class MyGraphicsView : public QGraphicsView
    2. {
    3.  
    4. public:
    5.  
    6. MyGraphicsView( QWidget *parent = 0 );
    7. };
    To copy to clipboard, switch view to plain text mode 

    file mygraphicsview-ctor1.cpp (like your cpp)
    Qt Code:
    1. #include <mygraphicsview.h>
    2.  
    3. MyGraphicsView::MyGraphicsView( QWidget *parent ) : QGraphicsView(parent)
    4. {
    5. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: subclassing QGraphicsView on ui

    You are forgetting the Q_OBJECT macro in the class declaration.

    Qt Code:
    1. class MyGraphicsView : public QGraphicsView
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. MyGraphicsView( QWidget *parent = 0 );
    8. };
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: subclassing QGraphicsView on ui

    Correct. My apologies.

  10. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: subclassing QGraphicsView on ui

    It's something none of us have ever done before...

Similar Threads

  1. Subclassing QGraphicsView crash the app
    By guidupas in forum Qt Programming
    Replies: 2
    Last Post: 22nd May 2014, 23:24
  2. Replies: 7
    Last Post: 1st December 2011, 16:24
  3. Subclassing QGraphicsView
    By sincnarf in forum Qt Programming
    Replies: 11
    Last Post: 27th August 2007, 19:36
  4. subclassing headerview 2
    By TheM in forum Qt Programming
    Replies: 1
    Last Post: 26th February 2007, 18:54
  5. Subclassing
    By joseph in forum Newbie
    Replies: 1
    Last Post: 25th February 2006, 14:06

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.