Results 1 to 3 of 3

Thread: Custom widgets in a grid view

  1. #1
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Post Custom widgets in a grid view

    I have a requirement of displaying custom widgets in a grdview. These items are selectable & have an image being displayed with some text below it. I assume Model-View must be used, but should I also use a custom delegate to handle the painting of the widgets ? Of what type should the widget be, if it has to be paint-able by the delegate ?
    Last edited by ada10; 23rd August 2010 at 06:10.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Custom widgets in a grid view

    There are a couple of ways to implement this. It all depends on how you want to use the grid.

    For widgets to be painted, they need to be a subclass of QWidget. You implement the paint event to do the actual painting.
    But, you do not have to add widgets. You can paint an svg, or bmp and use events to handle mouse clicks etc... But that will be more work than just subclassing a QWidget I guess.

    My opinion:

    One way is to use model/view:
    - Advantages: table based models and views are readily available.
    - Disadvantages: might be too complex for a basic grid. I guess you only need to write a delegate to paint the widgets in each cell. Maybe a simple custom table model.

    Another way is to use QGraphicsView:
    - Advantages: easier to include widgets
    - Disadvantages: might take more work to handle the grid widgets.

    A third way is to use layouts:
    - Advantages: easy to include widgets
    - Disadvantages: might take more work to handle the grid widgets.

  3. #3
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Post Re: Custom widgets in a grid view

    I have the following model & model data classes defined -

    //custommodel.h
    Qt Code:
    1. class custommodel : public QAbstractListModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. custommodel( QObject *parent = 0 );
    7.  
    8. ~custommodel();
    9.  
    10. QVariant data (const QModelIndex &index, int role) const;
    11.  
    12. int rowCount (const QModelIndex &parent=QModelIndex()) const;
    13.  
    14. QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
    15.  
    16. Qt::ItemFlags flags(const QModelIndex &index) const;
    17.  
    18. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
    19.  
    20. QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
    21.  
    22. bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
    23.  
    24. bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
    25.  
    26. void addDeviceInfo( devicedata* aDeviceInfo );
    27.  
    28. void removeDeviceInfo( devicedata* aDeviceInfo );
    29.  
    30. void addDeviceInfoList( QList<devicedata*>& aInfoList );
    31.  
    32. void PrintDeviceNames();
    33.  
    34. private:
    35.  
    36. Q_DISABLE_COPY(custommodel)
    37. QList<devicedata*> mNamesList;
    38. };
    To copy to clipboard, switch view to plain text mode 

    //model data class -

    Qt Code:
    1. class devicedata: public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. devicedata();
    6. devicedata( QString aName );
    7. //copy ctor
    8. devicedata( const devicedata& other );
    9.  
    10. ~devicedata();
    11.  
    12. QString getPhotoName() const;
    13. void setPhotoName( QString aName );
    14.  
    15. private:
    16. QString mPhotoName;
    17.  
    18. };
    19.  
    20. Q_DECLARE_METATYPE( devicedata )
    To copy to clipboard, switch view to plain text mode 


    For every instance of devicedata added to model, I need to have a custom widget having an image & some text below it. Should
    Qt Code:
    1. devicedata
    To copy to clipboard, switch view to plain text mode 
    be of type QWidget ? Or should I have another class just for the ui functionality? Should the delegate use this widget class to draw ?

Similar Threads

  1. Replies: 8
    Last Post: 5th May 2021, 16:41
  2. Cant't draw custom grid
    By Tottish in forum Qwt
    Replies: 4
    Last Post: 1st May 2010, 09:55
  3. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 20:23
  4. Replies: 5
    Last Post: 21st January 2010, 15:55
  5. slider control and combo box in grid view
    By steg90 in forum Qt Programming
    Replies: 13
    Last Post: 21st November 2007, 10:45

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.