Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. How to add label or button in QOpenGL widget?
Forum Updated to NodeBB v4.3 + New Features

How to add label or button in QOpenGL widget?

Scheduled Pinned Locked Moved Unsolved Game Development
6 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bat_man
    wrote on 14 Mar 2019, 09:05 last edited by
    #1

    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.
    button/text
    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!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Mar 2019, 22:04 last edited by
      #2

      Hi,

      You don't call show on the widget you create in your Window class.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply 15 Mar 2019, 01:16
      0
      • S SGaist
        14 Mar 2019, 22:04

        Hi,

        You don't call show on the widget you create in your Window class.

        B Offline
        B Offline
        Bat_man
        wrote on 15 Mar 2019, 01:16 last edited by
        #3

        @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();
        }
        
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Mar 2019, 22:46 last edited by
          #4

          I am talking about:
          @Bat_man said in How to add label or button in QOpenGL widget?:

          createDisplayInfo

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          B 1 Reply Last reply 20 Mar 2019, 07:36
          0
          • S SGaist
            15 Mar 2019, 22:46

            I am talking about:
            @Bat_man said in How to add label or button in QOpenGL widget?:

            createDisplayInfo

            B Offline
            B Offline
            Bat_man
            wrote on 20 Mar 2019, 07:36 last edited by
            #5

            @SGaist said in How to add label or button in QOpenGL widget?:

            createDisplayInfo

            Sorry I don't understand what's your mean, what show...

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 20 Mar 2019, 22:06 last edited by
              #6

              @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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              1/6

              14 Mar 2019, 09:05

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved