Results 1 to 2 of 2

Thread: QListView won't show up

  1. #1
    Join Date
    Jan 2008
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8

    Default QListView won't show up

    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-draw...ostcount5.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):

    Qt Code:
    1. model.setRowCount(2);
    2. model.setColumnCount(1);
    3. model.setData(model.index(0, 0), QPixmap("/usr/share/icons/crystalsvg/32x32/actions/filenew.png"), Qt::DecorationRole);
    4. model.setData(model.index(0, 0), "Some wonderful text which is long enough to cover more than one row");
    5. model.setData(model.index(1, 0), QPixmap("/usr/share/icons/crystalsvg/32x32/actions/exit.png"), Qt::DecorationRole);
    6. model.setData(model.index(1, 0), "Some wonderful text");
    7. ui.listView->setModel(&model);
    8. ui.listView->setItemDelegate(new PluginDelegate(ui.listView));
    9. ui.listView->setAlternatingRowColors(true);
    To copy to clipboard, switch view to plain text mode 

    PluginDelegate (word for word from other post)
    Qt Code:
    1. class PluginDelegate : public QAbstractItemDelegate {
    2. public:
    3. PluginDelegate(QObject *parent=0) : QAbstractItemDelegate(parent){}
    4. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
    5. if(option.state & QStyle::State_Selected){
    6. painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
    7. }
    8. QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    9. QString txt = index.data(Qt::DisplayRole).toString();
    10. QRect r = option.rect.adjusted(2, 2, -2, -2);
    11. ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
    12. r = r.adjusted(r.height()+20, 0, 0, 0);
    13. painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, txt, &r);
    14. }
    15. QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{
    16. return QSize(200, 52); // very dumb value
    17. }
    18.  
    19. };
    To copy to clipboard, switch view to plain text mode 

    And my listView that is generated by uic
    Qt Code:
    1. QListView *listView;
    2. // ..snip
    3. tab_3 = new QWidget();
    4. tab_3->setObjectName(QString::fromUtf8("tab_3"));
    5. listView = new QListView(tab_3);
    6. listView->setObjectName(QString::fromUtf8("listView"));
    7. listView->setGeometry(QRect(0, 0, 311, 151));
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: QListView won't show up

    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:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    rishid (20th January 2008)

Similar Threads

  1. Problems customizing tooltips in QListView
    By kalos80 in forum Qt Programming
    Replies: 6
    Last Post: 14th November 2007, 23:46
  2. QListView word wrap
    By serega in forum Qt Programming
    Replies: 17
    Last Post: 30th August 2007, 04:13
  3. QDialog / QListView problem
    By harakiri in forum Qt Programming
    Replies: 1
    Last Post: 10th July 2007, 19:31
  4. Subclass QListView to show two colums in one
    By Mookie in forum Qt Programming
    Replies: 2
    Last Post: 23rd June 2007, 03:12
  5. .gif and .tiff image type in QListView in QT3.3.5
    By darpan in forum Qt Programming
    Replies: 5
    Last Post: 18th March 2006, 05:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.