How to lay QObject over an other



  • 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??


  • Moderators

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



  • 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



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


  • Moderators

    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();
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.