Results 1 to 3 of 3

Thread: Version 4.1.3 and QTreeView

  1. #1
    Join Date
    Mar 2006
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Version 4.1.3 and QTreeView

    Hello,
    I had a QTreeView linked with a model and a delegate, which worked wonderfully under Qt 4.1.1 but when I try the new release 4.1.3, I have a problem in the coordinate function of QTreeView (this function is not the same in qt 4.1.1 & qt 4.1.3).
    There seems to be a problem when I open the delegate and then I move my mouse in the blank space of my view : I have qassert(false).
    Above the coordinate function, there is a note :

    if this is ever changed to not estimate then update item()

    I don't see what it means. Do you ?

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Version 4.1.3 and QTreeView

    Any code to give us??? I don' understand exactly what is your problem...
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Mar 2006
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Version 4.1.3 and QTreeView

    here it is. the problem is when you edit the line "toto" and then you move your mouse in the blank space of the window.
    This is inspired by the simple tree example. I added a delegate.
    Qt Code:
    1. delegate :
    2. ------------------------------------
    3. DelegateCaracs::DelegateCaracs( QObject *parent): QItemDelegate(parent){}
    4.  
    5. QWidget *DelegateCaracs::createEditor(QWidget *parent, const QStyleOptionViewItem & option, const QModelIndex &index) const
    6. {
    7. return new QPushButton(parent);
    8. }
    9.  
    10. view :
    11. ----------------------------------------------
    12. ueSelects::VueSelects():QTreeView(){
    13.  
    14. // on utilise un delegate pour avoir un affichage personnalise
    15.  
    16.  
    17.  
    18. DelegateCaracs* del=new DelegateCaracs(this );
    19.  
    20. setItemDelegate(del);
    21.  
    22. header()->hide();
    23.  
    24.  
    25.  
    26. // on autorise le drag and drop
    27.  
    28. setDragEnabled(true);
    29.  
    30. setAcceptDrops(true);
    31.  
    32. setDropIndicatorShown(true);
    33.  
    34.  
    35.  
    36. setSelectionMode(QAbstractItemView::ExtendedSelection);
    37.  
    38.  
    39.  
    40. // la selection est de couleur jaune (sinon sous linux on ne voit pas le texte)
    41.  
    42. QPalette p = palette();
    43.  
    44. QColor couleurJaunePale = QColor(Qt::white);
    45.  
    46. couleurJaunePale.setRgb(243,247,126);
    47.  
    48. QBrush yellowBrush(couleurJaunePale);
    49.  
    50. p.setBrush(QPalette::Highlight,yellowBrush);
    51.  
    52. setPalette(p);
    53.  
    54.  
    55.  
    56.  
    57.  
    58. }
    59.  
    60.  
    61.  
    62. itemModel
    63. ------------------------------------------------
    64. #include <QStringList>
    65.  
    66. #include "treeitem.h"
    67.  
    68. TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
    69. {
    70. parentItem = parent;
    71. itemData = data;
    72. }
    73.  
    74. TreeItem::~TreeItem()
    75. {
    76. qDeleteAll(childItems);
    77. }
    78.  
    79. void TreeItem::appendChild(TreeItem *item)
    80. {
    81. childItems.append(item);
    82. }
    83.  
    84. TreeItem *TreeItem::child(int row)
    85. {
    86. return childItems.value(row);
    87. }
    88.  
    89. int TreeItem::childCount() const
    90. {
    91. return childItems.count();
    92. }
    93.  
    94. int TreeItem::columnCount() const
    95. {
    96. return itemData.count();
    97. }
    98.  
    99. QVariant TreeItem::data(int column) const
    100. {
    101. return itemData.value(column);
    102. }
    103.  
    104. TreeItem *TreeItem::parent()
    105. {
    106. return parentItem;
    107. }
    108.  
    109. int TreeItem::row() const
    110. {
    111. if (parentItem)
    112. return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
    113.  
    114. return 0;
    115. }
    116.  
    117.  
    118. Model
    119. ---------------------------
    120. TreeModel::TreeModel(const QString &data, QObject *parent)
    121. {
    122. QList<QVariant> rootData;
    123. rootData << "Title" << "Summary";
    124. rootItem = new TreeItem(rootData);
    125. setupModelData(data.split(QString("\n")), rootItem);
    126. }
    127.  
    128. TreeModel::~TreeModel()
    129. {
    130. delete rootItem;
    131. }
    132.  
    133. int TreeModel::columnCount(const QModelIndex &parent) const
    134. {
    135. if (parent.isValid())
    136. return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
    137. else
    138. return rootItem->columnCount();
    139. }
    140.  
    141. QVariant TreeModel::data(const QModelIndex &index, int role) const
    142. {
    143. if (!index.isValid())
    144. return QVariant();
    145.  
    146. if (role != Qt::DisplayRole)
    147. return QVariant();
    148.  
    149. TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
    150.  
    151. return item->data(index.column());
    152. }
    153.  
    154. Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
    155. {
    156. if (!index.isValid())
    157. return Qt::ItemIsEnabled;
    158.  
    159. return Qt::ItemIsEnabled | Qt::ItemIsSelectable| Qt::ItemIsEditable;
    160. }
    161.  
    162. QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
    163. int role) const
    164. {
    165. if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    166. return rootItem->data(section);
    167.  
    168. return QVariant();
    169. }
    170.  
    171. QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
    172. const
    173. {
    174. TreeItem *parentItem;
    175.  
    176. if (!parent.isValid())
    177. parentItem = rootItem;
    178. else
    179. parentItem = static_cast<TreeItem*>(parent.internalPointer());
    180.  
    181. TreeItem *childItem = parentItem->child(row);
    182. if (childItem)
    183. return createIndex(row, column, childItem);
    184. else
    185. return QModelIndex();
    186. }
    187.  
    188. QModelIndex TreeModel::parent(const QModelIndex &index) const
    189. {
    190. if (!index.isValid())
    191. return QModelIndex();
    192.  
    193. TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
    194. TreeItem *parentItem = childItem->parent();
    195.  
    196. if (parentItem == rootItem)
    197. return QModelIndex();
    198.  
    199. return createIndex(parentItem->row(), 0, parentItem);
    200. }
    201.  
    202. int TreeModel::rowCount(const QModelIndex &parent) const
    203. {
    204. TreeItem *parentItem;
    205.  
    206. if (!parent.isValid())
    207. parentItem = rootItem;
    208. else
    209. parentItem = static_cast<TreeItem*>(parent.internalPointer());
    210.  
    211. return parentItem->childCount();
    212. }
    213.  
    214. void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
    215. {
    216. QList<TreeItem*> parents;
    217. QList<int> indentations;
    218. parents << parent;
    219. indentations << 0;
    220. qDebug("%d", lines.size());
    221. int number = 0;
    222.  
    223. while (number < lines.count()) {
    224. int position = 0;
    225. while (position < lines[number].length()) {
    226. if (lines[number].mid(position, 1) != " ")
    227. break;
    228. position++;
    229. }
    230.  
    231. QString lineData = lines[number].mid(position).trimmed();
    232.  
    233. if (!lineData.isEmpty()) {
    234. // Read the column data from the rest of the line.
    235. QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
    236. QList<QVariant> columnData;
    237. for (int column = 0; column < columnStrings.count(); ++column)
    238. columnData << columnStrings[column];
    239.  
    240. if (position > indentations.last()) {
    241. // The last child of the current parent is now the new parent
    242. // unless the current parent has no children.
    243.  
    244. if (parents.last()->childCount() > 0) {
    245. parents << parents.last()->child(parents.last()->childCount()-1);
    246. indentations << position;
    247. }
    248. } else {
    249. while (position < indentations.last() && parents.count() > 0) {
    250. parents.pop_back();
    251. indentations.pop_back();
    252. }
    253. }
    254.  
    255. // Append a new item to the current parent's list of children.
    256. parents.last()->appendChild(new TreeItem(columnData, parents.last()));
    257. }
    258.  
    259. number++;
    260. }
    261. }
    262.  
    263. and main
    264. -------------------------
    265. int main(int argc, char *argv[])
    266. {
    267.  
    268.  
    269. QApplication app(argc, argv);
    270.  
    271.  
    272. TreeModel model("toto");
    273.  
    274. VueSelects view;
    275. view.setModel(&model);
    276. view.setWindowTitle(QObject::tr("Simple Tree Model"));
    277. view.show();
    278. return app.exec();
    279. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTreeView repaint
    By graeme in forum Qt Programming
    Replies: 17
    Last Post: 13th March 2012, 13:27
  2. Replies: 3
    Last Post: 12th May 2006, 19:31
  3. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 14:24
  4. QSortFilterProxyModel & QTreeView
    By Bear in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 15:04
  5. How to dispay an icon in the first column of QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 1
    Last Post: 5th January 2006, 15:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.