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. Move a Rectangle in QGraphicsView
Forum Update on Monday, May 27th 2025

Move a Rectangle in QGraphicsView

Scheduled Pinned Locked Moved Unsolved General and Desktop
rectanglesmove problem
10 Posts 6 Posters 5.8k Views
  • 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.
  • M Offline
    M Offline
    mandruk1331
    wrote on 21 Jan 2016, 01:05 last edited by
    #1

    I have a problem, I can't move the position of the rectangle no matter how hard I tried. Can someone help me,plzzz?? here the code.
    mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QErrorMessage>
    #include <QtCore>
    #include <QtGui>
    #include <QGraphicsScene>
    #include <QDialog>
    #include <QPaintEvent>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_4_clicked();

    void on_pushButton_8_clicked();
    

    private:
    Ui::MainWindow *ui;
    QGraphicsScene *scene;
    QGraphicsRectItem *rectangle;
    QGraphicsTextItem *text;
    QVector<int> random_numbers_;
    QVector<int> user_input_numbers_;
    QErrorMessage *error = nullptr;

    };

    #endif // MAINWINDOW_H

    mainwindow.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QMessageBox>
    #include <cstdlib>
    #include<QErrorMessage>
    #include<QPixmap>
    #include <QGraphicsScene>
    #define RANDOM_MAX 100
    #define QUANTITY_RANDOM_NUM 10
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    error = new QErrorMessage(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    QBrush blueBrush(Qt::blue);
    QPen Pen(Qt::black);
    Pen.setWidth(6);
    QPainter painter(this);
    QRectF rect;
    Pen.setWidth(2);
    painter.setPen(Pen);
    rect.bottom();
    ui->graphicsView->setScene(scene);
    for(int i=0;i<QUANTITY_RANDOM_NUM;i++){

    scene->addRect(0,20,40,30,Pen,blueBrush);
    

    }
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_4_clicked()
    {

    random_numbers_.clear();
    for(int i=0;i<QUANTITY_RANDOM_NUM;i++){
    random_numbers_.append(rand()%RANDOM_MAX);
    }
    QString second_line = NULL;
    for(int i=0;i<random_numbers_.length();i++){
    second_line.append(QString::number(random_numbers_.at(i))+"|");
    }

    ui->lineEdit_2->setText(second_line);
    //ui->graphicsView->setScene(scene);

    }

    void MainWindow::on_pushButton_8_clicked()
    {
    if(ui->lineEdit->text()==""){
    error->showMessage("Can not perform RECORD.Empty line.");

    }
    user_input_numbers_.clear();
    QString temp ="";
    QString first_line = ui->lineEdit->text();
    int i=0;

    for(i=0;i<first_line.length();i++){
    if(first_line[i]==' '){ //needs extra space in the end of input.FIX!!!
    user_input_numbers_.push_back(temp.toInt());
    temp="";
    continue;
    }
    else if (first_line[i].isNumber()){
    temp.append(first_line[i]);
    }
    else{

         error->showMessage("Error in inputing array numbers");
      }
    

    }

    for(int i=0;i<user_input_numbers_.length();i++){

    qDebug("%d",user_input_numbers_[i]);
    

    }
    qDebug("NEW");

    }

    Mandruk1331

    D 1 Reply Last reply 21 Jan 2016, 04:07
    0
    • M mandruk1331
      21 Jan 2016, 01:05

      I have a problem, I can't move the position of the rectangle no matter how hard I tried. Can someone help me,plzzz?? here the code.
      mainwindow.h
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H

      #include <QMainWindow>
      #include <QErrorMessage>
      #include <QtCore>
      #include <QtGui>
      #include <QGraphicsScene>
      #include <QDialog>
      #include <QPaintEvent>

      namespace Ui {
      class MainWindow;
      }

      class MainWindow : public QMainWindow
      {
      Q_OBJECT

      public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();

      private slots:
      void on_pushButton_4_clicked();

      void on_pushButton_8_clicked();
      

      private:
      Ui::MainWindow *ui;
      QGraphicsScene *scene;
      QGraphicsRectItem *rectangle;
      QGraphicsTextItem *text;
      QVector<int> random_numbers_;
      QVector<int> user_input_numbers_;
      QErrorMessage *error = nullptr;

      };

      #endif // MAINWINDOW_H

      mainwindow.cpp
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QMessageBox>
      #include <cstdlib>
      #include<QErrorMessage>
      #include<QPixmap>
      #include <QGraphicsScene>
      #define RANDOM_MAX 100
      #define QUANTITY_RANDOM_NUM 10
      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);
      error = new QErrorMessage(this);
      scene = new QGraphicsScene(this);
      ui->graphicsView->setScene(scene);

      QBrush blueBrush(Qt::blue);
      QPen Pen(Qt::black);
      Pen.setWidth(6);
      QPainter painter(this);
      QRectF rect;
      Pen.setWidth(2);
      painter.setPen(Pen);
      rect.bottom();
      ui->graphicsView->setScene(scene);
      for(int i=0;i<QUANTITY_RANDOM_NUM;i++){

      scene->addRect(0,20,40,30,Pen,blueBrush);
      

      }
      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }

      void MainWindow::on_pushButton_4_clicked()
      {

      random_numbers_.clear();
      for(int i=0;i<QUANTITY_RANDOM_NUM;i++){
      random_numbers_.append(rand()%RANDOM_MAX);
      }
      QString second_line = NULL;
      for(int i=0;i<random_numbers_.length();i++){
      second_line.append(QString::number(random_numbers_.at(i))+"|");
      }

      ui->lineEdit_2->setText(second_line);
      //ui->graphicsView->setScene(scene);

      }

      void MainWindow::on_pushButton_8_clicked()
      {
      if(ui->lineEdit->text()==""){
      error->showMessage("Can not perform RECORD.Empty line.");

      }
      user_input_numbers_.clear();
      QString temp ="";
      QString first_line = ui->lineEdit->text();
      int i=0;

      for(i=0;i<first_line.length();i++){
      if(first_line[i]==' '){ //needs extra space in the end of input.FIX!!!
      user_input_numbers_.push_back(temp.toInt());
      temp="";
      continue;
      }
      else if (first_line[i].isNumber()){
      temp.append(first_line[i]);
      }
      else{

           error->showMessage("Error in inputing array numbers");
        }
      

      }

      for(int i=0;i<user_input_numbers_.length();i++){

      qDebug("%d",user_input_numbers_[i]);
      

      }
      qDebug("NEW");

      }

      D Offline
      D Offline
      Devopia53
      wrote on 21 Jan 2016, 04:07 last edited by
      #2

      @mandruk1331

      Sure, you can move any items.

      QGraphicsRectItem *item = scene->addRect(0,20,40,30,Pen,blueBrush);

      item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);

      M 1 Reply Last reply 23 Jan 2016, 16:40
      0
      • D Devopia53
        21 Jan 2016, 04:07

        @mandruk1331

        Sure, you can move any items.

        QGraphicsRectItem *item = scene->addRect(0,20,40,30,Pen,blueBrush);

        item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);

        M Offline
        M Offline
        mandruk1331
        wrote on 23 Jan 2016, 16:40 last edited by
        #3

        @Devopia53 Sorry for the badly formulated question, I was trying to say that I cant change the position in the Graphics View, I tried to move it but It always spawns in the center how can I change that

        Mandruk1331

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on 23 Jan 2016, 17:09 last edited by
          #4

          Hi, I guess http://doc.qt.io/qt-5/qgraphicsview.html#alignment-prop is what you want.

          M 2 Replies Last reply 23 Jan 2016, 17:31
          1
          • ? A Former User
            23 Jan 2016, 17:09

            Hi, I guess http://doc.qt.io/qt-5/qgraphicsview.html#alignment-prop is what you want.

            M Offline
            M Offline
            mandruk1331
            wrote on 23 Jan 2016, 17:31 last edited by
            #5

            @Wieland I'll give it a try. Thank you.

            Mandruk1331

            1 Reply Last reply
            0
            • ? A Former User
              23 Jan 2016, 17:09

              Hi, I guess http://doc.qt.io/qt-5/qgraphicsview.html#alignment-prop is what you want.

              M Offline
              M Offline
              mandruk1331
              wrote on 23 Jan 2016, 18:05 last edited by
              #6

              @Wieland OMG!!! It worked. Thank you, again!

              Mandruk1331

              ? 1 Reply Last reply 23 Jan 2016, 19:40
              0
              • M mandruk1331
                23 Jan 2016, 18:05

                @Wieland OMG!!! It worked. Thank you, again!

                ? Offline
                ? Offline
                A Former User
                wrote on 23 Jan 2016, 19:40 last edited by
                #7

                @mandruk1331 You're welcome :-)

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Debjit
                  wrote on 20 Apr 2019, 21:30 last edited by
                  #8

                  Thanks from me too :)

                  1 Reply Last reply
                  0
                  • KillerSmathK Offline
                    KillerSmathK Offline
                    KillerSmath
                    wrote on 20 Apr 2019, 21:50 last edited by
                    #9

                    @mandruk1331
                    If your issue has been solved, mark this thread as Solved.

                    @Computer Science Student - Brazil
                    Web Developer and Researcher
                    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                    mrjjM 1 Reply Last reply 20 Apr 2019, 22:19
                    0
                    • KillerSmathK KillerSmath
                      20 Apr 2019, 21:50

                      @mandruk1331
                      If your issue has been solved, mark this thread as Solved.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 20 Apr 2019, 22:19 last edited by
                      #10

                      @KillerSmath
                      He has not been here for 6 months :)
                      But otherwise a good idea.

                      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