PDA

View Full Version : QStandardItem: Setting Top Column Data?



AaronMK
24th January 2008, 17:34
I am using QStandardItem (http://doc.trolltech.com/latest/qstandarditem.html) and QStandardModel (http://doc.trolltech.com/latest/qstandardmodel.html) for my data model. I like this much better than the base QAbstractItemView (http://doc.trolltech.com/latest/qabstractitemview.html) because of the Item class that I can use to treat clumps of data as a single object, but I can't figure out how to get any data into columns other than the root column on the first row.

Example Code:


class QMyModel : public QStandardItemModel
{
Q_OBJECT
public:
QMyModel(QObject *parent) : QStandardItemModel(parent) {};
virtual ~QMyModel();

enum Columns
{
COLUMN_NAME,
COLUMN_CUSTOM,
NUM_COLUMNS
};
};

class QMyItem : public QStandardItem
{
QMyItem(QString Name, QString Description, QString First, QString Second)
: QStandardItem()
{
setData(Name, Qt::DisplayRole);
// TODO: Put Description beside Name and make editable in TreeView.
setChild(0, QMyModel::COLUMN_NAME, "First:");
setChild(0, QMyModel::COLUMN_CUSTOM, First);
setChild(1, QMyModel::COLUMN_NAME, "Second:");
setChild(1, QMyModel::COLUMN_CUSTOM, Second);
}
}
This results in the following for the QTreeView (http://doc.trolltech.com/latest/qtreeview.html) with QMyItem("Item Name", "A Simple Item", "String 1", "String 2"):

+ Item Name
- - First: | String 1
- - Second: | String 2

How do I get it to look like this:
+ Item Name | A Simple Item
- - First: | String 1
- - Second: | String 2

And is there a way to do it within the QStandardItem, instead of having to go back to the parent view?

Thank you for your assistance.

jpn
24th January 2008, 18:07
See QStandardItemModel::setItem(int row, int column, QStandardItem* item). Notice the column parameter.

AaronMK
24th January 2008, 20:13
See QStandardItemModel::setItem(int row, int column, QStandardItem* item). Notice the column parameter.

A couple problems with this:

During construction, there is no parent model.
There is no slot or virtual function that is triggered when the Item is attached to a model, so delaying construction of that item is not an option.


True, I could pass the parent to the constructor, insert it manually (Which row?), create the QStandardItem for the second item of the top column for Description, and set Description as a child or sibling of the main item. At that point, I might as well go back to the basic QAbstractModel, and would lose the modularity that I am trying to get by using QStandardItem in the first place.