How to add label or button in QOpenGL widget?
-
Hello, I use QT5.6 to display 3D frame.
I study this example, it run well.
But now I want add text or button in this widget (shown as followed pic) and don't know how.
I tried to add it in Window or main widget, but it fail.Window::Window(QWindow *screen) : QWindow(screen) { setSurfaceType(QSurface::OpenGLSurface); resize(1024, 768); QSurfaceFormat format; if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { format.setVersion(4, 3); format.setProfile(QSurfaceFormat::CoreProfile); } format.setDepthBufferSize( 24 ); format.setSamples( 4 ); format.setStencilBufferSize(8); setFormat(format); createDisplayInfo(); create(); } void Window::createDisplayInfo() { QRect geo = this->geometry(); const QString styleSheet = "background-color: rgb(0, 0, 0); color: rgb(255, 255, 255);"; QLabel *lable = new QLabel(); lable->setText("hello world:"); lable->setGeometry(10, geo.bottom() - 10 - 130, 50, 15); lable->setStyleSheet(styleSheet); }
it fail, try another one...
int main(int argc, char **argv) { QApplication app(argc, argv); Window *view = new Window(); // initWidget QWidget *container = QWidget::createWindowContainer(view); QSize screenSize = view->screen()->size(); container->setMinimumSize(QSize(200, 100)); container->setMaximumSize(screenSize); QWidget *widget = new QWidget; QHBoxLayout *hLayout = new QHBoxLayout(widget); QVBoxLayout *vLayout = new QVBoxLayout(); vLayout->setAlignment(Qt::AlignTop); hLayout->addWidget(container, 1); hLayout->addLayout(vLayout); widget->setWindowTitle(QStringLiteral("Basic shapes")); QRect geo = widget->geometry(); const QString styleSheet = "background-color: rgb(0, 0, 0); color: rgb(255, 255, 255);"; QLabel *lable = new QLabel(); lable->setText("hello world:"); lable->setGeometry(10, geo.bottom() - 10 - 130, 50, 15); lable->setStyleSheet(styleSheet);
fail again...
Any one can help me, please!
-
Hi,
You don't call show on the widget you create in your Window class.
-
@SGaist said in How to add label or button in QOpenGL widget?:
call show on the widget
Thanks for your reply.
Yes, off course call show, it's done at last code.int main(int argc, char **argv) { QApplication app(argc, argv); Window *view = new Window(); // initWidget QWidget *container = QWidget::createWindowContainer(view); QSize screenSize = view->screen()->size(); container->setMinimumSize(QSize(200, 100)); //container->setMaximumSize(screenSize); container->setMaximumSize(view->geometry().size()); QWidget *widget = new QWidget; QHBoxLayout *hLayout = new QHBoxLayout(widget); QVBoxLayout *vLayout = new QVBoxLayout(); vLayout->setAlignment(Qt::AlignTop); hLayout->addWidget(container, 1); hLayout->addLayout(vLayout); widget->setWindowTitle(QStringLiteral("Basic shapes")); QRect geo = widget->geometry(); const QString styleSheet = "background-color: rgb(0, 0, 0); color: rgb(255, 255, 255);"; QLabel *lable = new QLabel(); lable->setText("hello world:"); lable->setGeometry(10, geo.bottom() - 10, 50, 15); lable->setStyleSheet(styleSheet); // initEngine Qt3DCore::QAspectEngine engine; engine.registerAspect(new Qt3DRender::QRenderAspect()); Qt3DInput::QInputAspect *input = new Qt3DInput::QInputAspect; engine.registerAspect(input); QVariantMap data; data.insert(QStringLiteral("surface"), QVariant::fromValue(static_cast<QSurface *>(view))); data.insert(QStringLiteral("eventSource"), QVariant::fromValue(view)); engine.setData(data); // Root entity Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();//创建顶层实体rootEntity // Camera Qt3DCore::QCamera *cameraEntity = new Qt3DCore::QCamera(rootEntity);//在新创建的rootEntity里面添加摄像机 cameraEntity->setObjectName(QStringLiteral("cameraEntity")); cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); cameraEntity->setPosition(QVector3D(0, 0, -20.0f));//设定摄像机初始位置 cameraEntity->setUpVector(QVector3D(0, 1, 0)); cameraEntity->setViewCenter(QVector3D(0, 0, 0));//设定一进入的中心点 input->setCamera(cameraEntity);//使摄像机能左右转动 // input->setUserData(); // input->setProperty() // FrameGraph Qt3DRender::QFrameGraph *frameGraph = new Qt3DRender::QFrameGraph(); Qt3DRender::QForwardRenderer *forwardRenderer = new Qt3DRender::QForwardRenderer(); forwardRenderer->setCamera(cameraEntity);//架设摄像机 forwardRenderer->setClearColor(QColor(QRgb(0x4d4d4f)));//设置前端背景颜色 frameGraph->setActiveFrameGraph(forwardRenderer);//激活frameGraph // Setting the FrameGraph rootEntity->addComponent(frameGraph); #if 1 // Scenemodifier SceneModifier *modifier = new SceneModifier(rootEntity); modifier->addLabel(1, 10, 10, 10); // Set root object of the scene engine.setRootEntity(rootEntity); // Create control widgets // QCommandLinkButton *info = new QCommandLinkButton(); // info->setText(QStringLiteral("Qt3D ready-made meshes")); // info->setDescription(QStringLiteral("Qt3D provides several ready-made meshes, like torus, cylinder, cube and sphere.")); // info->setIconSize(QSize(0,0)); QCheckBox *torusCB = new QCheckBox(widget); torusCB->setChecked(true); torusCB->setText(QStringLiteral("Torus")); QCheckBox *cylinderCB = new QCheckBox(widget); cylinderCB->setChecked(true); cylinderCB->setText(QStringLiteral("Cylinder")); QCheckBox *cuboidCB = new QCheckBox(widget); cuboidCB->setChecked(true); cuboidCB->setText(QStringLiteral("Cuboid")); QCheckBox *sphereCB = new QCheckBox(widget); sphereCB->setChecked(true); sphereCB->setText(QStringLiteral("Sphere")); // vLayout->addWidget(info); vLayout->addWidget(torusCB); vLayout->addWidget(cylinderCB); vLayout->addWidget(cuboidCB); vLayout->addWidget(sphereCB); QObject::connect(torusCB, &QCheckBox::stateChanged, modifier, &SceneModifier::enableTorus); QObject::connect(cylinderCB, &QCheckBox::stateChanged, modifier, &SceneModifier::enableCylinder); QObject::connect(cuboidCB, &QCheckBox::stateChanged, modifier, &SceneModifier::enableCuboid); QObject::connect(sphereCB, &QCheckBox::stateChanged, modifier, &SceneModifier::enableSphere); torusCB->setChecked(true); cylinderCB->setChecked(true); cuboidCB->setChecked(true); sphereCB->setChecked(true); #endif // Show window widget->show(); widget->resize(1200, 800); // Update the aspect ratio QSize widgetSize = container->size(); float aspectRatio = float(widgetSize.width()) / float(widgetSize.height()); cameraEntity->lens()->setPerspectiveProjection(45.0f, aspectRatio, 0.1f, 1000.0f); return app.exec(); }
-
I am talking about:
@Bat_man said in How to add label or button in QOpenGL widget?:createDisplayInfo
-
@Bat_man said in How to add label or button in QOpenGL widget?:
createDisplayInfo
In that method you are creating a new label, however, you don't call show on it.