Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding text file contents to QListWidget
Qt 6.12 LTS is out! See what's new in the release blog

Adding text file contents to QListWidget

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Laserbeak43
    wrote on last edited by
    #1

    Hello,
    I've been trying to add text file content to QListWidget and have not been successful.
    I was wondering if someone could help?

    so far i have this in my constructor:
    @
    WordEditor::WordEditor(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WordEditor)
    {
    QFile f("data.txt");
    if (!f.open(QIODevice::ReadWrite | QIODevice::Text))
    return;
    ui->setupUi(this);
    QString fileContents = f.readAll();
    QStringList meh = fileContents.split('\n');
    ui->listWidget->addItems(QString(f.readAll()).split('\n'));
    qDebug() << fileContents << meh;
    f.close();
    }@

    my qDebug out put shows me:


    Starting F:\Projects\qt\build-SpellingBee-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\debug\SpellingBee...
    "ewe
    this
    is
    my
    test
    data" ("ewe", "this ", "is", "my", "test", "data")


    Also,
    I have another method that is supposed to add items to QListWidget but it seems to erase and replace items:
    @
    void WordEditor::on_btnAddWord_clicked()
    {
    QFile f("data.txt");
    ui->listWidget->insertItem(ui->listWidget->currentRow()+1, ui->lineEnterWord->text());
    if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
    return;

        QTextStream out(&f);
        out << ui->lineEnterWord->text();
        f.close();
    

    }
    @

    Any guidance would be greatly appreciated.
    Thanks!!

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Laserbeak43
      wrote on last edited by
      #2

      Looks like i've managed to fix the loading issue with:
      @
      WordEditor::WordEditor(QWidget *parent) :
      QDialog(parent),
      ui(new Ui::WordEditor)
      {
      QFile f("data.txt");
      if (!f.open(QIODevice::ReadWrite | QIODevice::Text))
      return;
      ui->setupUi(this);
      QString fileContents = f.readAll();
      QListWidgetItem *itm = new QListWidgetItem;
      itm->setText(fileContents);
      ui->listWidget->insertItem(0,itm);
      f.close();
      }
      @
      I'm still trying to figure out the insert item replacement issue though.

      edit:
      this seems to just make a stringlist one big item. I'll work that out when i get back...

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved