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

Show a QStringList in QLabel



  • How can I show in my QLabel my list? Now I see only "test3".

    
    QStringList ex = {"test1", "test2", "test3"};
    for (int i = 0; i<3; i++) {
            ui->label_out->setText(ex[i]); //is a QLabel
    }
      
    


  • @sterbilly
    Yes, that is what your code does, overwrites the text on each iteration. How do you want it to be displayed?

    ui->label_out->setText(ex.join(' '));
    ui->label_out->setText(ex.join(','));
    ui->label_out->setText(ex.join(', '));
    ui->label_out->setText(ex.join('\n'));
    

    One of those? No loop if you use join(). Otherwise loop:

    ui->label_out->setText(ui->label_out->text() + ex[i])
    

    Or, QLabel is not the best widget choice, maybe you want a QListWidget?



  • Ok, thank you, with the loop it displayed more string, but how I can go to the end of the line after every artist string in QLabel? And if I want to do it in a QTextBrowser?
    At the end, in a QLabel or in a QTextBrowser I need to have a list of artist with hyperlink.

    row_uni read every single line of txt file and extract link and name of an artist.
    Var artist is a QString with link to the wikipedia page. It change for every line I read in a file.
    window_uni is a QTextBrowser.

    for (int i=0; i<100; i++) {
            row_uni = in.readLine();
            url=row_uni.section('"', 1, 1);
            link = "https://it.wikipedia.org"+url;
            artist = row_uni.section(' ', 1, 1);
            ui->label_out->setText(ui->label_out->text() + "<a href=\""+link+"\">"+artist+"</a>");
            //ui->window_uni-> ???
     }
    


  • @sterbilly said in Show a QStringList in QLabel:

    but how I can go to the end of the line after every artist string in QLabel?

    I don't understand what that means. If I have to guess, if you mean put in a line break to move down to the next line, that would be <br /> in HTML5.

    You cannot put a QLabel onto a QTextBrowser. You can put HTML into it.


  • Lifetime Qt Champion

    Hi,

    QTextBrowser has a append method.

    However you might as well build your QString content and then set the final html on the QTextBrower.


Log in to reply