Move a Rectangle in QGraphicsView



  • 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

    Sure, you can move any items.

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

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



  • @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


  • Moderators



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



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


  • Moderators

    @mandruk1331 You're welcome :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.