Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListView Not Displaying Anything

QListView Not Displaying Anything

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 386 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Calvin H-C
    wrote on 9 Oct 2023, 15:34 last edited by Calvin H-C 10 Sept 2023, 15:37
    #1

    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:
    Designer.png

    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.

    C 1 Reply Last reply 9 Oct 2023, 16:36
    0
    • C Calvin H-C
      9 Oct 2023, 15:34

      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:
      Designer.png

      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.

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 9 Oct 2023, 16:36 last edited by
      #2

      Your model is empty because you pass an empty QStringList.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      C 1 Reply Last reply 9 Oct 2023, 19:35
      3
      • C Christian Ehrlicher
        9 Oct 2023, 16:36

        Your model is empty because you pass an empty QStringList.

        C Offline
        C Offline
        Calvin H-C
        wrote on 9 Oct 2023, 19:35 last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 9 Oct 2023, 19:43 last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply 10 Oct 2023, 00:04
          1
          • S SGaist
            9 Oct 2023, 19:43

            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.

            C Offline
            C Offline
            Calvin H-C
            wrote on 10 Oct 2023, 00:04 last edited by
            #5

            @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.

            1 Reply Last reply
            0

            4/5

            9 Oct 2023, 19:43

            • Login

            • Login or register to search.
            4 out of 5
            • First post
              4/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved