QListView Not Displaying Anything
-
I am stuck on this for a week now and have searched for and read numerous articles on similar issues but have found nothing that has helped.
I have a QListWindow that is a child the centralwidget defined as:
Initially, I am using a QStringList to display using QStringListModel model. My mainwindow (derrived from QMainWindow) has these private member variables:
QListView * List; Ui::MainWindow * ui; QStringListModel * ListModel; QItemDelegate * ListDelegate; QStringList * StringList;
The following code sets things up in the constructor of my main window:
MainWindow::MainWindow( QWidget *parent ) : QMainWindow( parent ), ui( new Ui::MainWindow ), ListModel( new QStringListModel( this ) ), ListDelegate( new QItemDelegate ), StringList( new QStringList ) { ui->setupUi( this ); ListModel->setStringList( *StringList ); *StringList << "1. One" << "2. Two" << "3. Three"; List = ui->centralwidget->findChild<QListView*>( "ElementListView" ); List->setItemDelegate( ListDelegate ); List->setModel( ListModel ); }
I call the show() member function of List in the show() member function of my main window:
void MainWindow::show() { QMainWindow::show(); List->show(); }
I can't figure out what I am missing as only the border of the list appears in my main window. Are there parameters of the delegate that must be set? I thought that the defaults would work for just displaying strings.
One other question I have that will come up once I get the list displaying, how do I make the list be the same size as the main window (less the menu and status bar)? I can set its size to be the same as the main window, but if the user changes the size of the main window, the list should change with it.
-
Your model is empty because you pass an empty QStringList.
-
@Christian-Ehrlicher thanks! For some reason, I thought I have tried moving around the line that puts some dummy data into the string list.
Swapping them fixes this, but there is still a problem. The real information for the list comes from reading a file, so how do I force it to re-display the list once things have been added to the list? I had been thinking that was needed, but didn't find what would do it.
I'll have to do the same thing when something is added or removed from the list as well.
-
Generate the string list and then set it on the model.
On a side note, stop with your QStringList pointers, it does not provide any benefits.
It seems your main issue is that you think that the model is a acting like a "view" on top of a "modifiable" QStringList which it is not.
-
@SGaist thanks for the suggestion. There's no real reason why the string list was a pointer - likely just copying what was being done for the model and the delegate.
This code is being converted from a 15-year old application written to use MFC, so I have to keep shaking the "MFC-think" out of my head.