PDA

View Full Version : Display widget as items in a QListView



remy_david
10th January 2011, 11:23
Hello,

I am developing a mobile application with Nokia Qt SDK.
I have a table in a QSqlDatabase that I feed from a rss news feed.
I want to display those news in a list but I am new to Qt so I don't know what is the best solution.

I guess I need a QListView and set its model to a QSqlTableModel bind to my QSqlDatabase table.

But then I don't know how to display items (rows) in a custom way i.e. using widgets.
I know the QItemDelegate is there for that but how can I draw my custom widget build from data in QModelIndex into the painter object of the paint method :



MyItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
//Retrieve data from the model (a map with the news elements)
QVariantMap data = index.model()->data(index, Qt::DisplayRole);

//Construct a custom widget from data in the map
QWidget *item = new Widget;
QLabel *title = new QLabel(item);
title->setText(data.value("title").toString());

//.... add more elements to the widget from the data ....

// now paint the item widget in the painter but how ???
}


Thanks for the help