Patrick260284
Patrick260284

Reputation: 89

Qt assign lines from textfile to qlabels

What works so far: opening a textfile via QFileDialog and showing the contents of the textfile in a Qlabel(showfile). I too have a for-loop that counts how many \n are in the textfile. Now what I want is that line after line in the textfile is assigned to a new Qlabel meaning each Qlabel contains one line of the textfile and place it dynamically on runtime.

Maybe you can help since I´m a little stuck here.

Here´s my qwidget class where the labels should be placed:

class mywidget:public QWidget       //class for displaying everything
{
    Q_OBJECT

private:
    QGridLayout *area;
    QLabel *showfile;               //shows input from textfile
    QLabel *processname,*status;    //captionlabel
    QFont *pfont,*sfont;            //fontoption for processname&status
    QLabel **processes;             //2D array to dynamically create QLabels for each entry in file


public:
    mywidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags flags = 0):QWidget(parent,flags)
    {
        this->area = new QGridLayout(this);
        this->showfile = new QLabel(tr("Test"),this);
        this->pfont = new QFont();
        this->pfont->setPixelSize(20);
        this->sfont = new QFont();
        this->sfont->setPixelSize(20);
        this->processname = new QLabel(tr("Process_Name:"),this);
        this->processname->setFont(*pfont);
        this->status = new QLabel(tr("Status:"),this);
        this->status->setFont(*sfont);
        this->area->addWidget(this->processname,0,0,Qt::AlignHCenter);
        this->area->addWidget(this->status,0,1,Qt::AlignHCenter);
        this->area->addWidget(this->showfile,1,0,Qt::AlignHCenter);
        this->area->setSpacing(10);
    }
    ~mywidget()
    {
        delete this->area;
        delete this->showfile;
        delete this->pfont;
        delete this->sfont;
        delete this->processname;
        delete this->status;
    }
    friend class mywindow;
};

And here is my open-method from QMainwindow class:

void mywindow::oeffnen()
{
    this->openfilename = QFileDialog::getOpenFileName              //open textfile dialog
        (this,
         tr("Datei öffnen"),
         QDir::homePath(),
         "Textdateien (*.txt *.docx *.doc);;" "Alle Dateien (*.*)"
         );

    if(!this->openfilename.isEmpty())
    {
        this->file = new QFile(this->openfilename);
        this->file->open(QIODevice::ReadOnly);
        this->stream = new QTextStream(this->file);
        this->fileread = this->stream->readAll();



        for(int z = 0;z<this->fileread.length();++z)                //check entries in string by counting \n
        { 
            this->eintraege = this->fileread.count(QRegExp("\n"));
        }
        //this->s_eintraege = QString::number(this->eintraege);       //converting to string for displaying





        this->central->showfile->setText(this->fileread);           //assign filecontent to label



        if(!this->file->isReadable())
        {
            QMessageBox::information(this,
                                     tr("Fehler"),
                                     tr("Konnte Datei %1 nicht laden!").arg(this->openfilename)
                                     );
        }
        else
        {
            QMessageBox::information(this,
                                     tr("OK"),
                                     tr("Konnte Datei %1 laden!").arg(this->openfilename)
                                     );
        }
        this->file->close();    
    }
}

Upvotes: 0

Views: 316

Answers (1)

thuga
thuga

Reputation: 12931

You can add a new QLabel into a layout for each new line you read from a file. You can store the labels in a container like QVector so you can access their text later on. Here is an example:

#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QFile>
#include <QLayout>
#include <QTextStream>
#include <QDebug>

class DisplayWidget : public QWidget
{
    Q_OBJECT

public:
    DisplayWidget(QWidget *parent = 0) : QWidget(parent)
    {
        labelLayout = new QVBoxLayout;
        setLayout(labelLayout);
        resize(200, 200);
    }
    void addLabel(const QString &text)
    {
        QLabel *label = new QLabel(text);
        label_vector.append(label);
        labelLayout->addWidget(label);
    }
    void readFile(const QString &filename)
    {
        QFile file(filename);
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;
        QTextStream ts(&file);
        while(!ts.atEnd())
        {
            QString line = ts.readLine();
            if(!line.isEmpty())
                addLabel(line);
        }
    }
    QString getLabelText(int index)
    {
        if(label_vector.size() > index)
            return label_vector[index]->text();
        return QString();
    }

private:
    QVBoxLayout *labelLayout;
    QVector<QLabel*> label_vector;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    DisplayWidget w;
    w.readFile("somefile.txt");
    w.show();
    qDebug() << w.getLabelText(3);

    return a.exec();
}

#include "main.moc"

Upvotes: 1

Related Questions