PDA

View Full Version : subclassing QGraphicsView on ui



ced
4th February 2016, 03:38
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

Radek
4th February 2016, 07:50
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.

ced
5th February 2016, 04:21
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
{....
}

Radek
5th February 2016, 06:59
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


class MyGraphicsView : public QGraphicsView
{
};

It should compile and run until you add some MyGraphicsView specialities not offered by QGraphicsView.

ced
6th February 2016, 22:44
I could only get it to compile after adding a class "MyGraphicsView" with the following header file:



#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#include<QGraphicsView>

class MyGraphicsView : public QGraphicsView
{

public:
MyGraphicsView(QWidget*);
};

#endif // MYGRAPHICSVIEW_H

and .cpp file:


#include "mygraphicsview.h"

MyGraphicsView::MyGraphicsView(QWidget *parent) : QGraphicsView(parent)
{

}

Radek
7th February 2016, 05:50
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)


class MyGraphicsView : public QGraphicsView
{

public:

MyGraphicsView( QWidget *parent = 0 );
};


file mygraphicsview-ctor1.cpp (like your cpp)


#include <mygraphicsview.h>

MyGraphicsView::MyGraphicsView( QWidget *parent ) : QGraphicsView(parent)
{
}

d_stranz
7th February 2016, 17:24
You are forgetting the Q_OBJECT macro in the class declaration.



class MyGraphicsView : public QGraphicsView
{
Q_OBJECT

public:

MyGraphicsView( QWidget *parent = 0 );
};

Radek
7th February 2016, 18:24
Correct. My apologies. :)

d_stranz
7th February 2016, 18:55
It's something none of us have ever done before... ;)