Results 1 to 20 of 35

Thread: How to simplify a complex item delegate?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to simplify a complex item delegate?

    Quote Originally Posted by Adam Badura View Post
    I need to know the size for QStyledItemDelegate::sizeHint as well...
    Yes, but only the height and width, not every exact position of the elements. Also I guess the count of labels is known and static, so you can cache that information. So it would be easy:
    Qt Code:
    1. int countOfLabels = 3; // or whatever
    2. int h = 0;
    3. h = countOfLabels * (fontmetrics.height() + padding)
    4. h = qMax(h, heightOfThumbnail);
    To copy to clipboard, switch view to plain text mode 

    Also I considered using columns.
    Also that doesn't change much of my example:
    Qt Code:
    1. QList<QPair<QString,QString> > labels; // a pair has "label:value"
    2. // Draw the pixmap
    3. int currentX = pixmap.width() + padding /* +... */;
    4.  
    5. // then drawing
    6. int height = fontmetrics.height() + padding;
    7. for(int i = /*...*/) {
    8. painter->drawText(QRect(currentX, i*height, maxDescWidth, height), Qt::AlignRight, labels.at(i).first);
    9. // same for the text.
    10. }
    To copy to clipboard, switch view to plain text mode 

    The solution with a QWidget is possible but then you could simply use Q***Widget. And also the delegate functions are called quite a lot so it isn't with good performance.

  2. #2
    Join Date
    Aug 2012
    Location
    Wrocław, Poland
    Posts
    25
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to simplify a complex item delegate?

    Quote Originally Posted by wysota View Post
    This should work (not tested but you should get the picture...)

    Qt Code:
    1. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
    2. QPixmap px = index.data(Qt::DecorationRole).toPixmap();
    3. painter->drawPixmap(option.rect.topLeft()+QPoint(1,1), px);
    4. painter->drawText(option.rect.adjusted(px.width+1+5, 1, -1, -1), Qt::AlighLeft|Qt::AlignTop, QString("%1\n%2\n%3").arg(index.data(Qt::DisplayRole).toString()).arg(index.data(...).toString()).arg(index.data(...).toString()));
    5. }
    To copy to clipboard, switch view to plain text mode 
    Only that this does not do what I wanted. The 3 text fields consist of two columns which have to be aligned properly. I did mention that and it was visible on the image of current state I posted in the mean time.

    Although I do like the idea of drawing multiline text. I wasn't aware this is possible. Some simplification at least.

    Quote Originally Posted by Lykurg View Post
    The solution with a QWidget is possible but then you could simply use Q***Widget. And also the delegate functions are called quite a lot so it isn't with good performance.
    Why do you think this is bad? I am actually very attracted to this idea as it would allow me to do what I indeed wanted: use UI for designing the items. Also this might ease styling. After all the QWidget drawing/layout would do the same thing that I would do "by hand".

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to simplify a complex item delegate?

    Quote Originally Posted by Adam Badura View Post
    Only that this does not do what I wanted. The 3 text fields consist of two columns which have to be aligned properly. I did mention that and it was visible on the image of current state I posted in the mean time.
    I don't see any "two columns" on the image you posted (http://stackoverflow.com/questions/4...tyled-listview). Could you elaborate what you mean? I can see a thumbnail and four rows of single word strings.

    Why do you think this is bad?
    This is really bad, trust me Try having more than 10 rows with cell widgets and notice how performance drops. If you need anything complex I would really consider using QtQuick for the view.

    Anyway I will think about some mechanism for simplified delegate layout. I think for static cases such as yours this is possible. It's much more complicated if you want to do animations (e.g. like in QProgressBar on modern Windows). I used to tutor a bachelor thesis that aimed to work around this problem.

    If you don't hear back from me in a couple of days, send me a PM or bump the thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Aug 2012
    Location
    Wrocław, Poland
    Posts
    25
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to simplify a complex item delegate?

    Quote Originally Posted by wysota View Post
    I don't see any "two columns" on the image you posted (http://stackoverflow.com/questions/4...tyled-listview). Could you elaborate what you mean? I can see a thumbnail and four rows of single word strings.
    See second paragraph of my first post. Then second paragraph of the post in which I asked you for sample. Then the post in which I provided my current code and a snapshot of what it does. The Windows Media Player example was just that. An example of similar layout which I found quickly. I didn't know any better example and didn't want to spend to much time looking for one.


    Quote Originally Posted by wysota View Post
    This is really bad, trust me Try having more than 10 rows with cell widgets and notice how performance drops. If you need anything complex I would really consider using QtQuick for the view.
    I didn't want to have QWidget for every item. (This way I could use QTreeWidget instead...) I wanted to have just one object hidden behind the scenes which I would feed with data (either directly or by sharing model and changing "active index") to than size/paint through it. Would that be bad to? Why? What more would it do (as compared to ordinary delegate) that it would influence performance so much?

    I cannot switch to QtQuick. I would like but the decision is not up to me.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to simplify a complex item delegate?

    Quote Originally Posted by Adam Badura View Post
    See second paragraph of my first post. Then second paragraph of the post in which I asked you for sample. Then the post in which I provided my current code and a snapshot of what it does. The Windows Media Player example was just that. An example of similar layout which I found quickly. I didn't know any better example and didn't want to spend to much time looking for one.
    Your delegate is still pretty simple.

    I didn't want to have QWidget for every item. (This way I could use QTreeWidget instead...)
    I don't understand how using QTreeWidget influences anything here.

    I wanted to have just one object hidden behind the scenes which I would feed with data (either directly or by sharing model and changing "active index") to than size/paint through it. Would that be bad to? Why?
    You would have to modify most properties of that object during every paint which is quite heavy.

    I cannot switch to QtQuick.
    Not even for this single view?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to simplify a complex item delegate?

    Just to let you know, I already have a preliminary working solution. It's not pixel-perfect yet, but it already gives acceptable results.

    delegatelayout.jpg

    Qt Code:
    1. DelegateLayout *l = new DelegateLayoutFrame(Qt::Horizontal);
    2. l->setSpacing(10);
    3. l->setMargins(QMargins(5,5,5,5));
    4. DelegatePixmap *p1 = new DelegatePixmap(l);
    5. DelegateText *t1 = new DelegateText(l);
    6. t1->setRole(Qt::UserRole+1);
    7. t1->setMargins(QMargins(5,5,5,5));
    8. t1->setMinimumSize(QSize(QFontMetrics(f).width("MMMM"), 1));
    9.  
    10. DelegateLayout *headersl = new DelegateLayout(Qt::Vertical, l);
    11. DelegateStaticText *h1 = new DelegateStaticText(headersl);
    12. h1->setText("Name:");
    13. DelegateStaticText *h2 = new DelegateStaticText(headersl);
    14. h2->setText("Created:");
    15.  
    16. DelegateLayout *valuesl = new DelegateLayout(Qt::Vertical, l);
    17. DelegateText *t2 = new DelegateText(valuesl);
    18. t2->setRole(Qt::DisplayRole);
    19. t2->setForegroundColor(Qt::darkGreen);
    20. DelegateText *t3 = new DelegateText(valuesl);
    21. t3->setRole(Qt::UserRole);
    22.  
    23. LayoutDelegate *ld = new LayoutDelegate(&v);
    24. ld->setLayout(l);
    25. v.setItemDelegate(ld);
    26. v.setModel(&model);
    27.  
    28. v.show();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2012
    Location
    Wrocław, Poland
    Posts
    25
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to simplify a complex item delegate?

    I'm waiting. Looks very promising. Those Delegate* types are your own, are they?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to simplify a complex item delegate?

    Quote Originally Posted by Adam Badura View Post
    I'm waiting. Looks very promising. Those Delegate* types are your own, are they?
    Yes, they are mine.

    Here is the current state:

    delegatelayout1.png
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Aug 2012
    Location
    Wrocław, Poland
    Posts
    25
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to simplify a complex item delegate?

    How is this going? I got assigned back to the task at least for some time so I can now introduce improvements.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to simplify a complex item delegate?

    Are you interested only in the display of items or also about editing and interaction?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Aug 2012
    Location
    Wrocław, Poland
    Posts
    25
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to simplify a complex item delegate?

    Quote Originally Posted by wysota View Post
    Are you interested only in the display of items or also about editing and interaction?
    Only display for now.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to simplify a complex item delegate?

    In that case I will post my current solution and provide a link to it during the weekend. You want to use it in a commercial project?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QLabel as an item delegate
    By qlands in forum Qt Programming
    Replies: 13
    Last Post: 12th August 2011, 11:51
  2. Using model indices in complex model item relationships
    By hackerNovitiate in forum Newbie
    Replies: 0
    Last Post: 29th June 2011, 14:30
  3. Replies: 0
    Last Post: 10th March 2011, 11:44
  4. Replies: 5
    Last Post: 10th August 2009, 10:50
  5. Delegate for a certain item?
    By somebody in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 22:55

Tags for this Thread

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.