QGraphicsScene problem
-
hi, I'm new in Qt development.I want to add a scene and while I click to the left mouse button on the scene at several position,the coordinate of that position will be stored at the polygon Item and when click the right click on the scene,the polygon Item will be added at the scene.I've the following code..
main.cpp
@#include <QtGui/QApplication>
#include "Dialog.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();return a.exec();
}
@dialog.h
@#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
namespace Ui {
class Dialog;
}class EERTView;
class Dialog : public QDialog
{
Q_OBJECTpublic:
explicit Dialog(QWidget *parent = 0);
~Dialog();private:
Ui::Dialog ui;
EERTView widget;
};#endif // DIALOG_H
@dialog.cpp
@#include "Dialog.h"
#include "ui_Dialog.h"#include "EERTView.h"
#include <QGraphicsScene>Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
this->setWindowTitle("Mouse Click Polygon");
widget=new EERTView(this);
}Dialog::~Dialog()
{
delete ui;
}
@EERTView.h
@#ifndef EERTVIEW_H
#define EERTVIEW_H
#include<QGraphicsView>
#include<QGraphicsScene>
#include<QGraphicsItem>
#include<QWidget>
class EERTPoly;class EERTView : public QGraphicsView
{
Q_OBJECT
public:
explicit EERTView(QWidget *parent = 0);signals:
public slots:
protected:
virtual void mousePressEvent ( QMouseEvent * event );private:
EERTPoly* polygonItem;};
#endif // EERTVIEW_H
@EERTView.cpp
@#include "EERTView.h"
#include "EERTPoly.h"EERTView::EERTView(QWidget *parent) :
QGraphicsView(parent)
{QGraphicsScene *scene = new QGraphicsScene();
// scene->setItemIndexMethod(QGraphicsScene::NoIndex);
scene->setSceneRect(0, 0, 400, 400);
scene->setBackgroundBrush(Qt::yellow);
setScene(scene);EERTPoly* polygonItem=new EERTPoly(polygonItem);
}
void EERTView::mousePressEvent ( QMouseEvent * event ){
if(event->button() == Qt::LeftButton)
{polygonItem->polygon().append(QPointF(event->pos().x(), event->pos().y()));
// markList.append(new GraphMarkItemCustom(QPointF(event->pos().x(), event->pos().y())));
// ptr = markList.last();
// markGroup->addToGroup(ptr);
//this->scene()->addItem(ptr);
}
if(event->button()==Qt::RightButton)
{scene.addItem(polygonItem); }
}
@EERTPoly.h
@#ifndef EERTPOLY_H
#define EERTPOLY_H#include <QGraphicsPolygonItem>
class EERTPoly : public QGraphicsPolygonItem
{
Q_OBJECT
public:
explicit EERTPoly( QGraphicsItem * parent = 0);
QPolygonF polygon () const;
virtual QRectF boundingRect () const;
virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
virtual void setPolygon(const QPolygonF &polygon);signals:
public slots:
//protected:
// QPolygonF polygonItem;};
#endif // EERTPOLY_H
@EERTPoly.cpp
@#include "EERTPoly.h"
#include <QtGui/QPainter>EERTPoly::EERTPoly( QGraphicsItem * parent = 0) :
QGraphicsPolygonItem(parent)
{
// setFlag(QGraphicsPolygonItem::ItemIsFocusable);
// QGraphicsPolygonItem* polygonItem=new QGraphicsPolygonItem(polygonItem);
}void EERTPoly::setPolygon(const QPolygonF &polygon)
{
polygonItem = polygon;
}QPolygonF EERTPoly::polygon () const
{
return polygonItem;
}QRectF EERTPoly::boundingRect () const
{
return ploygonItem.boundingRect();}
void EERTPoly::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 )
{
painter->drawPolygon(boundingRect());
}
@but it gives the following error
1.invalid use of incomplete type 'struct QMouseEvent'
2.forward declaration of 'struct QMouseEvent'
3.'((EERTView*)this)->QGraphicsView::scene' does not have the class typecould anyone please help me to finish this work.
-
You are missing <code>#include <QtGui/QMouseEvent></code> and it is <code>scene()->addItem(...)</code>, not <code>scene.addItem(...)</code>.
-
Ya I used this but there is also an error
1..’((EERTView*)this)->QGraphicsView::scene’ does not have the class typehow could I solve this.can u help a bit providing some instruction
-
Sorry Sorry it's not the error ,I'v made a mistake.bt the error like following
1.Invalid use of the member (did u forget '&')
2.base operand of '->' is not a pointercan u please help me to get rid of this error
-
QGraphicsView has no member scene, it just has a member scene(), which returns a pointer to the set QGraphicsScene. Therefore you will have to use <code>scene()->addItem(polygonItem)</code> instead of <code>scene.addItem(polygonItem)</code>.
-
Hi,I've made the several change on my programe
at
EERTView.h@#ifndef EERTVIEW_H
#define EERTVIEW_H
#include<QGraphicsView>
#include<QGraphicsScene>
#include<QGraphicsItem>
#include<QWidget>
#include<QtGui>
class EERTPoly;class EERTView : public QGraphicsView
{
Q_OBJECT
public:
explicit EERTView(QWidget *parent = 0);signals:
public slots:
protected:
void mousePressEvent ( QMouseEvent * event );private:
EERTPoly* polygonItem;QPolygonF polygom;
};
#endif // EERTVIEW_H
@
EERTView.cpp@#include "EERTView.h"
#include "EERTPoly.h"
#include<QGraphicsView>EERTView::EERTView(QWidget *parent) :
QGraphicsView(parent)
{QGraphicsScene *scene = new QGraphicsScene();
// scene->setItemIndexMethod(QGraphicsScene::NoIndex);
scene->setSceneRect(0, 0, 400, 400);
scene->setBackgroundBrush(Qt::yellow);
setScene(scene);
// EERTPoly polygonItem=NULL;
// polygonItem=new EERTPoly;
EERTPoly polygonItem=new EERTPoly(polygonItem);}
void EERTView::mousePressEvent ( QMouseEvent * event ){
if(event->button() == Qt::LeftButton) {
// polygonItem->polygon();
polygom.append(QPointF(event->pos().x(),event->pos().y()));
// polygonItem->polygon().append(QPointF(event->pos().x(), event->pos().y()));
}// polygonItem->polygon().append(QPointF(event->pos().x(), event->pos().y()));
//// markList.append(new GraphMarkItemCustom(QPointF(event->pos().x(), event->pos().y())));
//// ptr = markList.last();
//// markGroup->addToGroup(ptr);
// //this->scene()->addItem(ptr);
// }// if (event->button() == Qt::LeftButton)
// {
// polygonItem= event->pos();
//// polygonItem=setpos;
//// QListWidget::mousePressEvent(event);
// }if(event->button()==Qt::RightButton) {
// SuperItem *super=NULL;
// super=new SuperItem;
// scene->addItem(super);
this->scene()->addItem(polygonItem);
polygonItem->setFocus();
// scene.addItem(polygonItem);
}}
@EERTPoly.h
@#ifndef EERTPOLY_H
#define EERTPOLY_H#include <QGraphicsPolygonItem>
class EERTView;class EERTPoly : public QGraphicsPolygonItem
{
Q_OBJECT
public:explicit EERTPoly( QGraphicsItem * parent = 0); QPolygonF polygon () const; virtual QRectF boundingRect () const; virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ); void setPolygon(const QPolygonF &polygon);
signals:
public slots:
protected:
QPolygonF polygom;};
#endif // EERTPOLY_H
@EERTPoly.cpp
@#include "EERTPoly.h"
#include <QtGui/QPainter>EERTPoly::EERTPoly( QGraphicsItem * parent = 0) :
QGraphicsPolygonItem(parent)
{
// setFlag(QGraphicsPolygonItem::ItemIsFocusable);
// QGraphicsPolygonItem* polygonItem=new QGraphicsPolygonItem(polygonItem);
}void EERTPoly::setPolygon(const QPolygonF &polygon)
{
this->setPolygon(polygon);
}QPolygonF EERTPoly::polygon () const
{
return QPolygonF;
}QRectF EERTPoly::boundingRect () const
{
return ploygonItem.boundingRect();}
void EERTPoly::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 )
{
painter->drawPolygon(boundingRect());
}
@but It's Give me the following error
1.Default argument was given parameter 1'EERTPoly::EERTPoly(*QGraphicsItem)'
2.after previous specification in 'EERTPoly::EERTPoly(*QGraphicsItem)'
in the memeber function in 'QPolygonF EERTPoly::polygon() const'3.expected primary expression ';' in the member function 'QPolygonF EERTPoly::polygon () const'
4.'polygonItem' was not declared in this scope
5.default argument given 'EERTPoly::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 )'
6.after previous specification in ' virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );'actuallt I want to draw a polygon on the scene when I clicked on the scene .the polygon point was store in the void EERTView::mousePressEvent ( QMouseEvent * event ) function.can anyone plz help me to do so