【发布时间】:2016-12-15 03:17:15
【问题描述】:
我想有一个在线监控系统,可以知道形状当前的位置,但是我得到了非常奇怪的项目坐标,而且每次我创建新的并拖动它时,它的尺寸都会增加 1。
初始位置(地图大小为 751 x 751,通过输出到qDebug() 进行检查,场景绑定到黄色空间):
将它拖到左上角。
正如您在开始时看到的那样,它在 (200;200) 上,但在拖动后它在 (-201;-196) 上。删除它并在相同位置创建具有相同属性的新形状后,由于它在地图之外,因此看不到新形状,这表明编辑未显示正确的数据。
这里是更新编辑的代码:
void CallableGraphicsRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsRectItem::mouseReleaseEvent(event);
ptr->updateEdits(this);
}
这是我设法缩减为updateEdits()的内容:
void MainWindow::updateEdits(QAbstractGraphicsShapeItem* item)
{
//stuff not related to scene
auto posReal = item->scenePos();
auto pos = posReal.toPoint();
//create QString from coordinates
QString coordinate;
coordinate.setNum(pos.x());
ui->leftXEdit->setText(coordinate);
coordinate.setNum(pos.y());
ui->upperYEdit->setText(coordinate);
//get width and height for rect, radius for circle
auto boundingRectReal = item->sceneBoundingRect();
auto boundingRect = boundingRectReal.toRect();
ui->widthEdit->setText(QString::number(boundingRect.width()));
//disables height edit for circles, not really relevant
if (!items[currentShapeIndex].isRect)
{
ui->heightEdit->setDisabled(true);
}
else
{
ui->heightEdit->setDisabled(false);
ui->heightEdit->setText(QString::number(boundingRect.height()));
}
}
这是我如何将QGraphicsScene 锚定到黄色区域的左上角:
scene->setSceneRect(0, 0, mapSize.width() - 20, mapSize.height() - 20);
ui->graphicsView->setScene(scene);
如何向编辑报告正确的数据?
【问题讨论】:
标签: c++ qt qgraphicsitem