Results 1 to 6 of 6

Thread: QListView and delegate to display data

  1. #1
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QListView and delegate to display data

    First - i've made ListView with some names.
    Then I wish that every time user selects name - this position expands to my widget with more data (from other structure - name would be key - or maybe you know better way?).
    As I see in QItemDelegate - it should work to display and edit data. Theres a place where widget for editing is created - but there's no place where to create widget for display. At last i see nowhere.
    Other approach - connect my self to clicked() signal and then set my widget to appropriate cell.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListView and delegate to display data

    I suggest using QListWidget, and subclassing QItemDelegate and QListWidgetItem.

    In the subclass of QListWidgetItem you add roles for the info you wish to draw and override
    QVariant data( int role ) const;

    In the subclass of QItemDelegate you must only override paint(...) in which you paint the item, and sizeHint() where you return the sizeHint of you item.

    It is more flexible this way.

    regards

  3. #3
    Join Date
    May 2007
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QListView and delegate to display data

    Hi

    while using QListView, custom widgets are displayed as an QListView item while editing only. ie when u double click item to edit its content (see spinboxdelegate example in Qt manual).

    To display data as per ur need in as QListView items, use model and delegates both.
    Use delegate paint() method to paint items in QListView as you needed.

    U have to use QModelIndex for this.
    give it a try, it will definetely work.

    Regards
    Gaurav Agarwal

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

    Default Re: QListView and delegate to display data

    I'd simply adjust the edit triggers of the view. Either QAbstractItemView::CurrentChanged or QAbstractItemView::SelectedClicked, however you want it to be. Then, all you need to do is to provide a bigger size hint and a custom widget as the editor.
    J-P Nurmi

  5. #5
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QListView and delegate to display data

    Custom widgets are drawn correctly but, don't know why, place for each line isn't expanding. Which means that my widget is drawn over 3 string lines.
    I've added sizeHint appropriate for my widget, but edited line isn't expanding. What should I do to make it look correct?
    Attached Images Attached Images

  6. #6
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListView and delegate to display data

    To make editor-widget proper size we need to set SizeHintRole in index for which we create this editor.
    But this has to be done in delegate createEditor method - not in setEditorData. SetEditorData is being called every time model changes.
    To make sure that editor-widget overwrites background we need to setAutoFillBackground(true) for him.

    Problem lies also - where to put SizeHintRole reset for removed editor?
    In my opinion - in onCloseEditor slot connected with closeEditor signal from QAbstractItemDelegate looks promising.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.