Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to lay QObject over an other
Forum Updated to NodeBB v4.3 + New Features

How to lay QObject over an other

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k 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.
  • QT-static-prgmQ Offline
    QT-static-prgmQ Offline
    QT-static-prgm
    wrote on last edited by
    #1

    Hi,

    i have an custom QOpenGLWidget and need to write text on it. Since text in Ogl is not that easy, i thought it may be better to lay and Label over the widget, make the background of that label transparent and write to that label instead of an fancy ogl solution.

    So my Question, is there a way to put and Object over and other??

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @QT-static-prgm said in How to lay QObject over an other:

      So my Question, is there a way to put and Object over and other??

      I think you mean QWidget, because QObjects are not graphical entities.
      Yes, it's doable in couple ways. The simplest one - make one widget a child of the other. In this case make the QLabel a child of the QOpenGLWidget. You can put it in a layout if you want to or place it manually using move(), resize() or setGeometry().

      Another way is to put the "above" widget in the same parent as the "below" widget, but not place it in a layout (position manually). You can then call "raise()" on it to make sure it's in front of the "below" widget.

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        Since QOpenGLWidget is qwidget you can and you can place any widget on it using setgeometry. Qobject u can't place as it is not visual element. U can place any class derived from qwidget

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        3
        • QT-static-prgmQ Offline
          QT-static-prgmQ Offline
          QT-static-prgm
          wrote on last edited by
          #4

          @Chris-Kawa i tried to place the label in the designer and used a grid layout, didn't worked. Next try, no layout and use raise, didn't worked.

          Any ideas??

          void MainWindow::setupWidgets()
          {
          	OglViewerWidget* viewer = new OglViewerWidget(this);
          	setCentralWidget(viewer);
          
          	QAction *openFile = new QAction(QIcon(":/images/toolbar/open.png"), "Open file", this);
          	connect(openFile, &QAction::triggered, this, &MainWindow::openFile);
          	ui->mainToolBar->addAction(openFile);
          
          	QAction *screenShot = new QAction(QIcon(":/images/toolbar/screenshot.png"), "Screenshot", this);
          	connect(screenShot, &QAction::triggered, this, &MainWindow::takeScreenShot);
          	ui->mainToolBar->addAction(screenShot);
          
          	ui->mainToolBar->addSeparator();
          
          	QSignalMapper* signalMapper = new QSignalMapper(this);
          
          	QAction *x = new QAction(QIcon(":/images/toolbar/X.png"), "X", this);
          	x->setCheckable(true);
          	x->setChecked(true);
          	ui->mainToolBar->addAction(x);
          
          	QAction *y = new QAction(QIcon(":/images/toolbar/Y.png"), "Y", this);
          	y->setCheckable(true);
          	y->setChecked(true);
          	ui->mainToolBar->addAction(y);
          
          	QAction *z = new QAction(QIcon(":/images/toolbar/Z.png"), "Z", this);
          	z->setCheckable(true);
          	z->setChecked(true);
          	ui->mainToolBar->addAction(z);
          
          	connect(x, SIGNAL(triggered()), signalMapper, SLOT(map()));
          	connect(y, SIGNAL(triggered()), signalMapper, SLOT(map()));
          	connect(z, SIGNAL(triggered()), signalMapper, SLOT(map()));
          
          	signalMapper->setMapping(x, 1);
          	signalMapper->setMapping(y, 2);
          	signalMapper->setMapping(z, 3);
          
          	connect(signalMapper, SIGNAL(mapped(int)), viewer, SLOT(changeDirection(int)));
          
          	ui->mainToolBar->addSeparator();
          
          	QAction *fileInfo = new QAction(QIcon(":/images/toolbar/info.png"), "File info", this);
          	connect(fileInfo, &QAction::triggered, this, &MainWindow::aboutFile);
          	ui->mainToolBar->addAction(fileInfo);
          
          	QAction *help = new QAction(QIcon(":/images/toolbar/about.png"), "Help", this);
          	connect(help, &QAction::triggered, this, &MainWindow::aboutTool);
          	ui->mainToolBar->addAction(help);
          
          	ui->output->raise();
          }
          

          the interesting part are the first two and last line.

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You mixed up the two solutions I gave. If you put a label in a grid layout in the designer it's already bad.
            The layout was suppose to be set on the OpenGL widget, not on the central widget in the designer, because setCentralWidget creates a layout.

            Here's how to do it right from the code:

            void MainWindow::setupWidgets()
            {
                OglViewerWidget* viewer = new OglViewerWidget(this);
                viewer->setLayout(new QVBoxLayout());
                setCentralWidget(viewer);
            
                QLabel* label = new QLabel("It works!");
                viewer->layout()->addWidget(label)
            }
            

            The other solution would look like this:

            void MainWindow::setupWidgets()
            {
                OglViewerWidget* viewer = new OglViewerWidget(this);
                setCentralWidget(viewer);
            
                QLabel* label = new QLabel("It works!", this); //we're not using layout so we need to pass parent manually
                label->move(10,10); //wherever you want it
                label->raise();
            }
            
            1 Reply Last reply
            2

            • Login

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