Results 1 to 2 of 2

Thread: Qtreeview not displaying data (data() is called)

  1. #1
    Join Date
    Apr 2013
    Posts
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Unhappy Qtreeview not displaying data (data() is called)

    hi

    the QTreeView in my program doesn't display the data (should be only string , so qt::displayrole)
    the data() function is called and return a QString("no data " should be the output )(i tested it before with qdebug so i don't know what's wrong here
    i already used multiple list model with different view on an older version the code was awfull but worked.
    thx
    ps : i can publish more code , the code is still experimental(draft) (64bit),originaly the was qt4.8 but now 5.0
    Qt Code:
    1. #include "CommandModel.h"
    2. #include <new>
    3. #include <QDebug>
    4. //#include <QSize>
    5.  
    6.  
    7. using std::unique_ptr;
    8. using std::bad_alloc;
    9. CommandModel::CommandModel(QObject * parent):mTreeItemRoot(nullptr)
    10. {
    11. setParent(parent);
    12. try
    13. {
    14. mTreeItemRoot = new TreeItem(TreeItem::TypeData::Root,/*QVariantRootItem*/QVariant() );
    15. }
    16. catch (bad_alloc& ba ) { throw; }
    17.  
    18. }
    19.  
    20. CommandModel::~CommandModel()
    21. {
    22. delete mTreeItemRoot;
    23. }
    24.  
    25. int CommandModel::rowCount ( const QModelIndex & parent ) const
    26. {
    27.  
    28. if ( parent.isValid() )
    29. {
    30. TreeItem* ti_parent =static_cast<TreeItem*>(parent.internalPointer() );
    31. return ti_parent->Childcount();
    32. }
    33. return mTreeItemRoot->Childcount();
    34. }
    35.  
    36. int CommandModel::columnCount ( const QModelIndex & parent ) const
    37. {
    38. return 0;
    39. }
    40.  
    41. QVariant CommandModel::data ( const QModelIndex & index, int role) const
    42. {
    43. if(index.isValid() )
    44. {
    45. if (role == Qt::DisplayRole || role == Qt::EditRole )
    46. {
    47. TreeItem* ti_index =static_cast<TreeItem*>(index.internalPointer() );
    48. auto item_type = ti_index->getType();
    49. switch( item_type )
    50. {
    51. case TreeItem::TypeData::GameProfile :
    52. return QVariant(*(ti_index->data().value<GameProfileData*>()->getGameProfileTitle() ) );
    53.  
    54. case TreeItem::TypeData::GroupCommand :
    55. return QVariant(ti_index->data().value<GroupCommand*>()->getGroupCommandName() );
    56.  
    57. case TreeItem::TypeData::VirtualKeyboard :
    58. return QVariant(*(ti_index->data().value<VirtualKeyboard*>()->getVirtualKeyboardName() ) );
    59.  
    60. case TreeItem::TypeData::Command :
    61. return QVariant(*(ti_index->data().value<KeyItemData*>()->getCommandName() ) );
    62.  
    63. case TreeItem::TypeData::MacroKey :
    64. return QVariant(ti_index->data().value<MacroKey*>()->getMacroKeyName() );
    65.  
    66. }
    67. }
    68. else if(role == TypeDataRole)
    69. {
    70. auto ti_index =static_cast<TreeItem*>(index.internalPointer() );
    71. QVariant qvariant_type_data;
    72. qvariant_type_data.setValue(ti_index->getType() );
    73. return qvariant_type_data;
    74. }
    75. }
    76. return QVariant();
    77. }
    78.  
    79. Qt::ItemFlags CommandModel::flags ( const QModelIndex & index ) const
    80. {
    81. if(index.isValid() )
    82. return Qt::ItemIsEnabled | Qt::ItemIsEditable/*| Qt::ItemIsSelectable*/ | QAbstractItemModel::flags(index);
    83. return 0;
    84. }
    85.  
    86. QModelIndex CommandModel::index ( int row, int column, const QModelIndex & parent ) const
    87. {
    88. TreeItem *parentItem;
    89.  
    90. if (parent.isValid() && parent.column() != 0)
    91. return QModelIndex();
    92. if (!parent.isValid())
    93. parentItem = mTreeItemRoot;
    94. else
    95. parentItem = static_cast<TreeItem*>(parent.internalPointer());
    96.  
    97. TreeItem *childItem = parentItem->Child(row);
    98. if (childItem)
    99. return createIndex(row, column, childItem);
    100. else
    101. return QModelIndex();
    102. }
    103.  
    104. bool CommandModel::insertGameProfile()
    105. {
    106. auto row = mTreeItemRoot->Childcount();
    107. if(row <= mTreeItemRoot->Childcount() && row >= 0 )
    108. {
    109. beginInsertRows(QModelIndex(),row,row);
    110. try
    111. {
    112. QVariant GameProfile_Variant_temp;
    113. GameProfile_Variant_temp.setValue( new GameProfileData );
    114. auto tree_item_gameprofile = new TreeItem(TreeItem::TypeData::GameProfile , GameProfile_Variant_temp ,mTreeItemRoot ) ;
    115. mTreeItemRoot->appendChild(tree_item_gameprofile);
    116. endInsertRows();
    117. return true;
    118. }
    119. catch (bad_alloc& ba)
    120. {
    121. endInsertRows();
    122. return false;
    123. }
    124. }
    125. return false;
    126. }
    127.  
    128. bool CommandModel::insertCommandGroup( const QModelIndex & parent )
    129. {
    130.  
    131. if(parent.isValid() )
    132. {
    133. try
    134. {
    135. auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
    136. auto row = parent_temp->Childcount();
    137. beginInsertRows(parent,row,row);
    138. QVariant CommandGroup_variant_temp;
    139. CommandGroup_variant_temp.setValue( new GroupCommand );
    140. auto tree_item_CommandGroup = new TreeItem(TreeItem::TypeData::GroupCommand , CommandGroup_variant_temp ,parent_temp ) ;
    141. parent_temp->appendChild(tree_item_CommandGroup);
    142. endInsertRows();
    143. return true;
    144. }
    145. catch (bad_alloc& ba)
    146. {
    147. endInsertRows();
    148. return false;
    149. }
    150. }
    151. return false;
    152. }
    153.  
    154. bool CommandModel::insertVirtualKeyboard( const QModelIndex & parent )
    155. {
    156. if(parent.isValid() )
    157. {
    158. try
    159. {
    160. auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
    161. auto row = parent_temp->Childcount();
    162. beginInsertRows(parent,row,row);
    163. QVariant VirtualKeyboard_variant_temp;
    164. VirtualKeyboard_variant_temp.setValue( new VirtualKeyboard );
    165. auto tree_item_VirtualKeyboard = new TreeItem(TreeItem::TypeData::VirtualKeyboard , VirtualKeyboard_variant_temp ,parent_temp );
    166. parent_temp->appendChild(tree_item_VirtualKeyboard);
    167. endInsertRows();
    168. return true;
    169. }
    170. catch (bad_alloc& ba)
    171. {
    172. endInsertRows();
    173. return false;
    174. }
    175. }
    176. return false;
    177. }
    178.  
    179. bool CommandModel::insertCommand(const QModelIndex & parent )
    180. {
    181.  
    182. if(parent.isValid() )
    183. {
    184. try
    185. {
    186. auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
    187. auto row = parent_temp->Childcount();
    188. beginInsertRows(parent,row,row);
    189. QVariant Command_variant_temp;
    190. Command_variant_temp.setValue( new KeyItemData );
    191. auto tree_item_Command = new TreeItem(TreeItem::TypeData::Command , Command_variant_temp ,parent_temp );
    192. parent_temp->appendChild(tree_item_Command);
    193. endInsertRows();
    194. return true;
    195. }
    196. catch (bad_alloc& ba)
    197. {
    198. endInsertRows();
    199. return false;
    200. }
    201. }
    202. return false;
    203. }
    204.  
    205. bool CommandModel::insertMacro(const MacroKey& macro , const QModelIndex & parent )
    206. {
    207. if(parent.isValid() )
    208. {
    209. try
    210. {
    211. auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
    212. int row = parent_temp->Childcount();
    213. beginInsertRows(parent,row,row);
    214. QVariant MacroKey_variant_temp;
    215. MacroKey_variant_temp.setValue( new MacroKey(macro) );
    216. auto tree_item_MacroKey = new TreeItem(TreeItem::TypeData::MacroKey , MacroKey_variant_temp ,parent_temp );
    217. parent_temp->appendChild(tree_item_MacroKey);
    218. endInsertRows();
    219. return true;
    220. }
    221. catch (bad_alloc& ba)
    222. {
    223. endInsertRows();
    224. return false;
    225. }
    226. }
    227. return false;
    228. }
    229.  
    230. bool CommandModel::removeRows ( int first, int last, const QModelIndex & parent )
    231. {
    232. if (parent.isValid() )
    233. {
    234. beginRemoveRows (parent, first, last );
    235. auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
    236. auto result = parent_temp->removeChild(first , last);
    237. endRemoveRows ();
    238. return result;
    239. }
    240. return false;
    241. }
    242.  
    243. bool CommandModel::setData ( const QModelIndex & index, const QVariant & value, int role )
    244. {
    245. if (role == Qt::DisplayRole && Qt::EditRole && index.isValid() )
    246. {
    247. auto index_temp = static_cast<TreeItem*>(index.internalPointer() );
    248. index_temp->setData(value);
    249. return true;
    250. }
    251. return false;
    252. }
    253.  
    254. QModelIndex CommandModel::parent ( const QModelIndex & index ) const
    255. {
    256. if ( index.isValid() )
    257. {
    258. auto index_temp = static_cast<TreeItem*>(index.internalPointer() ) ;
    259. auto parent_temp = index_temp->parent();
    260.  
    261. if(parent_temp == mTreeItemRoot)
    262. return QModelIndex();
    263. return createIndex(parent_temp->row(), 0 , parent_temp);
    264. }
    265. return QModelIndex();
    266. }
    267.  
    268. QVariant CommandModel::headerData(int section, Qt::Orientation orientation,int role ) const
    269. {
    270. return QVariant();
    271. }
    272.  
    273. bool CommandModel::hasChildren(const QModelIndex & parent ) const
    274. {
    275. if(parent.isValid() )
    276. {
    277. auto parent_item = static_cast<TreeItem*>( parent.internalPointer() );
    278. if(parent_item->Childcount() )
    279. return true;
    280. }
    281. if(mTreeItemRoot->Childcount())
    282. return true;
    283. return false;
    284. }
    To copy to clipboard, switch view to plain text mode 

    manykey.jpg
    Last edited by mraww; 13th April 2013 at 11:12.

  2. The following user says thank you to mraww for this useful post:


  3. #2
    Join Date
    Apr 2013
    Posts
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Cool Re: Qtreeview not displaying data (data() is called) solved

    i solved the problem

    Qt Code:
    1. int CommandModel::columnCount ( const QModelIndex & parent ) const
    2. {
    3. return 0;
    4. }
    To copy to clipboard, switch view to plain text mode 

    i just needed to change the return from 0 to 1

    Qt Code:
    1. int CommandModel::columnCount ( const QModelIndex & parent ) const
    2. {
    3. return 1;
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to mraww for this useful post:


Similar Threads

  1. Replies: 4
    Last Post: 22nd January 2013, 19:53
  2. displaying data
    By willief in forum Newbie
    Replies: 4
    Last Post: 10th March 2011, 06:33
  3. Process the display data before displaying in QTreeView
    By babu198649 in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2009, 05:21
  4. Replies: 1
    Last Post: 3rd December 2008, 15:51
  5. QAbstractTableModel::data not being called...
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 09:52

Tags for this Thread

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.