Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Getting value from custom QGraphicsItem
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Getting value from custom QGraphicsItem

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nikitto
    wrote on last edited by
    #1

    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&#40;&#41;;
    x_value = InpInt.intValue(&#41;;
    

    }

    @

    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_OBJECT

    public:
    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      x_value is only modified in mousePressEvent which is never called. You're lucky it returns 0.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nikitto
        wrote on last edited by
        #3

        Hi,
        with the function mousePressEvent I input the values for x_value via QInputDialog. That means that i define the x_value later.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          Nikitto
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved