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 ?
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.
Re: Custom widgets in a grid view
I have the following model & model data classes defined -
//custommodel.h
Code:
{
Q_OBJECT
public:
custommodel
( QObject *parent
= 0 );
~custommodel();
QVariant headerData
( int section, Qt
::Orientation orientation,
int role
= Qt
::DisplayRole ) const;
void addDeviceInfo( devicedata* aDeviceInfo );
void removeDeviceInfo( devicedata* aDeviceInfo );
void addDeviceInfoList( QList<devicedata*>& aInfoList );
void PrintDeviceNames();
private:
Q_DISABLE_COPY(custommodel)
QList<devicedata*> mNamesList;
};
//model data class -
Code:
{
Q_OBJECT
public:
devicedata();
//copy ctor
devicedata( const devicedata& other );
~devicedata();
void setPhotoName
( QString aName
);
private:
};
Q_DECLARE_METATYPE( devicedata )
For every instance of devicedata added to model, I need to have a custom widget having an image & some text below it. Should be of type QWidget ? Or should I have another class just for the ui functionality? Should the delegate use this widget class to draw ?