Using a QStyledItemDelegate on a QListView with QS

2019-01-23 17:10发布

问题:

I have a QListView, that has a QSqlQueryModel set as its model. How can I use a QStyledItemDelegate in order to customise the QListView's rows' appearence (e.g. show 2 text lines) ?

QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName( "test.db" );
if( !db.open() )
{
    qDebug() << db.lastError();
    qFatal( "Failed to connect." );
}

qDebug( "Connected!" );

QSqlQueryModel *sqlModel = new QSqlQueryModel;
sqlModel->setQuery("SELECT * FROM entries");

mListWidget->setModel(sqlModel);

Essentially, what I think I need to do is to somehow "match" roles to db table's fields, in order to be able to get the data from the QStyledItemDelegate, using something like this:

void ListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    [...]
    QString headerText = qvariant_cast<QString>(index.data(headerRole));
    QString subText = qvariant_cast<QString>(index.data(subHeaderRole));
    [...]
}

Thanks!

回答1:

You certainly can use QStyledItemDelegate for custom item drawing. QModelIndex has a reference to the model object which you can use to get "entries" record fields. You also have to redefine model's sizeHint method to increase items size if you need to show more data then a single data. Other then that it's more or less trivial.

Pls, see if an example below would work you:

class ListViewDelegate : public QStyledItemDelegate
{
protected:
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        QStyleOptionViewItemV4 opt = option;
        initStyleOption(&opt, index);

        QString line0 = index.model()->data(index.model()->index(index.row(), 1)).toString();
        QString line1 = index.model()->data(index.model()->index(index.row(), 2)).toString();

        // draw correct background
        opt.text = "";
        QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);

        QRect rect = opt.rect;
        QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
        if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
            cg = QPalette::Inactive;

        // set pen color
        if (opt.state & QStyle::State_Selected)
            painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
        else
            painter->setPen(opt.palette.color(cg, QPalette::Text));

        // draw 2 lines of text
        painter->drawText(QRect(rect.left(), rect.top(), rect.width(), rect.height()/2),
                          opt.displayAlignment, line0);
        painter->drawText(QRect(rect.left(), rect.top()+rect.height()/2, rect.width(), rect.height()/2),
                          opt.displayAlignment, line1);
    }

    QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
        QSize result = QStyledItemDelegate::sizeHint(option, index);
        result.setHeight(result.height()*2);
        return result;
    }
};

The test database set is defined here:

QSqlError initDb()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(":memory:");

    if (!db.open())
        return db.lastError();

    QStringList tables = db.tables();
    if (tables.contains("test", Qt::CaseInsensitive))
        return QSqlError();

    QSqlQuery q;
    if (!q.exec(QLatin1String("create table entries(id integer primary key, first_line varchar, second_line varchar)")))
        return q.lastError();

    q.exec("insert into entries(id, first_line, second_line) values(0, 'first line 0', 'second line 0')");
    q.exec("insert into entries(id, first_line, second_line) values(1, 'first line 1', 'second line 1')");
    q.exec("insert into entries(id, first_line, second_line) values(2, 'first line 2', 'second line 2')");

    return QSqlError();
}

model and listview definition:

initDb();

QSqlQueryModel *sqlModel = new QSqlQueryModel();
sqlModel->setQuery("SELECT * FROM entries");

ui->listView->setModel(sqlModel);
ui->listView->setItemDelegate(new ListViewDelegate());

hope this helps, regards



标签: qt qt4 symbian