PDA

View Full Version : Custom widgets in a grid view



ada10
23rd August 2010, 05:54
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 ?

tbscope
23rd August 2010, 10:21
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.

ada10
23rd August 2010, 11:28
I have the following model & model data classes defined -

//custommodel.h

class custommodel : public QAbstractListModel
{
Q_OBJECT

public:
custommodel( QObject *parent = 0 );

~custommodel();

QVariant data (const QModelIndex &index, int role) const;

int rowCount (const QModelIndex &parent=QModelIndex()) const;

QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;

Qt::ItemFlags flags(const QModelIndex &index) const;

bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);

QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;

bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );

bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );

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 -


class devicedata: public QObject
{
Q_OBJECT
public:
devicedata();
devicedata( QString aName );
//copy ctor
devicedata( const devicedata& other );

~devicedata();

QString getPhotoName() const;
void setPhotoName( QString aName );

private:
QString mPhotoName;

};

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
devicedata be of type QWidget ? Or should I have another class just for the ui functionality? Should the delegate use this widget class to draw ?