Results 1 to 7 of 7

Thread: QListWidget or QListView with QLabel(s)

  1. #1
    Join Date
    Aug 2008
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QListWidget or QListView with QLabel(s)

    Hello.

    How can I insert QLabel(as item) to QListWidget (or QListView)? I want this for adding rich text messages to list widget.
    Can anyone please post some examples?

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

    Default Re: QListWidget or QListView with QLabel(s)

    Oh, that's a bad idea because of performance reasons. A much better one is to implement a custom delegate that would render rich text using QTextDocument. I think there's even an example of such a delegate on the forum somewhere.

  3. #3
    Join Date
    Aug 2008
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget or QListView with QLabel(s)

    Quote Originally Posted by wysota View Post
    Oh, that's a bad idea because of performance reasons. A much better one is to implement a custom delegate that would render rich text using QTextDocument. I think there's even an example of such a delegate on the forum somewhere.
    You are thinking about this: http://www.qtcentre.org/forum/f-qt-p...view-8371.html?

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

    Default Re: QListWidget or QListView with QLabel(s)

    Post #19 in the thread looks promising, although I was referring to some other thread. You can also download my wwWidgets and look at the QwwRichTextButton implementation and implement a similar thing as a delegate.

  5. #5
    Join Date
    Aug 2008
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget or QListView with QLabel(s)

    For first I must explain something.
    I would like to do the chat widget.
    So I started with the QTextBrowser.
    After adding 80 lines(every line contained 30 pictures(16x16px gif)), the QTextBrowser turned out to be too slow(slow selecting text, slow srolling, etc.).
    So perhaps would the QListWidget(with rich text items) be good for chat presentation?

    OK. I downloaded http://www.qtcentre.org/forum/attach...3&d=1188673979.
    I changed in maindialog.cpp lines:
    Qt Code:
    1. << "The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class. The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class. The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class."
    2. << "The <b>QObject</b> class is the base class of all Qt objects."
    3. << "Qt's <b>drag and drop</b> infrastructure is fully supported by the model/view framework. Items in lists, tables, and trees can be dragged within the views, and data can be imported and exported as MIME-encoded data.";
    To copy to clipboard, switch view to plain text mode 
    to:
    Qt Code:
    1. << "<font size=\"6\">The <b>QListWidgetItem</b> class."
    2. << "The <b>QObject</b> class is the base class of all Qt objects."
    3. << "Qt's <b>drag and drop</b>";
    To copy to clipboard, switch view to plain text mode 
    Result is wrong, because the text "The QListWidgetItem class." is covering the different text(See attachment.)

    In sectionitem.cpp I changed in function:
    Qt Code:
    1. QSize SectionItem::sizeHint(QObject *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    2. {
    3. QListWidget *p = (QListWidget*)parent;
    4. QFontMetrics fm(p->font());
    5.  
    6. float rw = float(p->viewport()->size().width());
    7. float tw = fm.width(itemText);
    8. float ratio = tw/rw;
    9. int lines = int(ratio) + 1;
    10. return QSize(rw, lines * fm.height());
    11. }
    To copy to clipboard, switch view to plain text mode 
    one line:
    Qt Code:
    1. int lines = int(ratio) + 2;
    To copy to clipboard, switch view to plain text mode 
    That works fine, but only when I don't resize widget and when text is "<font size=\"6\">The <b>QListWidgetItem</b> class.".

    How to calculate the number of lines dynamically, if the text have different lenght and font sizes?
    Attached Images Attached Images
    Last edited by lukass; 8th August 2008 at 15:08. Reason: add text

  6. #6
    Join Date
    Aug 2008
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget or QListView with QLabel(s)

    Can anyone help?

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

    Default Re: QListWidget or QListView with QLabel(s)

    The text document can calculate the size of its contents, so just use it.

Similar Threads

  1. Replies: 6
    Last Post: 1st July 2008, 20:34
  2. QListView word wrap
    By serega in forum Qt Programming
    Replies: 17
    Last Post: 30th August 2007, 03:13
  3. Replies: 13
    Last Post: 15th December 2006, 11:52
  4. QListWidget / QListView Bug ?
    By guilugi in forum Qt Programming
    Replies: 5
    Last Post: 16th November 2006, 11:33

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.