PDA

View Full Version : Subclassing QGraphicsView



sincnarf
26th August 2007, 08:30
Here is my iadfuqgraphicsview.h. I encounter errors such as

iadfuqgraphicsview.h:13: error: expected unqualified-id at end of input
iadfuqgraphicsview.h:13: error: expected `,' or `;' at end of input

How can I solve this?



#ifndef IADFUQGraphicsView_H
#define IADFUQGraphicsView_H

#include <QtGui>
#include <QObject>

class IADFUQGraphicsView : public QGraphicsView
{
Q_OBJECT

public:
IADFUQGraphicsView(QGraphicsScene* s);
~IADFUQGraphicsView(){} ;

protected:
void contextMenuEvent(QContextMenuEvent *event);
void mousePressEvent( QMouseEvent* e );

private:
QPointF* m_clickedPt;
QGraphicsPixmapItem* m_pixmapItem;
QGraphicsScene* m_scene;

QAction *cutAct;
QAction *copyAct;
QAction *pasteAct;

}

#endif

Also, How can I create an iadfuqgraphicsview.cpp that properly overrides the contextMenuEvent and mousePressEvent methods of QGraphicsView? Can anybody create a code for that?

jpn
26th August 2007, 08:44
I encounter errors such as

iadfuqgraphicsview.h:13: error: expected unqualified-id at end of input
iadfuqgraphicsview.h:13: error: expected `,' or `;' at end of input


You're missing a semi-colon (";") at the end of the class declaration:


class Blaa
{
}; // <---




Also, How can I create an iadfuqgraphicsview.cpp that properly overrides the contextMenuEvent and mousePressEvent methods of QGraphicsView? Can anybody create a code for that?
Take a look at the Elastic Nodes Example (http://doc.trolltech.com/4.3/graphicsview-elasticnodes.html). GraphWidget inherits QGraphicsView and reimplements a few event handlers. You can do it the same way.

sincnarf
26th August 2007, 12:07
Thanks but my problem is I need to use the constructor of the QGraphicsView that accepts QWidget as a parameter... how can I do that? also where is the proper place to place that constructor? in .cpp or .h ?

marcel
26th August 2007, 12:47
The proper way is to declare it in the header and define(implement) it in the cpp.


//in the header
class CustomView:public QGraphicsView
{
public:
CustomView(QGraphicsScene*, QWidget*=NULL);
~CustomView();
};

//in the cpp
CustomView::CustomView(QGraphicsScene *scene, QWidget* parent)
:QGraphicsView(scene, parent)
{
//...
}
Regards

sincnarf
27th August 2007, 01:22
I have compilation problems using your recommended consctructor

This is my iadfuqgraphicsview.cpp file


#include "iadfuqgraphicsview.h"

IADFUQGraphicsView::IADFUQGraphicsView(QGraphicsSc ene *scene, QWidget* parent) :QGraphicsView(scene, parent)
{
}


and this is my iadfuqgrahicsview.h file


#ifndef IADFUQGRAPHICSVIEW_H
#define IADFUQGRAPHICSVIEW_H

#include <QtGui>

class QObject;
class QWidget;
class QGraphicsScene;
class QGraphicsView;

class IADFUQGraphicsView : public QGraphicsView
{
Q_OBJECT

public:
IADFUQGraphicsView(QGraphicsScene*, QWidget*= NULL);
~IADFUQGraphicsView();
};


my errors are as follows:
error: prototype for `IADFUQGraphicsView::IADFUQGraphicsView(QGraphicsS cene*, QWidget*)' does not match any in class `IADFUQGraphicsView'

error: candidates are: IADFUQGraphicsView::IADFUQGraphicsView(const IADFUQGraphicsView&)

sincnarf
27th August 2007, 18:11
o m g. Can't figure this out. Sorry for my simple mistakes in C. I'm more inclined to 'other' programming languages than C and C++.

marcel
27th August 2007, 18:30
Well, first of all, don't include QtGui in the header. Do it in the cpp.
Include only QGraphicsView and keep the forward declarations.

And are those all your sources? Could you post the complete ones?

sincnarf
27th August 2007, 18:48
Using your previous tips I got compilation errors that says
iadfuqgraphicsview.h:14: error: expected `,' or `...' before '*=' token
iadfuqgraphicsview.cpp:4: error: prototype for `IADFUQGraphicsView::IADFUQGraphicsView(QGraphicsS cene*, QWidget*)' does not match any in class `IADFUQGraphicsView'
iadfuqgraphicsview.h:10: error: candidates are: IADFUQGraphicsView::IADFUQGraphicsView(const IADFUQGraphicsView&)
iadfuqgraphicsview.h:14: error: IADFUQGraphicsView::IADFUQGraphicsView(QGraphicsSc ene*, QWidget)

iadfuqgraphicsview.cpp


#include <QtGui>
#include "iadfuqgraphicsview.h"
IADFUQGraphicsView::IADFUQGraphicsView(QGraphicsSc ene *scene, QWidget* parent) :QGraphicsView(scene, parent)
{
}


complete header code


#ifndef IADFUQGRAPHICSVIEW_H
#define IADFUQGRAPHICSVIEW_H

class QObject;
class QWidget;
class QGraphicsScene;
class QGraphicsView;

class IADFUQGraphicsView : public QGraphicsView
{
Q_OBJECT

public:
IADFUQGraphicsView(QGraphicsScene*, QWidget*= NULL);
~IADFUQGraphicsView();
};

#endif

marcel
27th August 2007, 18:51
But I told you to include QGraphicsView in the header.

sincnarf
27th August 2007, 19:08
same error. sorry kind of sleepy here.

I tried to place
#include <QtGui/QGraphicsView>

also tried to place
#include <QGraphicsView>

also tried the forward declaration
class QGraphicsView;

still got the same error as before
iadfuqgraphicsview.h:16: error: expected `,' or `...' before '*=' token
iadfuqgraphicsview.cpp:4: error: prototype for `IADFUQGraphicsView::IADFUQGraphicsView(QGraphicsS cene*, QWidget*)' does not match any in class `IADFUQGraphicsView'
iadfuqgraphicsview.h:12: error: candidates are: IADFUQGraphicsView::IADFUQGraphicsView(const IADFUQGraphicsView&)
iadfuqgraphicsview.h:16: error: IADFUQGraphicsView::IADFUQGraphicsView(QGraphicsSc ene*, QWidget)

marcel
27th August 2007, 19:14
Try this:

the header(customgv.h):


#ifndef CUSTOMGV_H
#define CUSTOMGV_H

#include <QGraphicsView>

class QWidget;
class QGraphicsScene;

class customgv : public QGraphicsView
{
Q_OBJECT

public:
customgv(QGraphicsScene*, QWidget*= NULL);
~customgv();
};
#endif


the cpp(customgv.cpp):


#include <QtGui>
#include "customgv.h"
customgv::customgv(QGraphicsScene *scene, QWidget* parent) :QGraphicsView(scene, parent)
{
}


You might have another class named IADF...

Regards

sincnarf
27th August 2007, 19:36
What the.. this worked. Now trying to override contextMenuEvent and mousePressEvent s