首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取QWidget在QGraphicsItem中的位置

获取QWidget在QGraphicsItem中的位置
EN

Stack Overflow用户
提问于 2022-04-23 03:01:26
回答 1查看 157关注 0票数 2

我有一个QWidget和一个QPushButton,同时,这个QWidget被嵌入到一个QGraphicsItem中,这个QGraphicsItem在一个QGraphicsScene中。

我需要在两个指向QGraphicsItems的QPushButton之间画一条线。为此,我需要得到QPushButton的职位。看起来是这样的:

我尝试获取QPushButton在QGraphicsItem构造函数中的位置,但它返回0,0。我想这是QWidget内按钮的位置。我想我需要的是一种能在屏幕上找到位置的方法。

最小的例子:尽可能简化。QWidget:

代码语言:javascript
运行
复制
NodeFrame::NodeFrame()
{
  setFixedSize(200,80);
  setStyleSheet("QFrame { background-color: #2e4076; }");
  
  // Creates and add a QPushButton to the frame.
  // I need the position of this button on the QGraohicsScene
  auto button = new QPushButton("B");
  button->setFixedSize(40,20);
  
  auto layout = new QHBoxLayout();
  layout->addWidget(button);
  setLayout(layout);
}

QGraphicsItem:

代码语言:javascript
运行
复制
class Node : public QGraphicsItem
{
public:
  Node();
  QRectF boundingRect() const override;
  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
};

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

  // Create a GraphicsProxyWidget to insert the nodeFrame into the scene
  auto proxyWidget = new QGraphicsProxyWidget(this);
  auto 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:

代码语言:javascript
运行
复制
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);
  
  // Show the the view
  view->show();
  return app.exec();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-23 16:44:51

nodeframe.cpp中,我添加了一个函数getButtonRect()

代码语言:javascript
运行
复制
#ifndef NODEFRAME_H
#define NODEFRAME_H

#include <QWidget>
#include <QPushButton>
#include <QRect>


class NodeFrame: public QWidget
{
public:
    NodeFrame();

    QRect  getButtonRect();

private:
    QPushButton *button;
    QHBoxLayout *layout;

};

#endif // NODEFRAME_H

nodeframe.cpp

代码语言:javascript
运行
复制
#include "nodeframe.h"

NodeFrame::NodeFrame()
{
    setFixedSize(200, 80);
    setStyleSheet("QFrame { background-color: #2e4076; }");

    // 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);
    setLayout(layout);
}

QRect  NodeFrame::getButtonRect()
{
    return layout->itemAt(0)->geometry();
}

在Node中,将这个函数传递给main.cpp,因为QGraphicsView在那里:

node.cpp:

代码语言:javascript
运行
复制
#include "node.h"

#include <QGraphicsProxyWidget>
#include <QPainter>

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);
}

QRect  Node::getButtonRect()
{
    return frame->getButtonRect();
}

main.cpp

代码语言:javascript
运行
复制
#include "node.h"

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>

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(0, 0);

// qDebug() << "RECT bottomLeft= " << view->mapToScene(item->getButtonRect().bottomLeft());
// qDebug() << "RECT bottomRight= " << view->mapToScene(item->getButtonRect().bottomRight());
// qDebug() << "RECT topLeft= " << view->mapToScene(item->getButtonRect().topLeft());
// qDebug() << "RECT topRight= " << view->mapToScene(item->getButtonRect().topRight());

    auto  btnRect = item->getButtonRect();
    auto  ellipse = new QGraphicsEllipseItem(QRect(view->mapToGlobal(btnRect.center()).x(), view->mapToGlobal(btnRect.center()).y(), 40, 40));
    qDebug() << "Center" << view->mapToGlobal(btnRect.center());
    scene->addItem(ellipse);

    // Show the the view
    view->show();

    return app.exec();
}

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71976490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档