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. Problem in TextWord Wrapping in QListView[Unable to do both TextWordWrap & TextWrapAnywhere together ].
QtWS25 Last Chance

Problem in TextWord Wrapping in QListView[Unable to do both TextWordWrap & TextWrapAnywhere together ].

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.4k Views
  • 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.
  • P Offline
    P Offline
    psbhardwaj09gmail.com
    wrote on 26 Feb 2013, 06:14 last edited by
    #1

    Hi,

    I am working on a chat application where i have a QtextEdit and QListView . What ever i type in QTextedit its shown as new entry in QListView.

    I am using custom delegate for QListView to show whole text in an item in QlistView . In that delegate i am using sizeHint and paint function as described below

    @
    void ChatViewItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const
    {

    QString uri = index.model()->data(index, Qt::UserRole+2).toString();
    QString textMessage = index.model()->data(index, Qt::UserRole+5).toString();
    
    QRect messageRect = option.rect;
    QRect uriRect = option.rect;
    
    QFont font = QApplication::font();
    font.setBold(true);
    QFont messageFont = QApplication::font();
    messageFont.setWeight(10);
    
    QRectF boundryRect(option.rect.topLeft(), option.rect.bottomRight());
    boundryRect.setRight(messageRect.right());
    boundryRect.setTop(messageRect.top() + 1);
    
    if (option.state & QStyle::State_Selected)
         painter->fillRect(boundryRect, option.palette.highlight());
    
    painter->save();
    painter->drawRect(boundryRect);
    
    messageRect.setLeft(messageRect.left() + 20);
    messageRect.setTop(messageRect.top() + 5);
    messageRect.setRight(messageRect.right() - 5);
    messageRect.setBottom(messageRect.bottom() - 5);
    
    painter->drawText(messageRect,  Qt::AlignBottom | Qt::TextWrapAnywhere , textMessage);
    
    painter->setFont(font);
    painter->drawText(uriRect, Qt::AlignLeft  , uri);
    painter->restore();
    

    }

    QSize ChatViewItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
    QAbstractItemView view = dynamic_cast<QAbstractItemView>(parent());
    QFontMetricsF fontMetrics(view->fontMetrics());
    QString str = index.data().toString().trimmed();
    QStringList words = str.split(QChar(' '));
    QStringList lines = str.split(QChar('\n'));
    int noOfLines = lines.size();
    if(noOfLines > 2)
    noOfLines = 1;
    else
    noOfLines =0;
    QRectF boundingRect = fontMetrics.boundingRect(str);
    int width = view->viewport()->width()- 25;
    int height = boundingRect.height();
    int times = 0;
    while (words.size() > 0)
    {
    times++;
    qreal lineWidth = 0;
    bool enoughSpace = true;
    do
    {
    QString word = words.first();
    qreal wordWidth = fontMetrics.width(word);
    if(wordWidth > width)
    {
    times++;
    while(wordWidth > width )
    {
    times++;
    wordWidth -= width;
    }
    lineWidth = wordWidth;
    words.removeFirst();
    }

            else if (wordWidth + lineWidth < width)
            {
                lineWidth += wordWidth;
                lineWidth += fontMetrics.width(QChar(' '));
                words.removeFirst();
            }
            else
                enoughSpace = false;
        }
        while (enoughSpace && words.size() > 0);
    }
    height = height * times  + (2 + noOfLines)* boundingRect.height();
    return QSize(width,  height);
    

    }

    @

    Problem: please see the attachments
    I am using Qt::TextWrapAnywhere for text wrapping. The problem with that is that it truncate the word in the end of line and show it in next line.

    If i Use QT::TextWordWrap then if the word is too long then it doesnt truncate it ... on maximizing the window it appears.

    If i use flags as @int flags = Qt::AlignBottom|Qt::TextDontClip|Qt::TextWordWrap|Qt::TextWrapAnywhere|Qt::TextJustificationForced;@ still it works like Qt::TextWrapAnywhere

    Can someone please help me that what i should do to fix this.
    !http://www2.picturepush.com/photo/a/12284140/640/12284140.jpg(word wrap)!
    !http://www5.picturepush.com/photo/a/12284153/640/12284153.jpg!
    !http://www2.picturepush.com/photo/a/12284155/640/12284155.jpg(word wrap anywhere)!

    Pardeep Sharma

    1 Reply Last reply
    0
    • B Offline
      B Offline
      b1gsnak3
      wrote on 26 Feb 2013, 07:30 last edited by
      #2

      try with Qt::WrapAtWordBoundaryOrAnywhere

      1 Reply Last reply
      0
      • P Offline
        P Offline
        psbhardwaj09gmail.com
        wrote on 26 Feb 2013, 09:53 last edited by
        #3

        Hi, this is not working in my case. Can u give me suggestion how to use it?

        Pardeep Sharma

        1 Reply Last reply
        0
        • P Offline
          P Offline
          psbhardwaj09gmail.com
          wrote on 26 Feb 2013, 09:55 last edited by
          #4

          Hi, this is not working in my case. Can u give me suggestion how to use it?

          Pardeep Sharma

          1 Reply Last reply
          0

          1/4

          26 Feb 2013, 06:14

          • Login

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