1)How can I make QGraphicsScene to start from top-left not from center? I tried to do this mathematically, but failed:
QPoint temp
( view
->pos
() );
// Coords pos in the window( qDialog ) QPoint correct
( view
->rect
().
width() / 2, view
->rect
().
height() / 2 );
if( lineButClicked )
{
items.
push_back( scene
->addLine
( QLine( // items - QVector of QGraphicItem geom[index][0] - temp - correct, // geom - a matrix of QPoint
geom[index][1] - temp - correct ), pen ) );
index++; // just an int don't worry about it
pointsNum = 0; // another int
}
QPoint temp( view->pos() ); // Coords pos in the window( qDialog )
QPoint correct( view->rect().width() / 2, view->rect().height() / 2 );
if( lineButClicked )
{
items.push_back( scene->addLine( QLine( // items - QVector of QGraphicItem
geom[index][0] - temp - correct, // geom - a matrix of QPoint
geom[index][1] - temp - correct ), pen ) );
items[index]->setFlag( QGraphicsItem::ItemIsMovable );
index++; // just an int don't worry about it
pointsNum = 0; // another int
}
To copy to clipboard, switch view to plain text mode
2) I tried to separate different graphics items( f.e. lines, rects and ellipses ) in separate classes, but again failed. First I get an error while creating an instance of mygraphicsitem in qdialog class, because it says it's abstract. How can I manage to go through it? Second, I get a bunch of errors from qt libs somelike this: 'staticMetaObject' is not a member of QGraphicsItem, error: 'QScopedPointer<QObjectData> QObject::d_ptr' is protected QScopedPointer<QObjectData> d_ptr,
etc.
Here's my code for line:
lineitem.h
#ifndef LINEITEM_H
#define LINEITEM_H
#include <QGraphicsItem>
#include <QPaintEvent>
#include <QPainter>
#include <QPen>
{
public:
LineItem();
private:
private:
};
#endif // LINEITEM_H
#ifndef LINEITEM_H
#define LINEITEM_H
#include <QGraphicsItem>
#include <QPaintEvent>
#include <QPainter>
#include <QPen>
class LineItem : public QGraphicsItem
{
public:
LineItem();
QRectF boundingRect() const;
void SetRect( QPoint p1, QPoint p2 );
void paint( QPainter* painter, const QStyleOptionGraphicsItem *option, QWidget* widget );
private:
private:
QPoint point1;
QPoint point2;
QPen pen;
};
#endif // LINEITEM_H
To copy to clipboard, switch view to plain text mode
lineitem.cpp
#include "lineitem.h"
{
}
QRectF LineItem
::boundingRect() const {
return QRectF( 0,
0,
10,
10 );
}
{
point1 = p1;
point2 = p2;
}
{
painter
->drawRect
( QRect( point1, point2
) );
}
#include "lineitem.h"
LineItem::LineItem() : QGraphicsItem()
{
}
QRectF LineItem::boundingRect() const
{
return QRectF( 0, 0, 10, 10 );
}
void LineItem::SetRect(QPoint p1, QPoint p2)
{
point1 = p1;
point2 = p2;
}
void LineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget *widget)
{
painter->drawRect( QRect( point1, point2 ) );
}
To copy to clipboard, switch view to plain text mode
You can find hole project without QGraphicsItem classes, here https://github.com/Vyivrain/ProjectFiles if needed.
Bookmarks