Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make QListview and QFilesystemModel item with multiple line text?
Forum Updated to NodeBB v4.3 + New Features

How to make QListview and QFilesystemModel item with multiple line text?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 7.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kennylemon
    wrote on last edited by
    #1

    Hi guys:

    I want to use a QListView and a QFilesystemModel to customize an icon viewer like the original one in Mac!!!

    The original Mac icon viewer:
    !http://dl.dropbox.com/u/24137109/icontext1.png(Mac Icon Viewer)!

    And the custom icon viewer:
    !http://dl.dropbox.com/u/24137109/icontext2.png(Mac Icon Viewer)!

    The problem is that the file name in the custom icon viewer can only display single line, however, the original Mac icon viewer can display file name in two lines.

    What I should do to make the custom icon viewer displays the file name in two lines.
    If two line can not contain all the file name, show ellipsis at the right side of the second line.

    Should I subclass QStyledItemDelegate and override paint() function or something else??

    Could anyone give me a hint!!

    Thanks in advance!!

    Kenny

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      you could try out "ElidedText...":http://doc.qt.nokia.com/4.7/qfontmetrics.html#elidedText

      and use Qt::ElideMiddle

      But it seems strange to me that the mac style uses it and it's not in your example because the OS style should be followed.

      If you inherit QStyledItemDelegate then paint is where you can do your thing.

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kennylemon
        wrote on last edited by
        #3

        Thanks for your replied!!

        Actually, I try to make a cross platform( mac/windows ) icon viewer, and the elide condition of windows seems to elide right, so I want to follow the condition in windows.

        I have tried to use Qt::ElideRight, but it can not make the file name to display in two lines, and elide at the end of the second line of the file name.
        Any hint about this issue??

        Besides, I have tried to inherit QStyledItemDelegate, but the text seems to show twice, and the text may overlap with each other, how to make it just display once??

        Thanks in advance!!

        Kenny

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          bq. Besides, I have tried to inherit QStyledItemDelegate, but the text seems to show twice, and the text may overlap with each other, how to make it just display once??

          can you show the code for the delegate?

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kennylemon
            wrote on last edited by
            #5

            sure, I inherited QStyledItemDelegate and override paint() function as the code below

            @
            void CFMDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
            {
            QStyleOptionViewItemV4 opt = option;
            initStyleOption( &opt, index );

            // draw correct background
            QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
            style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
            
            QString strText = opt.text;
            opt.text = "";
            QRect rect = opt.rect;
            QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
            if ( cg == QPalette::Normal && !( opt.state & QStyle::State_Active ) )
            {
                cg = QPalette::Inactive;
            }
            
            // set pen color
            if ( opt.state & QStyle::State_Selected )
            {
                painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
            }
            else
            {
                painter->setPen(opt.palette.color(cg, QPalette::Text));
            }
            
            QString newText = painter->fontMetrics().elidedText( strText, Qt::ElideRight, rect.width() );
            int nFlags = Qt::TextWrapAnywhere | Qt::AlignBottom;
            painter->drawText( rect, nFlags, newText );
            

            }@

            And the overlap situation is like the image below:
            !http://dl.dropbox.com/u/24137109/overlap.png(image)!

            1 Reply Last reply
            0
            • EddyE Offline
              EddyE Offline
              Eddy
              wrote on last edited by
              #6

              Hi Kenny,

              1. in your code you have :

              @style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
              ...
              painter->drawText( rect, nFlags, newText ); @
              both lines draw text. the first draws the item and the other text only. That's why you see the text double.

              1. Also sizeHint and updateEditorGeometry(if you want editing) will need to be reimplemented to put the second line under the first. Now there is only on line.
                Here is some example code. not tested for your case...

              @QSize yourDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
              {
              QString Value =index.data(Qt::DisplayRole).toString() ;
              QSize sz = QStyledItemDelegate::sizeHint(option, index);

                  sz.setHeight(2 *  sz.height());
              
                  return sz;
              

              }

              void yourDelegate::updateEditorGeometry(QWidget editor,
              const QStyleOptionViewItem &option, const QModelIndex &/
              index */) const
              {
              QSize ExtraSize= QSize(option.rect.width(), 2 * option.rect.height());
              QRect ExtraRect = option.rect;
              ExtraRect.setSize(ExtraSize);
              editor->setGeometry(option.rect);
              }@

              1. You will need some QString parsing if the wrapping doesn't do it's job. Let's hope not

              Qt Certified Specialist
              www.edalsolutions.be

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Kennylemon
                wrote on last edited by
                #7

                Thanks for your reply, Eddy
                I will try it ASAP.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  philk
                  wrote on last edited by
                  #8

                  [quote author="Eddy" date="1312548300"]Hi Kenny,

                  @
                  QSize yourDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
                  {
                  QString Value =index.data(Qt::DisplayRole).toString() ;
                  QSize sz = QStyledItemDelegate::sizeHint(option, index);

                      sz.setHeight(2 *  sz.height());
                  
                      return sz;
                  

                  }
                  @
                  [/quote]

                  Eddy, how is Value used in this code? I do not see it referenced.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved