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

View a List of QLabel



  • Hi! I'm new to Qt and I want to create an application that has a list of hyperlink and RichTex that is displayed in Layout ui.
    I Tried to use setItemWidget of listWidget but it create memoryLeak for every QLabel i have (rightly).

    any advice or help?


  • Lifetime Qt Champion

    Hi
    Then it seems like a false positive as you set a parent so they will be cleaned up but
    if the parent live long then it will first be much later that might fool
    valgrind.



  • @Ggwppino said in View a List of QLabel:

    setItemWidget of listWidget but it create memoryLeak for every QLabel i have

    If you add new items to your QListWidget, you dont have memory leaks. The parent (ListWidget takes care of all child items)



  • thanks for the reply, it reassures me.
    actually valgrind does not notify me of any problems in the memcheck section but When i click any QLabel with hyperlink in my application output, appear a lot of Valgrind notification of Memory leaks writed in xml.



  • @Ggwppino

    What labels? Do you mean QListWidgetItems?



  • i Need to show in Layout a List of hyperlink. QLabel has possibility for containing richtext, so I do:

    ui->listWidget->setWidgetItem(item, myLabel)
    

    In memcheck, valgrind doesn't warning me, but in Application Output there are a lot of Valgrind error.


  • Lifetime Qt Champion

    @Ggwppino
    The docs don't say it takes ownership so try to give the Label a parent. ( as far as i could find)
    I think it does own it but try and see if it stops the valgrind warnings.

    auto label = new QLabel( ui->listWidget);
    ui->listWidget->setItemWidget(ui->listWidget->item(0), label);
    

    Also note that valgrind might not understand Qt system in all cases.
    https://doc.qt.io/qt-5/objecttrees.html



  • @mrjj thanks for the reply,

    I'm already doing it this way

        void mainwindow::printHyperLink(QString in){
            QListWidgetItem *item;
            QLabel * myLabel;
            bool opened = in!="Error";
        
            if (opened)
            {
        
                item = new QListWidgetItem("");
                ui->listWidget1->addItem(item);
                myLabel = new QLabel(in, this);
                myLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
                myLabel->setOpenExternalLinks(true);
                ui->listWidget1->setItemWidget(item, myLabel);
            } else {
                item = new QListWidgetItem("Error");
                ui->listWidget1->addItem(item);
            }
        }
    

    practically this function is called every time one of my threads finishes processing a link.

    Could this create problems with valgrind?
    I specify that the problem persists even if I manually add only one item in ListWidget in mainwindow.


  • Lifetime Qt Champion

    Hi
    Then it seems like a false positive as you set a parent so they will be cleaned up but
    if the parent live long then it will first be much later that might fool
    valgrind.


Log in to reply