Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I view a txt file in QlistWidget.
QtWS25 Last Chance

How can I view a txt file in QlistWidget.

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 6.3k Views
  • 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.
  • D Offline
    D Offline
    Dimitrius
    wrote on 8 May 2014, 17:00 last edited by
    #1

    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) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()

    {

    QString filename=QFileDialog::getOpenFileName(
                this,
                tr("Open File"),
                "C://",
                "All files (*.*);; Text File &#40;*.txt&#41;"
    
    
                );
    
    
    QMessageBox::information(this, tr("File Name"),filename);
    
    if (!filename.isEmpty()) {
            QFile file&#40;filename&#41;;
            if (!file.open(QIODevice::ReadOnly)) {
                QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
                return;
            }
            QTextStream in(&file);
            ui->listWidget->setText(in.readAll());
            file.close();
        }
    

    }
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 8 May 2014, 17:11 last edited by
      #2

      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()

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zeljko
        wrote on 8 May 2014, 17:50 last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dimitrius
          wrote on 8 May 2014, 18:05 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            Xander84
            wrote on 8 May 2014, 19:12 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dimitrius
              wrote on 8 May 2014, 20:38 last edited by
              #6

              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.
              !http://www.intechgrity.com/wp-content/uploads/2012/12/setpath-dialogue-box2.png

              1 Reply Last reply
              0
              • X Offline
                X Offline
                Xander84
                wrote on 8 May 2014, 20:55 last edited by
                #7

                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":http://qt-project.org/doc/qt-5/qlistwidget.html#details 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.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dimitrius
                  wrote on 8 May 2014, 21:50 last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    Xander84
                    wrote on 8 May 2014, 21:57 last edited by
                    #9

                    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 :)

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dimitrius
                      wrote on 8 May 2014, 22:21 last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      1/10

                      8 May 2014, 17:00

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved