gpt4 book ai didi

c++ - Qt 将文本文件中的行分配给标签

转载 作者:行者123 更新时间:2023-11-28 05:40:06 25 4
gpt4 key购买 nike

到目前为止的工作原理:通过QFileDialog 打开一个文本文件并在Qlabel(showfile) 中显示文本文件的内容。我也有一个 for-loop 来计算文本文件中有多少 \n 。现在我想要的是将文本文件中的一行接一行分配给一个新的 Qlabel,这意味着每个 Qlabel 都包含文本文件的一行并将其动态放置在运行时。

也许你可以帮忙,因为我有点卡在这里。

这是我应该放置标签的 qwidget 类:

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;
};

这是我在 QMainwindow 类中的打开方法:

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

最佳答案

您可以为从文件中读取的每一行添加一个新的 QLabel 到布局中。您可以将标签存储在 container 中喜欢QVector这样您以后就可以访问他们的文本。这是一个例子:

#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"

关于c++ - Qt 将文本文件中的行分配给标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37299035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com