PDA

View Full Version : QTreeView - Children Displaying Same Data As Parent, WhatsThisRole is Not



vulcan
17th January 2013, 22:00
Hi all,

First post here - I'm beginning on Qt, having just gotten a good foothold [though I suppose these things are always subjective] on C++. Here's my issue:

I'm creating a QTreeView hooked up to a custom model that stores file paths for the program to process, sorted by "Device" (or some other arbitrary header). If the program gets passed a valid file path, it should automatically parent it to a new device.

So, the idea is to have:
Root Item
|
- Device Item #1
- File #1
- File #2
- ....
- Device Item #2
[/code]

In the data structure, everything seems fine. Things are constructed appropriately, in Debug mode, I can walk through from the root item all the way to each child through the recursive layers of QList<Node *>. All the data seems correct.

When I hook it up to the QTreeView, and try to expand, I end up with the DisplayRole data of the parent being displayed for all the children. I've tried variations with deeper levels of children/parents, but it always shows me incorrect data for the children. In what seems like adding insult to injury, the WhatsThisRole data actually displays correctly - I can hover over the item and it will show me the correct text (it showing me the correct text was an addition by me in the data() function just to make sure I wasn't going crazy). Adding to the confusion, it appears that the selection model seems to be off a bit - I can't unclick items once selected, and selecting a child item selects all child items, same parent or not.

It has to be something in my code. I have a bunch of logging/debugging lines set into it that will take a bit to back out (without losing them). I'm assuming though that the issue is in my Model implementation. Being my first time with this, I'm heavily leaning on the examples provided in books and online to get a good model implementation going. Code below.



QModelIndex ParseList::index(int row, int column, const QModelIndex &parent) const
{
if(!head || row < 0 || column < 0)
{
return QModelIndex();
}
ParseNode * parentNode = nodeFromIndex(parent);
ParseNode * childNode = parentNode->getChild(row);
if(!childNode)
{
return QModelIndex();
}
QModelIndex temp = createIndex(row,column, childNode);
return createIndex(row, column, childNode);
}

QModelIndex ParseList::parent(const QModelIndex &child) const
{
ParseNode * node = nodeFromIndex(child);
if(!node)
return QModelIndex();
ParseNode * parentNode = node->getParent();
if(!parentNode)
{
return QModelIndex();
}
ParseNode * grandparentNode = parentNode->getParent();
if(!grandparentNode)
{
return QModelIndex();
}
int row = grandparentNode->indexOf(parentNode);
return createIndex(row, 0, parentNode);
}

int ParseList::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
{
return 0;
}
ParseNode *parentNode = nodeFromIndex(parent);
if(!parentNode)
{
return 0;
}
return parentNode->childCount();
}

int ParseList::columnCount(const QModelIndex &parent) const
{
return ParseNode::dataColumns;
}

QVariant ParseList::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
{
return QVariant();
}
ParseNode * node = nodeFromIndex(index);
if (!node)
{
return QVariant();
}
//TODO: Decide if smaller functions would be better
// - small private functions

switch(index.column())
{
case 0:
switch(role)
{
case Qt::DisplayRole:
{
return node->getPathString(); //stored as QString in node
}
case Qt::WhatsThisRole:
case Qt::StatusTipRole:
case Qt::ToolTipRole:
return node->getPathString();//tr("Path of selected log"); //Added for debug
default:
return QVariant();
}
case 1:
switch(role)
{
case Qt::DisplayRole:
return node->getParseStatus() ? tr("Yes") : tr("No"); //Stored as bool in underlying class
case Qt::WhatsThisRole:
case Qt::StatusTipRole:
case Qt::ToolTipRole:
return node->getParseStatus() ?
tr("Log has been parsed") :
tr("Log has not been parsed");
default:
return QVariant();
}
case 2:
switch(role)
{
case Qt::DisplayRole:
switch(node->getPathType()) //public enum declared by ParseNode
{
case ParseNode::File:
return tr("File");
case ParseNode::FileButNotReadable:
return tr("File - Unreadable");
case ParseNode::Directory:
return tr("Directory");
case ParseNode::Invalid:
return tr("Invalid");
case ParseNode::Device:
return tr("Device");
default:
return QVariant();
}
case Qt::WhatsThisRole:
case Qt::StatusTipRole:
case Qt::ToolTipRole:
switch(node->getPathType())
{
case ParseNode::File:
return tr("File from system");
case ParseNode::FileButNotReadable:
return tr("File exists, but is unreadable");
case ParseNode::Directory:
return tr("Directory from system");
case ParseNode::Invalid:
return tr("Invalid path specified");
default:
return QVariant();
}
default:
return QVariant();
}
default:
return QVariant();
}
}


And here's a screencap with things hooked up and at their worst:
8592

When I take out the "auto-reparenting" code, everything displays correctly on the top level, but again, all the children display the parent's data.

Hopefully I've provided a good start of information - does anyone have any ideas? I can work on trimming things up further to get a .zip of what I'm seeing together as a compilable example.

I'm using Qt 4.8.1 with MinGW as a compiler. The IDE I'm using is Qt Creator 2.4.1.

Thanks in advance!

Santosh Reddy
18th January 2013, 07:43
I suspect a problem in either ParseList::parent()/ParseList::index() or both. My bet is on ParseList::index().

I cannot suggest much without knowing what ParseNode does.

What is head?

Why grandparent / grandchild? Model can be implement with just parent relation.

Can you post a compilable code?

Anyway here are few tips:
1. Store parent's row information in ParseNode, so that you get back parent info when ParseList::parent() is called. (I can guess that is what ParseNode does)
2. Don't store QModelIndex in ParseNode.
3. In ParseList::rowCount(), ParseList::columnCount(), check for valid index.

vulcan
18th January 2013, 20:06
What is head?


Head was my mistake. It should be (and is now) properly named as root.



Why grandparent / grandchild? Model can be implement with just parent relation.


The grandparent was used to find the parent's index in the list. Even though I have removed it from this context, the ParseNode::myIndex() function does the same idea. The thought is what happens if items are inserted into the list? (Especially since I'm using QList and not QLinkedList and expect to support moving items around). Should I cache its position in ParseNode, and update the children when a change happens with the container?




Anyway here are few tips:
1. Store parent's row information in ParseNode, so that you get back parent info when ParseList::parent() is called. (I can guess that is what ParseNode does)
2. Don't store QModelIndex in ParseNode.
3. In ParseList::rowCount(), ParseList::columnCount(), check for valid index.


1. Definitely can do (is what I described above what you are saying)?
2. Yep. Also can do. The only times I created one outside of returning it from createIndex() is when I needed a temp object to dissect for debugging messages.
3. How would I handle the root passed to rowCount()?

Would you mean something like this?


int ParseList::rowCount(const QModelIndex &parent) const
{
if(!parent.isValid())
{
return root->childCount();
}
if (parent.column() > 0)
{
return 0;
}
ParseNode *parentNode = nodeFromIndex(parent);
if(!parentNode)
return 0;
return parentNode->childCount();
}


A compilable example is attached.
8593

Thanks very much for your reply and already useful help!

vulcan
22nd January 2013, 19:31
Does anyone have any ideas? I'm stuck going in circles and can't seem to find where the issue is. Is there any additional information I can provide, or perhaps things I can look for?

Thanks!

wysota
22nd January 2013, 19:53
This is surely invalid:


void ParseNode::addChild(ParseNode *child)
{
if(child && children.indexOf(child) == 0); // <--- semicolon here?
children.push_back(child);
}

In my opinion it should be:


void ParseNode::addChild(ParseNode *child)
{
if(child && children.indexOf(child) < 0) {
children.push_back(child);
child->myParent = this;
}
}

removeChild() has to be adjusted accordingly.

ParseList::addPaths() looks invalid as well.... You're (incorrectly) informing the model that you'll be adding top-level items but then you're never attaching items you're adding to the root node (you only set it as the new item's parent but never add it to the root node's list of children thus parent() implementation can't work correctly). Maybe there are other problems as well, I can't see them right now.

A minimal tree implementation looks more or less like this:


#include <QApplication>
#include <QAbstractItemModel>
#include <QTreeView>

template<typename T> class Node {
public:
Node() { parentNode = 0; }
~Node() {
if(parentNode)
parentNode->removeChild(this);
qDeleteAll(children);
}
void addChild(Node *n) {
if(n->parentNode) {
if(n->parentNode == this) return;
n->parentNode->removeChild(n);
n->parentNode = 0;
}
n->parentNode = this;
children.append(n);
}
void removeChild(Node *n) {
if(n->parentNode != this) return;
n->parentNode = 0;
children.removeOne(n);
}
int indexOf(Node *n) const { return children.indexOf(n); }
Node *at(int idx) const { return children.at(idx); }
int count() const { return children.count(); }
Node *parent() const { return parentNode; }
T data;

private:
Node<T> *parentNode;
QList<Node<T>*> children;
};

template<typename T, int ColCount> class TreeModel : public QAbstractItemModel {
public:
TreeModel(QObject *parent = 0) : QAbstractItemModel(parent) {
root = new Node<T>();
}
~TreeModel() { delete root; }
int rowCount(const QModelIndex &parent = QModelIndex()) const {
return nodeFromIndex(parent)->count();
}
int columnCount(const QModelIndex &parent = QModelIndex()) const {
return ColCount;
}
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const {
Node<T> *parentNode = nodeFromIndex(parent);
if(row < 0 || row >= parentNode->count() || column < 0 || column >= ColCount) return QModelIndex();
return createIndex(row, column, parentNode->at(row));
}
QModelIndex parent(const QModelIndex &child) const {
Node<T> *childNode = nodeFromIndex(child);
Node<T> *parentNode = childNode->parent();
if(parentNode == root) return QModelIndex();
Node<T> *grandparentNode = parentNode->parent();
return createIndex(grandparentNode->indexOf(parentNode), 0, parentNode);
}
QVariant data(const QModelIndex &index, int role) const {
qWarning("Reimplement in subclass");
return QVariant();
}
protected:
Node<T>* nodeFromIndex(const QModelIndex &idx) const {
if(idx.isValid()) return static_cast<Node<T>*>(idx.internalPointer());
return root;
}
Node<T> *root;
};

class Model : public TreeModel<int, 1> {
public:
Model(QObject *parent = 0) : TreeModel(parent) {}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const {
if(index.column() != 0) return QVariant();
if(role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
Node<int> *n = nodeFromIndex(index);
return n->data;
}
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) {
if(index.column() != 0) return false;
if(role != Qt::DisplayRole && role != Qt::EditRole) return false;
Node<int> *n = nodeFromIndex(index);
if(n->data != value.toInt()) {
n->data = value.toInt();
emit dataChanged(index, index);
return true;
}
return true;
}
void addItem(int dat) {
beginInsertRows(QModelIndex(), root->count(), root->count());
Node<int> *n = new Node<int>();
n->data = dat;
Node<int> *sn = new Node<int>();
sn->data = dat*10;
n->addChild(sn);
root->addChild(n);
endInsertRows();
}
};

class Adder : public QObject {
public:
Adder(Model *model, int interval = 2000) {
m_model = model;
startTimer(interval);
}
protected:
void timerEvent(QTimerEvent *) {
int val = rand() % 100;
m_model->addItem(val);
}

private:
Model *m_model;
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeView w;
Model model;
// TreeModel<int,1> model;
w.setModel(&model);
w.show();
Adder adder(&model, 1000);
return a.exec();
}