【问题标题】:Qt - Making a panel that overlaps a QGraphicsViewQt - 制作一个与 QGraphicsView 重叠的面板
【发布时间】:2019-07-15 01:58:23
【问题描述】:

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

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

如果我点击“帮助”,它应该会在其上方显示一个永不失焦的窗口

我研究过使用 QDockWidget,但这只是在它旁边创建一个面板,这不是我想要的。如果有人知道该怎么做,我将非常感激,谢谢。

【问题讨论】:

    标签: c++ qt qgraphicsview qlayout


    【解决方案1】:

    您可以在 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();
    

    【讨论】:

    • 谢谢,我可以用 qgraphicsview 替换标签吗?
    • 您可以将标签替换为任何 QWidget。但是,在这种情况下,您应该在主视图中处理 QGraphicsObject/item。第二个 QGraphicsView 没用。
    • 好吧,我想像图例类型的东西一样显示,我旁边有图像和标签,我应该使用 qtableview 吗?
    • QTableView 用于显示模型中的数据。您应该为此使用带有布局的小部件。