繁体   English   中英

Qt - 制作一个与 QGraphicsView 重叠的面板

[英]Qt - Making a panel that overlaps a QGraphicsView

我正在尝试制作一个显示一些数据的面板,当我按下按钮时会添加这些数据。 我将通过这些图片来解释它:

这将是应用程序的初始状态,一个带有 QGraphicsView 的窗口这将是应用程序的初始状态

如果我点击“帮助”,它应该在它上面显示一个永远不会失焦的窗口在此处输入图片说明

我考虑使用 QDockWidget,但这只是在它旁边创建了一个面板,这不是我想要的。 如果有人知道如何做到这一点,我将不胜感激,谢谢。

您可以在 QGraphicsView 中设置子小部件并将其视为常规 QWidget:

    QApplication app(argc, argv);
    QGraphicsScene* scene = new QGraphicsScene(0, 0, 1000, 1000);
    QGraphicsView* view = new QGraphicsView(scene);
    view->show();

    QPushButton* button = new QPushButton("Show label");
    QLabel* label = new QLabel("Foobar");
    QVBoxLayout* layout = new QVBoxLayout(view);
    layout->setAlignment(Qt::AlignRight | Qt::AlignTop);
    layout->addWidget(button);
    layout->addWidget(label);
    label->hide();
    QObject::connect(button, &QPushButton::clicked, label, &QLabel::show);
    return app.exec();

当您单击按钮时,该标签将在 QGraphicsView 中可见。

您还可以使用QGraphicsProxyWidget类在场景中嵌入一个小部件:

    QApplication app(argc, argv);
    QGraphicsScene* scene = new QGraphicsScene(0, 0, 1000, 1000);
    scene->addItem(new QGraphicsRectItem(500, 500, 50, 50));
    QGraphicsView* view = new QGraphicsView(scene);
    view->show();

    QWidget* w = new QWidget();
    QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget();


    QPushButton* button = new QPushButton("Show label");
    QLabel* label = new QLabel("Foobar");
    QVBoxLayout* layout = new QVBoxLayout(w);
    layout->addWidget(button);
    layout->addWidget(label);
    layout->setAlignment(Qt::AlignRight | Qt::AlignTop);
    label->hide();
    QObject::connect(button, &QPushButton::clicked, label, &QLabel::show);

    proxy->setWidget(w);
    scene->addItem(proxy);
    return app.exec();

暂无
暂无

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

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