[英]Qt - Making a panel that overlaps a QGraphicsView
您可以在 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.