PDA

View Full Version : QListView won't show up



rishid
20th January 2008, 04:35
Hello,

I am trying to use the mode/view approach with a QListView. Problem is the model and data will not show up in the QListView. If I run the code as it attached in this post here: http://www.qtcentre.org/forum/p-drawing-a-widget-in-qitemdelegates-paint-method-post46300/postcount5.html
it runs just fine and the QListView pops up. I also tried to just put the code in my int main() method and then it shows up, it just won't show up when the QListView is added in QtDesigner which is inside my widget. This a limitation of QListView?

I added a QListView object via QtDesigner but am not having any luck getting the data to show up. Here is my code (most of it is from the example linked above):



QStandardItemModel model;
model.setRowCount(2);
model.setColumnCount(1);
model.setData(model.index(0, 0), QPixmap("/usr/share/icons/crystalsvg/32x32/actions/filenew.png"), Qt::DecorationRole);
model.setData(model.index(0, 0), "Some wonderful text which is long enough to cover more than one row");
model.setData(model.index(1, 0), QPixmap("/usr/share/icons/crystalsvg/32x32/actions/exit.png"), Qt::DecorationRole);
model.setData(model.index(1, 0), "Some wonderful text");
ui.listView->setModel(&model);
ui.listView->setItemDelegate(new PluginDelegate(ui.listView));
ui.listView->setAlternatingRowColors(true);


PluginDelegate (word for word from other post)

class PluginDelegate : public QAbstractItemDelegate {
public:
PluginDelegate(QObject *parent=0) : QAbstractItemDelegate(parent){}
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
if(option.state & QStyle::State_Selected){
painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
}
QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
QString txt = index.data(Qt::DisplayRole).toString();
QRect r = option.rect.adjusted(2, 2, -2, -2);
ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
r = r.adjusted(r.height()+20, 0, 0, 0);
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, txt, &r);
}
QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{
return QSize(200, 52); // very dumb value
}

};

And my listView that is generated by uic


QListView *listView;
// ..snip
tab_3 = new QWidget();
tab_3->setObjectName(QString::fromUtf8("tab_3"));
listView = new QListView(tab_3);
listView->setObjectName(QString::fromUtf8("listView"));
listView->setGeometry(QRect(0, 0, 311, 151));

jpn
20th January 2008, 08:55
The model goes out of scope and gets destructed like any local (stack) object in C++. Allocated it on the heap instead (with keyword "new") so that it will remain alive:


QStandardItemModel* model = new QStandardItemModel(parent);