Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLabel inside another QLabel (code provided)



  • I have a label which I have create using the UI creatot(dragged the label on mainwindow) and now I want to create a label inside the label, and inside the label which has been created with code I want to display a message to indicate that everything is ok. How I can do this? I have overloaded the paintEvent so I could draw the borders of the labels(to see them).

    void MainWindow::paintEvent(QPaintEvent *event){
    
     if(divide == 2){
     P1.begin(this);
     P1.setPen(Qt::PenStyle::DashLine);
     P1.drawRect(ui->mainlabel->x(),ui->mainlabel>y()*2,ui->mainlabel->width()/2,ui->mainlabel->height()/2);
     P1.end();
    
     S2_0 = new QLabel(ui->mainlabel);
     S2_0->setFrameStyle(QFrame::Panel | QFrame::Sunken);
     S2_0->setGeometry(ui->mainlabel->x(),ui->mainlabel>y()*2,ui->mainlabel->width()/2,ui->mainlabel->height()/2);
     S2_0->setText("hello""); S2_0->setAlignment(Qt::AlignCenter);
    
    }
    

    When divide is changed i call repaint();



  • could you show us an image with what you want as final result?

    i suspect what you are really after is a widget with layout with 2 labels in it



  • @VRonin yes that's what I want. I have found that I have to create a layout and then store the label inside the layout, but at the moment I don't have a clue how to do it.




  • Moderators

    Here's a quick example:

    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(new Label("my first label");
    layout->addWidget(new Label("my second label");
    
    setLayout(layout);
    

    That would make a vertical box with label 1 on top of label 2. You can control sizing and spacing with the layout.


Log in to reply