Adding text file contents to QListWidget
-
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!! -
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...