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_OBJECTpublic:
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");}
-
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
-
Hi, I guess http://doc.qt.io/qt-5/qgraphicsview.html#alignment-prop is what you want.
-
@Wieland I'll give it a try. Thank you.
-
@Wieland OMG!!! It worked. Thank you, again!
-
@mandruk1331 You're welcome :-)
-
@mandruk1331
If your issue has been solved, mark this thread as Solved. -
@KillerSmath
He has not been here for 6 months :)
But otherwise a good idea.