[Qt-interest] header Data of Tree Model is Read-Only??
Gan Xiebin
gan.xiebin at gmail.com
Wed May 27 03:18:25 CEST 2009
I implemented the following functions, but it still no work for me...
Did I missing something ??
-----------------------
Editable Items
Editable models allow items of data to be modified, and may also provide
functions to allow rows and columns to be inserted and removed. To enable
editing, the following functions must be implemented correctly:
flags() <http://doc.trolltech.com/4.5/qabstractitemmodel.html#flags>Must
return an appropriate combination of flags for each item. In particular, the
value returned by this function must include
Qt::ItemIsEditable<http://doc.trolltech.com/4.5/qt.html#ItemFlag-enum>in
addition to the values applied to items in a read-only model.
setData() <http://doc.trolltech.com/4.5/qabstractitemmodel.html#setData>Used
to modify the item of data associated with a specified model index. To be
able to accept user input, provided by user interface elements, this
function must handle data associated with
Qt::EditRole<http://doc.trolltech.com/4.5/qt.html#ItemDataRole-enum>.
The implementation may also accept data associated with many different kinds
of roles specified by
Qt::ItemDataRole<http://doc.trolltech.com/4.5/qt.html#ItemDataRole-enum>.
After changing the item of data, models must emit the
dataChanged()<http://doc.trolltech.com/4.5/qabstractitemmodel.html#dataChanged>signal
to inform other components of the change.
setHeaderData()<http://doc.trolltech.com/4.5/qabstractitemmodel.html#setHeaderData>Used
to modify horizontal and vertical header information. After changing the
item of data, models must emit the
headerDataChanged()<http://doc.trolltech.com/4.5/qabstractitemmodel.html#headerDataChanged>signal
to inform other components of the change.
--------------------------
On Tue, May 26, 2009 at 5:54 PM, Gan Xiebin <gan.xiebin at gmail.com> wrote:
>
> Hi All,
>
> I want to set header data of my tree model ,
> like so:
>
> // myTreeModel.cpp
>
> myTreeModel::myTreeModel( QObject *parent )
> : QAbstractItemModel(parent)
> {
> QList<QVariant> rootData;
> rootData << "Name" << "IP" << "Status";
> rootNode = new Node(rootData);
> }
>
> QVariant myTreeModel::headerData(int section, Qt::Orientation orientation,
> int role) const
> {
> if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
> return rootNode->data(section);
>
> return QVariant();
> }
>
>
> Qt::ItemFlags myTreeModel::flags(const QModelIndex &index) const
> {
> if (!index.isValid())
> return 0;
>
> return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
> }
>
> bool myTreeModel::setHeaderData(int section, Qt::Orientation orientation,
> const QVariant &value, int role)
> {
> if (role != Qt::EditRole || orientation != Qt::Horizontal)
> return false;
>
> bool result = rootNode->setData(section, value);
>
> if (result)
> emit headerDataChanged(orientation, section, section);
>
> return result;
> }
>
> ------------------------------------
>
>
> When I used QTreeView to display my Tree Model data,
> I found that, normal data shows fine and is editable.
> but the header data can not be edited ...
>
> Did I missing something ??
>
> Thanks,
>
> Bin
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090527/48e5a3b4/attachment.html
More information about the Qt-interest-old
mailing list