Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7556385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:51:00+00:00 2026-05-30T11:51:00+00:00

I have a custom delegate derived from QStyleOptionViewItem which is trying to draw multiline

  • 0

I have a custom delegate derived from QStyleOptionViewItem which is trying to draw multiline (wordwrap) a long line of text in the paint method. After doing some search and Qt doc reading, I looks like I need to use QTextLayout for such task, below is the code I have which still puts the text in one single line, any hints on how to wrapping the line around length of the QStyleOptionViewItem passed in? Thanks!!

void Delegate::paint(QPainter *painter,
                     const QStyleOptionViewItem &option,
                     const QModelIndex &index) const
{
  painter->save();

  painter->translate(option.rect.topLeft());

  QString title = index.data(Qt::DisplayRole).toString();
  QTextLayout * layout = new QTextLayout(title, QApplication::font());

  layout->beginLayout();
  QTextLine line = layout->createLine();
  while (line.isValid()) {
    line.setLineWidth(option.rect.width());
    line = layout->createLine();
  }
 layout->endLayout();
  layout->draw(painter, QPointF(0, 0));

  painter->restore();
}

Since I can’t self answer, I will just post my findings here.
I found couple issues with my code:

  1. The test string I are one word consisted of 200 characters and by default QTextLayout does word wrap. So I have to explicitly call QTextLayout::setWrapMode() for that test case to wrap.
  2. I am not setting position for each line.

This is my paint method in Ruby:

def paint painter, styleOptionViewItem, modelIndex
  painter.save
  painter.translate styleOptionViewItem.rect.top_left

  marked_text = modelIndex.data(Qt::DisplayRole).value
  font = Qt::Application::font()
  text_layout = Qt::TextLayout.new marked_text
  text_layout.setFont font

  text_option = Qt::TextOption.new
  text_option.setWrapMode(Qt::TextOption::WrapAtWordBoundaryOrAnywhere)
  text_layout.setTextOption text_option

  text_layout.beginLayout
  fm = Qt::FontMetrics.new font
  font_height = fm.height
  i = 0
  while i< LINE_LIMIT do
    line = text_layout.createLine
    break if (!line.isValid())
    line.setLineWidth(styleOptionViewItem.rect.width)
    line.setPosition(Qt::PointF.new(0, font_height * i))
    i += 1
  end
  text_layout.endLayout
  text_layout.draw painter, Qt::PointF.new(0, 0)
  painter.restore
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:51:01+00:00Added an answer on May 30, 2026 at 11:51 am

    I had to do same task for a while.
    While I used simple QPainter::drwText, I bumped into this issue.

    To make word wrap work, you should:

    • disable uniformRowHeight property of view.
    • handle sizeHint correctly. By default this function return 0, you should override it to return Qt::SizeHint role of item data.

    • But you should also set correct value for Qt::SizeHint role. You can use QFontMetrics::boundingRect to calculate sizeHint, but you should ensure you use same font when calculating sizeHint and when drawing item. On Windows 7 I had an issue, that font of QStandardItem didn’t coincide with QListView‘s one.

      Note, that it is bad idea to calculate sizeHint from scratch every time it requested, because it works really slow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written custom delegate methods in ClassFile(B), now i am calling them from
I have a custom view which will act as a text field and another
I have a Custom class which is supposed to load a window from a
I have a question regarding setting up a custom delegate class for use with
I have a custom iPhone application which doesn't rely on UIKit in any way
I have a custom UIView which is composed of many images, their positions are
In Silverlight 4 I have a custom service class which has an asynchronous Completed
I want to use custom delegate in my search tab. I have never used
I'm trying to add a custom delegate to a custom UITableViewCell of mine. On
in app i have custom tableview(UITableView *tableView) which subclass of uiviewcontroller & in tableview

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.