tags:

views:

501

answers:

1

I am writing a small gui app with QT4.5 in QtCreator.

The main screen on the app contains a QTreeView with two columns, the first is text the second is a group of icons. These icons represent the last few states of the item displayed in the row.

I am not sure what the best way to do this is. I have currently implemented this by generating a QPixmap the model's data() method.

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole || role == Qt::EditRole) {
        switch(index.column()) {
            case 0:
                return item_.at(index.row()).title();
        }
    }
    if (role == Qt::DecorationRole) {
        switch(index.column()) {
            case 1:
                return makeImage(item_.add(index.row()).lastStates());
        }
    }

    return QVariant();
}

QVariant MyModel::makeImage(const QList<MyState> &states) const
{
    const int IconSize = 22;
    QPixmap image(IconSize * states.size(), IconSize);
    QPainter painter(&image);

    painter.fillRect(0, 0, IconSize * count, IconSize, Qt::transparent);
    for (int i = 0; i < states.size(); ++i) {
        QIcon * icon = stateIcon(state.at(i));
        icon->paint(&painter, IconSize * i, 0, IconSize, IconSize);
    }
    return image;
}

This works but for some small problems, the background which should be transparent is full of random noise, even filling this with a transparent colour does not fix it.

Second this does not seem very efficient, I am generating a new Image every time this is called, should I not just draw the icons onto the widget for the cell?

What is the best way to display multiple icons in a single cell?

+5  A: 

I would create a custom delegate, based on a hbox, into which you can place all the pictures. Have a look at delegates in the Qt Documentation about model view programming.

Marius
Thanks that worked a treat. The only problem now is how to get the new column to be selected correctly. I can get the selection background colour to display but it looks flat where as the other columns are rounded in 3D. If I have no luck I will add ask another question
iain