Results 1 to 2 of 2

Thread: QModelIndex Mapping in Template QWidget Class

  1. #1
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question QModelIndex Mapping in Template QWidget Class

    I have created a template class that inherits from QWidget and aggregates a sortable QTableView and a QSortFilterProxyModel. I want to be able to react to a double click in the QTableView and pass the QModelIndex to my custom model, which has the slot void DoubleClicked(const QModelIndex&) defined on it.

    This is a slimmed down version of the template class:

    Qt Code:
    1. template<typename MODEL>
    2. class TTableViewWidget : public QWidget
    3. {
    4. public:
    5. TTableViewWidget(MODEL& m, QWidget* parent = 0) :
    6. QWidget(parent),
    7. model(m)
    8. {
    9. sortProxy.reset(new QSortFilterProxyModel(this));
    10. sortProxy->setSourceModel(&model);
    11.  
    12. SetupUI();
    13.  
    14. connect(tableView.get(), SIGNAL(doubleClicked(const QModelIndex&)),
    15. &model, SLOT(DoubleClicked(const QModelIndex&)));
    16. }
    17. ~TTableViewWidget() {}
    18.  
    19. private:
    20. void SetupUI() {
    21. tableView.reset(new QTableView(this));
    22. tableView->setSortingEnabled(true);
    23. tableView->setModel(sortProxy.get());
    24.  
    25. verticalLayout.reset(new QVBoxLayout(this));
    26. verticalLayout->addWidget(tableView.get());
    27. }
    28.  
    29. private:
    30. std::auto_ptr<QVBoxLayout> verticalLayout;
    31. std::auto_ptr<QTableView> tableView;
    32. std::auto_ptr<QSortFilterProxyModel> sortProxy;
    33.  
    34. private:
    35. MODEL& model;
    36.  
    37. };
    To copy to clipboard, switch view to plain text mode 

    The problem is that a QTableView::doubleClicked signal returns a QModelIndex that needs to be mapped using the QSortFilterProxyModel. Q_OBJECT does not handle template classes, so I'm not able to define signals or slots in my template class, otherwise I could simply do the mapping in a slot on the template class.

    One idea I had was the subclass a QSortFilterProxyModel and provide a DoubleClicked slot that would map and call into its source model. Does anyone else have any clever ideas?
    Last edited by stefanadelbert; 1st March 2010 at 07:11.

  2. #2
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QModelIndex Mapping in Template QWidget Class

    It seems there are answers out there. For example, http://doc.trolltech.com/qq/qq15-aca...ingtotemplates,

    If the signals and slots don't require the template parameter to be part of the prototype, the workaround is to make a template class inherit a QObject subclass that provides the required signals and slots:

Similar Threads

  1. Template of ingredient class
    By Trok in forum Qt Programming
    Replies: 2
    Last Post: 21st August 2009, 12:27
  2. Use QWidget derived class in Dialog
    By qtneuling in forum Qt Tools
    Replies: 2
    Last Post: 17th May 2008, 23:29
  3. Replies: 1
    Last Post: 15th April 2008, 23:15
  4. Template class
    By steg90 in forum General Programming
    Replies: 15
    Last Post: 12th June 2007, 20:49
  5. template parameter and conversion operator
    By bitChanger in forum General Programming
    Replies: 7
    Last Post: 21st April 2006, 15:36

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.