[SOLVED] Getting value from custom QGraphicsItem
-
Hi everyone,
I try to make my own custom QGraphicsItem. After that I wanna to define values in the Items so I can edit them later. The problem is that i make my QGraphicsItem with a pointer (i have no others ideas) and i can not take it back the values after that.
Here my code
mysquare.h
@#ifndef MYSQUARE_H
#define MYSQUARE_H#include <QPainter>
#include <QGraphicsItem>
#include <QDebug>
#include <QWidget>
#include <QtGui>
#include <QtCore>
#include <QMessageBox>class MySquare : public QGraphicsItem
{
public:
MySquare();QRectF boundingRect() const; int x_pos; int x_value; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};#endif // MYSQUARE_H
@and mysquare.cpp
@#include "mysquare.h"
#include <QInputDialog>MySquare::MySquare()
{}
QRectF MySquare::boundingRect() const
{return QRectF (x_pos,0,5,5);
}
void MySquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec=boundingRect();
QPen pen(Qt::black);
QBrush brush(Qt::black);
pen.setWidth(1);
painter->setPen(pen);
painter->setBrush(brush);
painter->drawRect(rec);
QString pattern = QString("Coordinate X = %1").arg(x_pos);
QGraphicsItem::setToolTip(pattern);
}void MySquare::mousePressEvent(QGraphicsSceneMouseEvent *event)
{QGraphicsItem::mousePressEvent(event); update(); QInputDialog InpInt; InpInt.setInputMode(QInputDialog::IntInput); InpInt.setIntMinimum(0); InpInt.setIntMaximum(522); InpInt.setIntValue(5); InpInt.setLabelText("Test input"); InpInt.exec(); x_value = InpInt.intValue();
}
@
they are used in dialog further: first dialog.h
@#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
#include <QtCore>
#include <QtGui>
#include <QGraphicsScene>
#include "mysquare.h"
#include <armadillo>
using namespace arma;
namespace Ui {
class Dialog;
}class Dialog : public QDialog
{
Q_OBJECTpublic:
explicit Dialog(QWidget *parent = 0);
~Dialog();private:
Ui::Dialog *ui;
QGraphicsScene *scene;
vec test;
};#endif // DIALOG_H
@and dialog.cpp
@#include "dialog.h"
#include "ui_dialog.h"
#include <QGraphicsItem>
#include <QInputDialog>Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
scene =new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
ui->graphicsView->setRenderHint(QPainter::Antialiasing);int Nr_r=10; test.resize(Nr_r); for( int i = 0; i < Nr_r; i++ ) { MySquare *square3=new MySquare(); square3->x_pos=5+i*25; scene->addItem(square3); test(i)=square3->x_value; } update(); test.save("test.dat",csv_ascii);
}
Dialog::~Dialog()
{
delete ui;
}
@I dialog.cpp i make in a loop couple of the custom Items, and then i want to save the values in a array (vec is a armadilo 1D array) and then save the array (armadilo function). I get only "0".... Have anybody an idea?
Thanks -
Hi and welcome to devnet,
x_value is only modified in mousePressEvent which is never called. You're lucky it returns 0.
-
Hi,
with the function mousePressEvent I input the values for x_value via QInputDialog. That means that i define the x_value later. -
I have the solution yeee :).
I defined an variable for the id for all custom items and i can call them with a separat dialog. prima.
If someone have the same problem, can write me i will give him the code.