Results 1 to 7 of 7

Thread: Reimplemented QStandardItem

  1. #1
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Reimplemented QStandardItem

    Hello!
    I've got QTreeView and QStandardItemModel. There are items in this model based on QStandardItem:
    Qt Code:
    1. class TComplexItem : public QStandardItem
    2. {
    3. public:
    4.  
    5. const TElement *getElement() const;
    6. void setElementData(const TElement & element);
    7.  
    8. private:
    9. TElement *m_pElement; //string and integer members in class TElement
    10. }
    To copy to clipboard, switch view to plain text mode 
    It's now necessary to add to the window with QTreeView some controls for representation data from m_pElement. So when we walk though the tree we should get the appropriate data for current item in these controls.
    How could I do this? It looks like QDataWidgetMapper. But QDataWidgetMapper itself doesn't fit here.

    Thanks in advance for any ideas.

  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: Reimplemented QStandardItem

    What's wrong with QDataWidgetMapper?
    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. #3
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reimplemented QStandardItem

    How could I use QDataWidgetMapper in this situation?
    Class TElement contains some members. And all of them belong to the same section of the model.

    From the description of function QDataWidgetMapper::addMapping: "Only one-to-one mappings between sections and widgets are allowed. It is not possible to map a single section to multiple widgets..."
    So, I can't use the construction like:
    Qt Code:
    1. mapper->addMapping(nameLineEdit, 0);
    To copy to clipboard, switch view to plain text mode 
    Maybe, there are some other ways?


    Added after 7 minutes:


    Is it possible to use one QWidget as a container for all these QLineEdits and other controls?
    Then
    Qt Code:
    1. mapper->addMapping(MyWidget, 0);
    To copy to clipboard, switch view to plain text mode 
    But I have no idea how to put the fields to the appropriate controls (f.e. m_pElement->Name to MyWidget->leName).
    appMapping is not reimplemented.
    Last edited by Miga; 3rd April 2012 at 14:09.

  4. #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: Reimplemented QStandardItem

    I don't see why you'd want to map a single section to multiple widgets. As I understand TElement is some kind of structure and you want to show each field in this structure in a separate widget thus you have a perfect one to one mapping. You just have to implement the model properly which is, I guess, not what you are currently doing.
    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.


  5. #5
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reimplemented QStandardItem

    Quote Originally Posted by wysota View Post
    As I understand TElement is some kind of structure and you want to show each field in this structure in a separate widget thus you have a perfect one to one mapping.
    Thanks for your answers. Yes, you are quite right! But I can't catch how to do that.
    Quote Originally Posted by wysota View Post
    You just have to implement the model properly which is, I guess, not what you are currently doing.
    Sorry, I'm not quite understand. Could you say what should I reimplement in the model? I use simple QStandardItemModel now. Or you mean something other?

  6. #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: Reimplemented QStandardItem

    The problem is not with QStandardItemModel but rather with your getElement() and setElementData() methods.

    Somewhere in your code you are probably fetching standard items from the model, casting them to TComplexItem pointers and using getElement() and setElementData() to access your custom data, right? This is a bad approach, you should only be using QAbstractItemModel API for accessing the data. Since you didn't reimplement QStandardItem::data() I'm guessing you can't access fields from TElement using QAbstractItemModel::data().

    I don't know what your TElement class holds, but your model should probably look like this:

    Qt Code:
    1. class TElementModel : public QAbstractTableModel {
    2. public:
    3. // ...
    4.  
    5. QVariant data(const QModelIndex &index, int role == Qt::DisplayRole) const {
    6. // check if index is valid
    7. // ...
    8. const TElement &item = m_data.at(index.row());
    9. if(role == Qt::DisplayRole || role == Qt::EditRole) {
    10. switch(index.column()) {
    11. case 0: return item.column1;
    12. case 1: return item.column2;
    13. // etc.
    14. }
    15. }
    16. }
    17. private:
    18. QList<TElement> m_data;
    19. };
    To copy to clipboard, switch view to plain text mode 
    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.


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

    Miga (3rd April 2012)

  8. #7
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reimplemented QStandardItem

    Quote Originally Posted by wysota View Post
    Somewhere in your code you are probably fetching standard items from the model, casting them to TComplexItem pointers and using getElement() and setElementData() to access your custom data, right?
    Yes, you are rihgt.
    Quote Originally Posted by wysota View Post
    ... you should only be using QAbstractItemModel API for accessing the data. Since you didn't reimplement QStandardItem::data() I'm guessing you can't access fields from TElement using QAbstractItemModel::data().
    Thank you very much for your answers. I'll try to do so.

Similar Threads

  1. Can't paint in reimplemented QHeaderView
    By Tottish in forum Newbie
    Replies: 5
    Last Post: 21st March 2011, 08:56
  2. Tree without using of QStandardItem
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 12th July 2010, 12:23
  3. setObjectName for QStandardItem
    By abk883 in forum Newbie
    Replies: 1
    Last Post: 20th May 2010, 18:20
  4. Replies: 2
    Last Post: 4th December 2009, 06:15
  5. QGraphicsItem reimplemented mouse events
    By aarelovich in forum Qt Programming
    Replies: 12
    Last Post: 24th July 2009, 12:56

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.