I checked a couple of resources on qt's website and here but I could not solve my problem.
I'm trying to draw a rectangle on QGraphicsScene on mouse click and I want the new rectangle to be centered exactly where the user clicked but this does not work until the scene is big enough.
Here's what I tried
In MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
=
ui->graphView->setContextMenuPolicy(Qt::CustomContextMenu);
scene = new QGraphicsScene();
ui->graphView->setScene(scene);
...
}
void MainWindow::on_graphView_customContextMenuRequested(const QPoint &pos)
{
auto pp= ui->graphView->mapToScene(pos);
tableOfRectangles.push_back( new component(pp,s,n,t)); //component is my class that inherits from qgraphicsitem
scene->addItem(tableOfRectangles[tableOfRectangles.size()-1]);
}
and in compenent.cpp
component::component(QPointF pos,unsigned int id, QString cname, QString ctype )
{
this->center = pos;
this->id = id;
this->name = cname;
this->type = ctype;
setFlag(ItemIsMovable);
}
QRectF component::boundingRect() const
{
return QRectF(center.x(),center.y(),80,80);
}
My problem is:
The very first rectangles are drawn in the middle, and keep shifting slightly the more I add toward the correct position. Byadding more rectangles (or dragging the existing ones) making the scene big enough (when scroll bars start to appear), the new rectangles are added correctly at mouse position. but how do I do force them to be inserted at the correct position since the beginning?