开发者

Displaying multiple icons in a single cell of a QTableView

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()).lastSta开发者_开发问答tes());
        }
    }

    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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜