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. QTreeView does not show data
Forum Updated to NodeBB v4.3 + New Features

QTreeView does not show data

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 465 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Stephan Lorenzen
    wrote on last edited by
    #1

    Dear Forum,

    I am trying to redo the QTreeView minimal example described in
    https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

    Basically, all my code is from that side. However, the TreeView is empty and shows no data at all:
    emptyTreeView.png

    What am i doing wrong?

    MainWindow::MainWindow() :
    	QMainWindow(),
    	treeView(new QTreeView(this)),
    	treeModel(nullptr)
    {
    	setCentralWidget(treeView);
    
    	// initialize items with parent and row number
    	TreeItem *rootItem = new TreeItem(nullptr, -1);
    	TreeItem *item_0   = new TreeItem(rootItem, 0);
    	TreeItem *item_1   = new TreeItem(rootItem, 1);
    	TreeItem *item_0_0 = new TreeItem(item_0,   0);
    	TreeItem *item_0_1 = new TreeItem(item_0,   1);
    
    	// add children to parenta
    	rootItem->child.push_back(item_0);
    	rootItem->child.push_back(item_1);
    	item_0  ->child.push_back(item_0_0);
    	item_0  ->child.push_back(item_0_1);
    
    	treeModel = new TreeModel(rootItem);
    
    	treeView->setModel(treeModel);
    }
    
    QModelIndex TreeModel::index(int rowNr, int column, const QModelIndex& parent) const
    {
    	if (!hasIndex(rowNr, column, parent)) return QModelIndex();
    	const TreeItem *parentTI;
    	if (!parent.isValid()) parentTI = rootItem;
    	else parentTI = static_cast<TreeItem*>(parent.internalPointer());
    	TreeItem *ti = parentTI->child[rowNr];
    	return (createIndex(rowNr, column, ti));
    }
    
    
    
    QModelIndex TreeModel::parent(const QModelIndex& child) const
    {
    	if (!child.isValid()) return QModelIndex();
    	TreeItem *childItem = static_cast<TreeItem*>(child.internalPointer());
    	TreeItem *parentItem = childItem->parent;
    	if (parentItem == rootItem) return QModelIndex();
    	return(createIndex(parentItem->row, 0, parentItem));
    }
    
    int TreeModel::rowCount(const QModelIndex &parent) const
    {
    	const TreeItem *parentTI;
    	if (!parent.isValid()) parentTI = rootItem;
    	else parentTI = static_cast<TreeItem*>(parent.internalPointer());
    	return (parentTI->child.size());
    }
    
    int TreeModel::columnCount(const QModelIndex &) const {return (2);}
    
    QVariant TreeModel::data(const QModelIndex &index, int) const
    {
    	return(QVariant(QString::fromStdString("TEST")));
    }
    

    Best

    Stephan

    Christian EhrlicherC 1 Reply Last reply
    0
    • S Stephan Lorenzen

      Dear Forum,

      I am trying to redo the QTreeView minimal example described in
      https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

      Basically, all my code is from that side. However, the TreeView is empty and shows no data at all:
      emptyTreeView.png

      What am i doing wrong?

      MainWindow::MainWindow() :
      	QMainWindow(),
      	treeView(new QTreeView(this)),
      	treeModel(nullptr)
      {
      	setCentralWidget(treeView);
      
      	// initialize items with parent and row number
      	TreeItem *rootItem = new TreeItem(nullptr, -1);
      	TreeItem *item_0   = new TreeItem(rootItem, 0);
      	TreeItem *item_1   = new TreeItem(rootItem, 1);
      	TreeItem *item_0_0 = new TreeItem(item_0,   0);
      	TreeItem *item_0_1 = new TreeItem(item_0,   1);
      
      	// add children to parenta
      	rootItem->child.push_back(item_0);
      	rootItem->child.push_back(item_1);
      	item_0  ->child.push_back(item_0_0);
      	item_0  ->child.push_back(item_0_1);
      
      	treeModel = new TreeModel(rootItem);
      
      	treeView->setModel(treeModel);
      }
      
      QModelIndex TreeModel::index(int rowNr, int column, const QModelIndex& parent) const
      {
      	if (!hasIndex(rowNr, column, parent)) return QModelIndex();
      	const TreeItem *parentTI;
      	if (!parent.isValid()) parentTI = rootItem;
      	else parentTI = static_cast<TreeItem*>(parent.internalPointer());
      	TreeItem *ti = parentTI->child[rowNr];
      	return (createIndex(rowNr, column, ti));
      }
      
      
      
      QModelIndex TreeModel::parent(const QModelIndex& child) const
      {
      	if (!child.isValid()) return QModelIndex();
      	TreeItem *childItem = static_cast<TreeItem*>(child.internalPointer());
      	TreeItem *parentItem = childItem->parent;
      	if (parentItem == rootItem) return QModelIndex();
      	return(createIndex(parentItem->row, 0, parentItem));
      }
      
      int TreeModel::rowCount(const QModelIndex &parent) const
      {
      	const TreeItem *parentTI;
      	if (!parent.isValid()) parentTI = rootItem;
      	else parentTI = static_cast<TreeItem*>(parent.internalPointer());
      	return (parentTI->child.size());
      }
      
      int TreeModel::columnCount(const QModelIndex &) const {return (2);}
      
      QVariant TreeModel::data(const QModelIndex &index, int) const
      {
      	return(QVariant(QString::fromStdString("TEST")));
      }
      

      Best

      Stephan

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      @Stephan-Lorenzen said in QTreeView does not show data:

      QVariant TreeModel::data(const QModelIndex &index, int) const
      {
      return(QVariant(QString::fromStdString("TEST")));
      }

      This is wrong - you should only return something for Qt::DisplayRole.

      btw: Why convert a plain char string to a std::string just to convert it to a QString? QString can take a plain char* too.

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

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Stephan Lorenzen
        wrote on last edited by
        #3

        Wow, you are great -- it works!! Thanks a lot!
        Just for curiosity - what exactly goes wrong if data() returns a string for another role? I see things like TooltipRole or StatustipRole, it seems to me that it should not harm if something would be returned for these roles?

        Christian EhrlicherC 1 Reply Last reply
        0
        • S Stephan Lorenzen

          Wow, you are great -- it works!! Thanks a lot!
          Just for curiosity - what exactly goes wrong if data() returns a string for another role? I see things like TooltipRole or StatustipRole, it seems to me that it should not harm if something would be returned for these roles?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Stephan-Lorenzen said in QTreeView does not show data:

          it seems to me that it should not harm if something would be returned for these roles?

          There are also roles like e.g. the font role or color roles. I would guess it's the font role.

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

          JonBJ 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @Stephan-Lorenzen said in QTreeView does not show data:

            it seems to me that it should not harm if something would be returned for these roles?

            There are also roles like e.g. the font role or color roles. I would guess it's the font role.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            I sort of guessed it might be that too, because QFont contructor takes a string as a "family name". But it says/implies it will pick some other font when it doesn't recognise "TEST"?

            1 Reply Last reply
            0

            • Login

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