简体   繁体   English

Map QWidget 中心 position 到 QGraphicsScene 坐标?

[英]Map QWidget center position to QGraphicsScene coordinates?

I have a QGraphicsItem with an embedded QWidget, this QWidget have a QPushButton in it.我有一个带有嵌入式 QWidget 的 QGraphicsItem,这个 QWidget 中有一个 QPushButton。 I'm trying to map the center of the QPushButton to the QGraphicsScene coordinates, so for example, I can add a Circle to the center of the QPushButton.我正在尝试将 QPushButton 的中心 map 添加到 QGraphicsScene 坐标,例如,我可以将一个圆添加到 QPushButton 的中心。

With the help from another post I was able to find the center of the QPushButton, but it doesn't correspond to its actual position in the QGraphicsScene.在另一篇文章的帮助下,我找到了 QPushButton 的中心,但它与 QGraphicsScene 中的实际 position 不对应。

在此处输入图像描述

What I tried:我尝试了什么:

  1. Getting the QRect of the button and then its center, finally mapping to the global coordinates of the view.获取按钮的 QRect,然后是它的中心,最后映射到视图的全局坐标。

  2. Getting the QRect of the button, then its center and mapping it to the QGraphicsItem.获取按钮的 QRect,然后获取其中心并将其映射到 QGraphicsItem。

  3. Getting the QRect of the button, then its center, mapping it to the QGraphicsItem and than mapping it to the scene.获取按钮的 QRect,然后是它的中心,将其映射到 QGraphicsItem,然后将其映射到场景。

In general, I tried mapping it to Scene, to Global and to Item but it always looks incorrect.一般来说,我尝试将它映射到场景、全局和项目,但它看起来总是不正确。 And the farther I move the QGraphicsItem, the less accurate it gets.而且我将 QGraphicsItem 移动得越远,它就越不准确。 Here the circle is supposed to be positioned at the center of the "B" button:这里的圆应该位于“B”按钮的中心: 在此处输入图像描述

Qwidget:微件:

class NodeFrame : public QFrame
{
public:
  NodeFrame();
  QRect getButtonRect()
  {
    return layout->itemAt(0)->geometry();
  }
  
private:
  QPushButton* button = nullptr;
  QHBoxLayout* layout = nullptr;
};

NodeFrame::NodeFrame()
{
  setFixedSize(200,80);
  
  // Creates and add a QPushButton to the frame.
  // I need the position of this button on the QGraohicsScene
  button = new QPushButton("B");
  button->setFixedSize(40,20);
  
  layout = new QHBoxLayout();
  layout->addWidget(button);
  layout->setAlignment(Qt::AlignCenter);
  setLayout(layout);
}

QGraphicsItem: QGraphicsItem:

class Node : public QGraphicsItem
{
public:
  Node();
  QRectF boundingRect() const override;
  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
  
  QRect getButtonRect()
  {
    return frame->getButtonRect();
  }
  
  NodeFrame* frame = nullptr;
};

Node::Node()
{
  setFlag(ItemIsMovable);

  // Create a GraphicsProxyWidget to insert the nodeFrame into the scene
  auto proxyWidget = new QGraphicsProxyWidget(this);
  frame = new NodeFrame();
  proxyWidget->setWidget(frame);
  // Center the widget(frame) at the center of the QGraphicsItem
  proxyWidget->setPos(boundingRect().center() - proxyWidget->boundingRect().center());
}

QRectF Node::boundingRect() const
{
  return QRectF(-10, -10, 280, 150);
}

void Node::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
  QPainterPath path;
  path.addRoundedRect(boundingRect(), 10, 10);
  painter->drawPath(path);
}

main:主要的:

int main(int argc, char* argv[])
{
  QApplication app(argc, argv);
  
  // Create scene and view
  auto scene = new QGraphicsScene();
  auto view = new QGraphicsView(scene);
  
  view->setMinimumSize(800, 800);
  
  // Create the QGraphicsItem and add it to the scene
  auto item = new Node();
  scene->addItem(item);
  item->setPos(50, 50);

  
  auto btnRect = item->getButtonRect();
  auto center = view->mapToGlobal(btnRect.center());
  auto circle = new QGraphicsEllipseItem();
  circle->setRect(QRectF(center.x(), center.y(), 25, 25));
  scene->addItem(circle);


  // Show the the view
  view->show();
  return app.exec();
}

Appreciate any help.感谢任何帮助。

Solved.解决了。 This was caused by two things:这是由两件事造成的:

1: QRectF getButtonRect() was returning layout->itemAt(0)->geometry() , (index 0 being the first and only widget in the layout) but button->frameGeometry() seems to be a more accurate visual representation of the button's geometry. 1: QRectF getButtonRect()返回layout->itemAt(0)->geometry() ,(索引 0 是布局中的第一个也是唯一的小部件)但是button->frameGeometry()似乎是更准确的视觉表示按钮的几何形状。

2: When adding the widget to the graphic item using QGraphicsProxyWidget , I was adjusting the position of the widget inside the graphic item using: 2:当使用QGraphicsProxyWidget将小部件添加到图形项时,我正在使用以下方法调整图形项内小部件的 position:

proxyWidget->setPos(boundingRect().center() - proxyWidget->boundingRect().center());

This was changing the position (obviously) of the widget inside the graphic item, so visually it didn't align with the result given by button->frameGeometry() .这改变了图形项内小部件的 position(显然),因此在视觉上它与button->frameGeometry()给出的结果不一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM