Building Tree view from Table view which is already build.
Hi,
I have already build table view (by customizing QAbstractItemModel & QTableView).
My table view looks as below
Code:
Level Name Rank Sec
0 Rajesh 1 A
1 Krish 2 A
2 Ram 3 B
1 Krish 4 C
0 Krish 5 B
1 Krish 6 a
1 Krish 7 A
0 Rajesh 1 A
1 Krish 2 A
2 Ram 3 B
0 Rajesh 1 A
1 Krish 2 A
2 Ram 3 B
Now based on the level column I need to build the tree, Is it possible to re use the same model(from table view) and create tree view based on level column ?
or do I need to create separate new model for tree again. If it is possible please let me know what i need to implement for this.
Tree view : all the Level 0's are parents, followed by 1's are childs of 0's and 2's are childs of 1's.
Thanks in Advance :-)
Re: Building Tree view from Table view which is already build.
Your model would need to maintain data such that a parent/child relationship is maintained so that your QTreeView can display the parent/child hierarchy.
You should be able to also use the same model to display a QTableView, your model would essentially need to present data for all of the leaf nodes, with the Level of the parent being returned as one of the columns, etc.
Re: Building Tree view from Table view which is already build.
While it is possible to change structure with a proxy model, it is not something I would recommend unless you are already quite skilled with the model/view internals.
Creating a tree model that works on the same data as your table model sounds a lot easier.
Cheers,
_
Re: Building Tree view from Table view which is already build.
Hi, I am implementing Lazy population tree view.
have implemented hasChilderen(), fetchMore and canFetchMore() functions as below.
I am getting the Error: QSortFilterProxyModel: invalid inserted rows reported by source model;
Please let me know the problem.
Code:
bool TreeModel
::canFetchMore(const QModelIndex &parent
) const {
if(!parent.isValid())
return true;
if(m_totalRowCount > m_lastreadRowIndex) //I am storing lst read row value
{
return true;
}
return false;
}
void DebugTreeModel::fetchMore(const QModelIndex& parent)
{
//to fect readRows od records (rows) from sql
int readRows = 1000;
if( m_totalRowCount < ( readRows + m_lastreadRowIndex ) )
{
readRows = m_totalRowCount - m_lastreadRowIndex;
}
//will get me last row number
qint64 lastRow = m_lastreadRowIndex ;
emit fetchMoreData(readRows); //will fecth readRows number of records form the sql and form tree
qint64 parentsInFetchedData = getZerosInFetchedData();
//i have writtem logic so that, next inserted row should be 0 (so parent should be m_rootItem always (it is my top most parent))
//so I am passing m_rootItem as root and last inserted row number, num of row to insert.
beginInsertRows
(QModelIndex(), lastRow , lastRow
+parentsInFetchedData
-1 );
//same result for beginInsertRows(parent, lastRow , lastRow+parentsInFetchedData -1 ); endInsertRows();
}
Re: Building Tree view from Table view which is already build.
What have you done to debug this? When you are inserting rows, what range of row numbers are you passing to beginInsertRows, etc?
Also, I believe someone else had mentioned model test as a good way to debug models. Have you tried that?
Edit: I would add that trying to implement canFetchMore and fetchMore for a model that can't properly insert data seems like putting the cart before horse to me. Focus on getting the basics working before you try to pimp out your model!
Re: Building Tree view from Table view which is already build.
Quote:
Originally Posted by
DURGAPRASAD NEELAM
Hi, I am implementing Lazy population tree view.
have implemented hasChilderen(), fetchMore and canFetchMore() functions as below.
I am getting the Error: QSortFilterProxyModel: invalid inserted rows reported by source model;
Please let me know the problem.
This is a cross-post from http://www.qtcentre.org/threads/6224...h-lazy-loading
Re: Building Tree view from Table view which is already build.
Quote:
Originally Posted by
anda_skoa
While it is possible to change structure with a proxy model, it is not something I would recommend unless you are already quite skilled with the model/view internals._
Could you please explain little more about this, small example or any link to explore would be great :-)
Thanks.