Important: Please read the Qt Code of Conduct -

How can I view a txt file in QlistWidget.

  • In my code, I'm trying to open a txt file with a openfilename then view this txt file in QlistWidget.
    I have a problem on line 50 in the code: ui-> listWidget-> setText (in.readAll ());
    Being that setText is not a member of QlistWidget class.
    How can I solve this?

    My code:
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFileDialog>
    #include <QMessageBox>
    #include <QFile>
    #include <QTextStream>
    #include <iostream>
    #include <string>
    #include <QListWidget>
    #include <QtGui>

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)


    delete ui;

    void MainWindow::on_pushButton_clicked()


    QString filename=QFileDialog::getOpenFileName(
                tr("Open File"),
                "All files (*.*);; Text File &#40;*.txt&#41;"
    QMessageBox::information(this, tr("File Name"),filename);
    if (!filename.isEmpty()) {
            QFile file&#40;filename&#41;;
            if (! {
                QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
            QTextStream in(&file);


  • Simply don't use QListWidget to do this!!!

    To show plain text use QPlainTextEdit

    If you want use QListWidget (I suggest to don't do this) you must add the file contents line by line with QListWidget::addItem()

  • @Dimitrus, as mcosta says QListWidget isn't good choice to show text files, so better use QPlainTextEdit. But if you badly need to show some text in QListWidget then load text file into QStringList and then add QListWidget items from QStringList.

  • I also want to manipulate that txt file, how to add and delete items and save in txt file.
    QPlainTextEdit is good to do this?
    I'll use a button to add and another to remove.

  • QPlainTextEdit is just like the text input here in the forums, the user can usually edit the text or you can set it to readonly and manipulate it though your buttons if you like. So it depends what you really want to do I guess.

  • With QPlainTextEdit is not possible to do what I want, because I have to open a txt file and each line should behave like an itemand can be selected by full and so I will delete it or will insert another.

    What I want to do is similar to the Matlab image, and these directories that appear in figure, would be those in the txt file that I want to show.

  • Ok why didn't you say that in the first post, then it actually makes sense to use a list view. :)
    I think the "examples in the doc of QListWidget": should show you all you need for a simple list view? if you don't have thousands of lines in your text file that should be performant enough to populate the list with all lines at startup, you could also use a QListView with a custom model and load the text dynamically if you want, but if you text file is not that large that is just more work with the same result I think.

  • Ok, but I can't find an exemplo about how to show a txt file in a QListWidget.
    Can you help me ?

  • Well you know how to read a text file and how to display text in a list widget, sometimes I think nobody want to think for themselves anymore.. :p
    just split the text by line breaks or better read the file line by line directly and create list item for every item, is that so hard?
    QString line = in.readLine(); // in = your QTextStream
    while (!line.isNull()) {
    new QListWidgetItem(line, ui->listWidget);
    line = in.readLine();
    that is all I guess :)

  • Tanks! That works.
    I am unable to think about today.
    I'll back with more questions! :P

Log in to reply