PDA

View Full Version : Display QStrings in QListWidget



emp1953
2nd May 2019, 22:09
I am using RHEL 6 and Qt4.8
Also using QtCreator.

I have an array of QStrings that I read in from a .csv file.

I wish to display these in a ListWidget that I have inserted into a mainwindow panel.

the data type that the ListWidget accepts is (const QString & label)

What conversion do I need to perform on the QStrings in order to add them the ListWidget?



for(int i = 0; i< 100; i++ ) {
ListWidget->addItem(qstr_array[i]); //compiles and runs but nothing is displayed in the list widget and puts 100 blank lines in the widget.
}


for(int i = 0; i< 100; i++ ) {
ListWidget->addItem("snow"); //compiles and runs and displays correctly in the list widget
}




Thank you for any help

anda_skoa
3rd May 2019, 09:34
That would indicate that your array elements are empty strings.

Btw: if you read your input into QStringList, you can simply use QListWidget::addItems()

Cheers,
_

emp1953
3rd May 2019, 17:26
That would indicate that your array elements are empty strings.

Btw: if you read your input into QStringList, you can simply use QListWidget::addItems()

Cheers,
_

I do print out the array using qDebug() and all the elements contain the correct strings. I'll try your QStringList suggestion.

Thank You

d_stranz
4th May 2019, 01:27
I do print out the array using qDebug() and all the elements contain the correct strings.

Are you sure you didn't accidentally define qstr_array as both a member variable of the class and as a local variable at the place where you read the strings from the file? If you defined it as a local variable, that "hides" the member variable and will be destroyed as soon as the read function exits, leaving you with a member variable with no content.