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

QLabel Addition to layout



  • Hello everybody,

    I have created a table where if I add an item it creates a row in table and a new qlabel such as:

    void MainWindow::on_pushButton_clicked()
    {
        model->insertRows(0,1);
    
    
        QSpacerItem *item = new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Fixed);
        ui->horizontalLayout_2->addSpacerItem(item);
        for(int i =0;i<1;i++){
            QLabel *label1 = new QLabel("NOTE");
            ui->horizontalLayout_2->insertWidget(i,label1);
    
        }
    
    }
    

    But I want to know 2 things:

    1. how can I have access to second qlabel added, I mean, what is its name? label1, label2....to get acces to any of them

    2. When I add too many qlabels they supperpose each other, how do I readapt its size?

    Thanks!


  • Lifetime Qt Champion

    Hi
    A plain layout cannot scroll. so if labels have no minimum size set. then at some point, they will be compressed.
    You can use a QScrollArea for a widget that can scroll the Labels.

    However, why do you not use something like QListWidget that is a scroll list of Texts, ready to be edited if you want.



  • @mrjj said in QLabel Addition to layout:

    QScrollArea

    Thanks, I tried but do not know how to add new qlabels by pressing button
    ui->scrollArea->setWidgetResizable(true);
    ui->scrollArea->setWidget(label1);


  • Lifetime Qt Champion

    @jss193
    Hi
    you apply layout to ScollArea and add the Labels to the layout.



  • @mrjj how do I apply it, I have never seen it before...

    Thanks


  • Lifetime Qt Champion

    Hi,

    Did you saw the documentation of QScrollArea and its example ?


Log in to reply