- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个在线监控系统,可以判断形状当前的位置,但是我得到了非常奇怪的项目坐标,而且每次我创建新的并拖动它时它的尺寸都会增加 1。
初始位置( map 大小为 751 x 751,通过输出到 qDebug()
进行检查,场景绑定(bind)到黄色空间):
将它拖到左上角。
如您所见,一开始它是 (200;200),但拖动后它是 (-201;-196)。删除它并在相同位置创建具有相同属性的新形状后,新形状无法看到,因为它在 map 之外,这表明编辑未显示正确的数据。
这是更新编辑的代码:
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);
如何向编辑报告正确的数据?
最佳答案
您最好覆盖 itemChange 方法并使用 ItemPositionHasChanged 通知。您必须在项目上设置 ItemSendsGeometryChanges 标志,以便它接收这些通知。
当您仍在 mouseReleaseEvent 方法中时,我不确定您的项目的最终位置是否已设置。在 itemChange 中跟踪它会确保数据有效,而这种东西就是它的作用。
另外,请注意“pos”在项目的父坐标中,“boundingRect”在项目的坐标空间中。如果你想确保你使用的是场景坐标,你应该使用“scenePos”和“sceneBoundingRect”。如果项目没有父项,则“pos”和“scenePos”将返回相同的值,但“boundingRect”和“sceneBoundingRect”通常会不同。
关于c++ - 拖动释放后如何适本地获得 QGraphicsRectItem 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41130694/
我是一名优秀的程序员,十分优秀!