mvbhavsar
18th August 2015, 17:36
I have following code.
#include <QWidget>
struct ItemProperties{
int xpos1;
int ypos1;
};
namespace Ui {
class Properties;
}
class Properties : public QWidget
{
Q_OBJECT
public:
explicit Properties(QWidget *parent = 0);
~Properties();
void setProperty(ItemProperties &m_prop);
signals:
void propertyChanged(ItemProperties);
private slots:
void on_sbXpos1_valueChanged(int arg1);
private:
Ui::Properties *ui;
ItemProperties iprops;
};
#endif // PROPERTIES_H
#include "properties.h"
#include "ui_properties.h"
Properties::Properties(QWidget *parent) :
QWidget(parent),
ui(new Ui::Properties)
{
ui->setupUi(this);
}
Properties::~Properties()
{
delete ui;
}
void Properties::setProperty(ItemProperties &m_prop)
{
ui->sbXpos1->setValue(iprops.xpos1);
ui->sbYPos1->setValue(iprops.ypos1);
}
void Properties::on_sbXpos1_valueChanged(int arg1)
{
iprops.xpos1 = arg1;
emit propertyChanged(iprops);
}
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <QtWidgets>
#include "properties.h"
class Rectangle: public QObject, public QGraphicsRectItem
{
Q_OBJECT
public:
Rectangle(QGraphicsItem *parent=0);
Properties *properties();
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant & value);
private slots:
void propertyChangedSlot(ItemProperties iprops);
private:
QRectF newRect;
Handles *hands;
Properties *props;
ItemProperties ip;
void updateProperties();
};
#include "rectangle.h"
Rectangle::Rectangle(QGraphicsItem *parent):QObject(),
QGraphicsRectItem(parent)
{
setFlags(ItemIsMovable|ItemIsSelectable|ItemSendsG eometryChanges);
props = new Properties;
connect(props,SIGNAL(propertyChanged(ItemPropertie s)),SLOT(propertyChangedSlot(ItemProperties)));
ip.xpos1 = 100;
ip.ypos1 = 100;
props->setProperty(ip);
newRect.setRect(0,0,200,100);
setRect(newRect);
setPos(100,100);
}
Properties *Rectangle::properties()
{
return props;
}
QVariant Rectangle::itemChange(QGraphicsItem::GraphicsItemC hange change, const QVariant &value)
{
QPointF newpos = value.toPoint();
if (change==ItemPositionHasChanged && scene()){
ip.xpos1 = newpos.x();
ip.ypos1 = newpos.y();
props->setProperty(ip);
}
return QGraphicsItem::itemChange(change, value);
}
void Rectangle::propertyChangedSlot(ItemProperties iprops)
{
updateProperties();
}
void Rectangle::updateProperties()
{
newRect.setRect(ip.xpos1,ip.ypos1,ip.width,ip.heig ht);
setRect(newRect);
}
I want to create rectangle and also it provides properties. What I want if I dragged and moved rectangle, ItemProperties spinbox should get updated and if spinbox's value is changed rectangle's position should get updated.
When I execute above code, the movement is not proper.
Does anybody explain why?
Thanks
Manish
#include <QWidget>
struct ItemProperties{
int xpos1;
int ypos1;
};
namespace Ui {
class Properties;
}
class Properties : public QWidget
{
Q_OBJECT
public:
explicit Properties(QWidget *parent = 0);
~Properties();
void setProperty(ItemProperties &m_prop);
signals:
void propertyChanged(ItemProperties);
private slots:
void on_sbXpos1_valueChanged(int arg1);
private:
Ui::Properties *ui;
ItemProperties iprops;
};
#endif // PROPERTIES_H
#include "properties.h"
#include "ui_properties.h"
Properties::Properties(QWidget *parent) :
QWidget(parent),
ui(new Ui::Properties)
{
ui->setupUi(this);
}
Properties::~Properties()
{
delete ui;
}
void Properties::setProperty(ItemProperties &m_prop)
{
ui->sbXpos1->setValue(iprops.xpos1);
ui->sbYPos1->setValue(iprops.ypos1);
}
void Properties::on_sbXpos1_valueChanged(int arg1)
{
iprops.xpos1 = arg1;
emit propertyChanged(iprops);
}
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <QtWidgets>
#include "properties.h"
class Rectangle: public QObject, public QGraphicsRectItem
{
Q_OBJECT
public:
Rectangle(QGraphicsItem *parent=0);
Properties *properties();
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant & value);
private slots:
void propertyChangedSlot(ItemProperties iprops);
private:
QRectF newRect;
Handles *hands;
Properties *props;
ItemProperties ip;
void updateProperties();
};
#include "rectangle.h"
Rectangle::Rectangle(QGraphicsItem *parent):QObject(),
QGraphicsRectItem(parent)
{
setFlags(ItemIsMovable|ItemIsSelectable|ItemSendsG eometryChanges);
props = new Properties;
connect(props,SIGNAL(propertyChanged(ItemPropertie s)),SLOT(propertyChangedSlot(ItemProperties)));
ip.xpos1 = 100;
ip.ypos1 = 100;
props->setProperty(ip);
newRect.setRect(0,0,200,100);
setRect(newRect);
setPos(100,100);
}
Properties *Rectangle::properties()
{
return props;
}
QVariant Rectangle::itemChange(QGraphicsItem::GraphicsItemC hange change, const QVariant &value)
{
QPointF newpos = value.toPoint();
if (change==ItemPositionHasChanged && scene()){
ip.xpos1 = newpos.x();
ip.ypos1 = newpos.y();
props->setProperty(ip);
}
return QGraphicsItem::itemChange(change, value);
}
void Rectangle::propertyChangedSlot(ItemProperties iprops)
{
updateProperties();
}
void Rectangle::updateProperties()
{
newRect.setRect(ip.xpos1,ip.ypos1,ip.width,ip.heig ht);
setRect(newRect);
}
I want to create rectangle and also it provides properties. What I want if I dragged and moved rectangle, ItemProperties spinbox should get updated and if spinbox's value is changed rectangle's position should get updated.
When I execute above code, the movement is not proper.
Does anybody explain why?
Thanks
Manish