Results 1 to 8 of 8

Thread: QAbstractProxyModel , mapToSource() and mapFromSource()

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default QAbstractProxyModel , mapToSource() and mapFromSource()

    Hi forum,


    I would like to browse through an example where QAbstractProxyModel is sub-classed and the above mentioned functions are over-ridden.

    If any one aware of any open source examples , please refer them to me.


    I really like to have some idea of this concept.


    Regards
    Sajjad

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAbstractProxyModel , mapToSource() and mapFromSource()

    Here is one: [wiki]Transpose proxy model[/wiki]

    There are others in Qt docs too. Some more are available on this forum in different threads.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    d_stranz (27th March 2012)

  4. #3
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractProxyModel , mapToSource() and mapFromSource()

    Hi forum,

    The transpose roxy model example mantains the information hiding while the other example at Qt site didi not do that at all. They customized a tree view with proxy model and while doing that they have used many global variables as follows:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. QList<QStandardItem *> list;
    4.  
    5. class SortProxy : public QAbstractProxyModel
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. SortProxy(QObject *parent = 0) : QAbstractProxyModel(parent), hideThem(false)
    11. {
    12. fixModel();
    13. }
    14.  
    15. int rowCount(const QModelIndex &parent) const
    16. {
    17. QModelIndex sourceParent;
    18. if (parent.isValid())
    19. sourceParent = mapToSource(parent);
    20. int count = 0;
    21. QMapIterator<QPersistentModelIndex, QPersistentModelIndex> it(proxySourceParent);
    22. while (it.hasNext()) {
    23. it.next();
    24. if (it.value() == sourceParent)
    25. count++;
    26. }
    27. return count;
    28. }
    29.  
    30. int columnCount(const QModelIndex &) const
    31. {
    32. return 1;
    33. }
    34.  
    35. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const
    36. {
    37. QModelIndex sourceParent;
    38. if (parent.isValid())
    39. sourceParent = mapToSource(parent);
    40. QMapIterator<QPersistentModelIndex, QPersistentModelIndex> it(proxySourceParent);
    41. while (it.hasNext()) {
    42. it.next();
    43. if (it.value() == sourceParent && it.key().row() == row &&
    44. it.key().column() == column)
    45. return it.key();
    46. }
    47. return QModelIndex();
    48. }
    49.  
    50. QModelIndex parent(const QModelIndex &child) const
    51. {
    52. QModelIndex mi = proxySourceParent.value(child);
    53. if (mi.isValid())
    54. return mapFromSource(mi);
    55. return QModelIndex();
    56. }
    57.  
    58. QModelIndex mapToSource(const QModelIndex &proxyIndex) const
    59. {
    60. if (!proxyIndex.isValid())
    61. return QModelIndex();
    62. return mapping.key(proxyIndex);
    63. }
    64.  
    65. QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
    66. {
    67. if (!sourceIndex.isValid())
    68. return QModelIndex();
    69. return mapping.value(sourceIndex);
    70. }
    71.  
    72. public slots:
    73. void hideEverythingButA1AndChildren()
    74. {
    75. hideThem = !hideThem;
    76. // Now we set up the proxy <-> source mappings
    77. emit layoutAboutToBeChanged();
    78. fixModel();
    79. emit layoutChanged();
    80. }
    81.  
    82. private:
    83. void fixModel()
    84. {
    85. mapping.clear();
    86. proxySourceParent.clear();
    87.  
    88. //the following list is already populated
    89. //in the tree view and we shall browse and
    90. //customize it here
    91. for (int i=0;i<list.size();i++)
    92. {
    93. //pull out a standard item
    94. QStandardItem *si = list.at(i);
    95.  
    96. if (hideThem)
    97. {
    98. //if the hide item flag is true
    99.  
    100. //check if the standard item's text start with 'A'
    101. //of the item is not parent
    102. if (!si->text().startsWith("A") || !si->parent())
    103. continue;
    104.  
    105.  
    106. //means that we have encountered item that does not start with 'A'
    107. //or the item is not the parent
    108.  
    109. //we pull out the model index by creating the model index with the source items
    110. //row , column
    111. QModelIndex proxy = createIndex(si->row(), si->column(), si->index().internalPointer());
    112.  
    113. //insert the source model's index and the proxy index into the map
    114. mapping.insert(QPersistentModelIndex(si->index()), proxy);
    115. QModelIndex sourceParent;
    116.  
    117. if (si->parent()->parent())
    118. sourceParent = si->parent()->index();
    119. proxySourceParent.insert(proxy, sourceParent);
    120. }
    121. else
    122. {
    123. QModelIndex proxy = createIndex(si->row(), si->column(), si->index().internalPointer());
    124. mapping.insert(QPersistentModelIndex(si->index()), proxy);
    125. QModelIndex sourceParent;
    126. if (si->parent())
    127. sourceParent = si->parent()->index();
    128. proxySourceParent.insert(proxy, sourceParent);
    129. }
    130. }
    131. }
    132.  
    133. QMap<QPersistentModelIndex, QPersistentModelIndex> mapping;
    134. QMap<QPersistentModelIndex, QPersistentModelIndex> proxySourceParent;
    135. bool hideThem;
    136. };
    137.  
    138. SortProxy *proxyModel = 0;
    139.  
    140. class Tree : public QTreeView
    141. {
    142. Q_OBJECT
    143.  
    144. public:
    145. Tree(QWidget *parent = 0) : QTreeView(parent)
    146. {
    147. QStandardItemModel *sourceModel = new QStandardItemModel(this);
    148.  
    149. QStandardItem *parentA = sourceModel->invisibleRootItem();
    150. for (int i = 0; i < 2; ++i) {
    151. itemA = new QStandardItem(QString("A %0").arg(i));
    152. parentA->appendRow(itemA);
    153. parentA = itemA;
    154. list.append(itemA);
    155. }
    156. itemA = new QStandardItem(QString("A 2"));
    157. parentA->appendRow(itemA);
    158. list.append(itemA);
    159. itemA3 = new QStandardItem(QString("A 3"));
    160. list.append(itemA3);
    161. parentA->appendRow(itemA3);
    162. itemA4 = new QStandardItem(QString("A 4"));
    163. list.append(itemA4);
    164. parentA->appendRow(itemA4);
    165. itemNonA = new QStandardItem(QString("Non A"));
    166. list.append(itemNonA);
    167. parentA->appendRow(itemNonA);
    168.  
    169. QStandardItem *parentB = sourceModel->invisibleRootItem();
    170. for (int i = 0; i < 3; ++i) {
    171. itemB = new QStandardItem(QString("B %0").arg(i));
    172. parentB->appendRow(itemB);
    173. parentB = itemB;
    174. list.append(itemB);
    175. }
    176.  
    177. QStandardItem *parentC = sourceModel->invisibleRootItem();
    178. for (int i = 0; i < 3; ++i) {
    179. itemC = new QStandardItem(QString("C %0").arg(i));
    180. parentC->appendRow(itemC);
    181. parentC = itemC;
    182. list.append(itemC);
    183.  
    184. }
    185.  
    186. proxyModel = new SortProxy(this);
    187. proxyModel->setSourceModel(sourceModel);
    188. setModel(proxyModel);
    189. expandAll();
    190. }
    191. QStandardItem *itemA;
    192. QStandardItem *itemA3;
    193. QStandardItem *itemA4;
    194. QStandardItem *itemNonA;
    195. QStandardItem *itemB;
    196. QStandardItem *itemC;
    197. };
    198.  
    199.  
    200. #include "main.moc"
    201.  
    202. int main(int argc, char **argv)
    203. {
    204. QApplication app(argc, argv);
    205. QWidget widget;
    206. QPushButton *button = new QPushButton("Make only A1 + 'A' children visible", &widget);
    207. Tree *tree = new Tree(&widget);
    208. QVBoxLayout *lay = new QVBoxLayout(&widget);
    209. lay->addWidget(button);
    210. QObject::connect(button, SIGNAL(clicked()), proxyModel, SLOT(hideEverythingButA1AndChildren()));
    211. lay->addWidget(tree);
    212. widget.show();
    213. return app.exec();
    214. }
    To copy to clipboard, switch view to plain text mode 


    How do i access to those data if i want to remove the global variables that they have declared. In my own project i do not have any global variable that let the proxy model and main source model to interact with.


    Need your suggestion.


    Regards
    Sajjad

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAbstractProxyModel , mapToSource() and mapFromSource()

    The proxy has access to the source model data using QAbstractProxyModel::sourceModel(). There is no need for any global variables and I can't see any relevant global variables in the code you posted.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractProxyModel , mapToSource() and mapFromSource()

    At the beginning of the code snippet :

    Qt Code:
    1. QList<QStandardItem *> list;
    To copy to clipboard, switch view to plain text mode 

    Both the view and the model is accessing this , the source model is populating it and the proxy model is customizing it. Wouldnt you consider is global. I should be residing only inside the Source Model's scope, isnt it ?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAbstractProxyModel , mapToSource() and mapFromSource()

    Quote Originally Posted by sajis997 View Post
    At the beginning of the code snippet :

    Qt Code:
    1. QList<QStandardItem *> list;
    To copy to clipboard, switch view to plain text mode 

    Both the view and the model is accessing this ,
    Not really. The view indeed is accessing it however creating the model in the view's constructor is a silly approach by itself and besides doing that the view doesn't access the variable anymore. If you move model creation outside the view class and discard the global variable in favour of a local variable, the program will work exactly the same way. I'm not commenting bad C++ practices here

    the source model is populating it and the proxy model is customizing it.
    The proxy doesn't access the "list" variable anywhere.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractProxyModel , mapToSource() and mapFromSource()

    SortProxy does access the list variable inside the private function called fixModel()

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAbstractProxyModel , mapToSource() and mapFromSource()

    Ahh... indeed it does. In that case don't look at this code anymore since it's inherently broken. Find a better example
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QAbstractProxyModel MapToSource Problem
    By tuedl in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2012, 15:34
  2. Replies: 0
    Last Post: 8th December 2011, 01:00
  3. Help with ::mapFromSource (Proxy Model Question)
    By SSurgnier in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2011, 18:02
  4. Replies: 0
    Last Post: 21st June 2011, 18:27
  5. QAbstractProxyModel::mapToSource performance issues
    By maximAL in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 22:48

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.