Results 1 to 11 of 11

Thread: How to Wrap the text in QTableView

  1. #1
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to Wrap the text in QTableView

    Hi, I want to wrap the text in QTableView. I am using these lines of code. but its not wraping. Please help me

    QStandardItemModel *model= new QStandardItemModel(row,1);
    model->setColumnCount(1);
    QString readString;
    for (int r = 0; r < row; r++) {
    for (int column = 0; column < 1; column++) {
    QModelIndex mindex = model->index(r, column);
    model->setData(mindex,readString);
    }
    }
    HtmlDelegate* delegate = new HtmlDelegate();
    ui->tableView->setModel(model);
    QHeaderView* hhdr = ui->tableView->horizontalHeader();
    QHeaderView* vhdr = ui->tableView->verticalHeader();
    for (int r = 0; r < row; r++) {
    for (int column = 0; column < 1; column++) {
    vhdr->setSectionResizeMode(r, QHeaderView::ResizeToContents);
    hhdr->setSectionResizeMode(column, QHeaderView::Stretch);
    }
    }
    ui->tableView->setWordWrap(true);
    ui->tableView->setTextElideMode(Qt::ElideLeft);
    ui->tableView->resizeColumnsToContents();
    ui->tableView->resizeRowsToContents();

    ui->tableView->setItemDelegate(delegate);

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to Wrap the text in QTableView

    Don't call tableView->resizeColumnsToContents();
    Qt Code:
    1. //ui->tableView->resizeColumnsToContents(); //<<<<<< Don't Call
    2. ui->tableView->resizeRowsToContents();
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to Wrap the text in QTableView

    If I am not calling resizeColumnsToContents, its not resizing to content. You can find the attached pic.qt.jpg

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to Wrap the text in QTableView

    I am sure there is somthing with your code, either you have not posted the code properly, or have not commented the resizeColumnsToContents()

    Moreover this on my machine
    QTreeView.jpg
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QTableView * tableView = new QTableView;
    9. int rows = 10;
    10.  
    11. model->setColumnCount(1);
    12. QString readString = "Moscow: Oleg Topalov could be credited with engineering one of the most daring prison breaks ever. The 33-year-old escaped from one of the highest security Russian prison using kitchen utensils.";
    13. for (int r = 0; r < rows; r++) {
    14. for (int column = 0; column < 1; column++) {
    15. QModelIndex mindex = model->index(r, column);
    16. model->setData(mindex,readString);
    17. }
    18. }
    19.  
    20. tableView->setModel(model);
    21. tableView->setWordWrap(true);
    22. tableView->setTextElideMode(Qt::ElideLeft);
    23. // tableView->resizeColumnsToContents();
    24. tableView->resizeRowsToContents();
    25.  
    26. tableView->show();
    27.  
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 8th May 2013 at 11:24.

  5. #5
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to Wrap the text in QTableView

    When we are resizing the column in your code, the height of the row is not setting to the content. My requirement is:
    1) Only one column, should be visible which should set to the QTableView
    2) row height should be to the size of content
    3) If the text is big,it should wrap and should be displayed in the next line of the same row.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to Wrap the text in QTableView

    When we are resizing the column in your code, the height of the row is not setting to the content.
    The code I posted does not resize column, it only resizes rows.

    If you want to adjust the height of row after resizing the columns, then you will have to call tableView->resizeRowsToContents(); again

    My requirement is:
    1) Only one column, should be visible which should set to the QTableView
    2) row height should be to the size of content
    3) If the text is big,it should wrap and should be displayed in the next line of the same row.
    The code I posted will do all these
    Last edited by Santosh Reddy; 8th May 2013 at 12:17.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to Wrap the text in QTableView

    Please check my code
    /***************************************htmldelegat e.cpp*******************************************/
    #include "htmldelegate.h"

    HtmlDelegate::HtmlDelegate()
    {
    }
    void HtmlDelegate:aint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    //qDebug()<<"paint"<<endl;
    QStyleOptionViewItemV4 optionV4 = option;
    initStyleOption(&optionV4, index);
    QStyle *style=optionV4.widget->style();
    // QApplication::style();
    // QStyle *style = optionV4.widget->style() : QApplication::style();

    QTextDocument doc;
    doc.setHtml(optionV4.text);

    // QTextBrowser browser;
    // browser.setWordWrapMode(QTextOption::WrapAtWordBou ndaryOrAnywhere);
    // browser.setLineWrapMode(QTextBrowser::NoWrap);
    // browser.setHtml(index.data().value<QString>());
    // QAbstractTextDocumentLayout::PaintContext context;

    /// Painting item without text
    optionV4.text = QString();
    style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);



    QAbstractTextDocumentLayout::PaintContext ctx;

    // Highlighting text if item is selected
    if (optionV4.state & QStyle::State_Selected)
    ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));

    QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
    painter->save();
    painter->translate(textRect.topLeft());
    painter->setClipRect(textRect.translated(-textRect.topLeft()));
    doc.documentLayout()->draw(painter, ctx);
    //browser.document()->documentLayout()->draw(painter, ctx);
    painter->restore();
    }

    QSize HtmlDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    //qDebug()<<"size"<<endl;
    QStyleOptionViewItemV4 optionV4 = option;

    initStyleOption(&optionV4, index);

    QTextDocument doc;
    doc.setHtml(optionV4.text);
    doc.setTextWidth(optionV4.rect.width());

    return QSize(doc.idealWidth(), doc.size().height());


    }
    /************************************************** ************************************************** ********************************/


    /************************************mainwindow.cpp ************************************************** ************************/

    QStandardItemModel *model= new QStandardItemModel(row,1);
    model->setColumnCount(1);
    QString readString;
    for (int r = 0; r < row; r++) {
    for (int column = 0; column < 1; column++) {
    readString = mystringlist.at(r);
    qDebug()<<"readstring:"<<readString<<endl;

    QModelIndex mindex = model->index(r, column);
    model->setData(mindex,readString);

    model->setHeaderData(0,Qt::Horizontal,tr("QUESTIONS")) ;
    }

    }


    HtmlDelegate* delegate = new HtmlDelegate();


    ui->tableView->setModel(model);
    ui->tableView->setWordWrap(true);
    ui->tableView->setTextElideMode(Qt::ElideLeft);
    QHeaderView* hhdr = ui->tableView->horizontalHeader();
    QHeaderView* vhdr = ui->tableView->verticalHeader();
    for (int r = 0; r < row; r++) {
    for (int column = 0; column < 1; column++) {
    vhdr->setSectionResizeMode(r, QHeaderView::ResizeToContents);
    hhdr->setSectionResizeMode(column, QHeaderView::Stretch);
    }
    }
    ui->tableView->resizeColumnsToContents();
    ui->tableView->resizeRowsToContents();

    ui->tableView->setItemDelegate(delegate);

    /************************************************** ************************************************** ***********************************/

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to Wrap the text in QTableView

    First thing, please use code tags, [code]...[\code] and replace \ with /

    What do you want me to do with this code? I have already suggested the change in my first post.

    Do you any other specific question?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. #9
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to Wrap the text in QTableView

    I have used ur code by commenting the 'resizeColumnsToContents'. Its not as per my requirement. I am using 'setSectionResizeMode'. Does it affects word wrap??

  10. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to Wrap the text in QTableView

    Its not as per my requirement. I am using 'setSectionResizeMode'. Does it affects word wrap??
    Yes if you use QHeaderView::Stretch, as QHeaderView will automatically resize the section to fill the available space. The size cannot be changed by the user or programmatically.

    This works perfectly
    Qt Code:
    1. #include <QtGui>
    2. #include <QtWidgets>
    3. #include <QApplication>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QTableView * tableView = new QTableView;
    10. int rows = 10;
    11.  
    12. model->setColumnCount(1);
    13. QString readString = "Moscow: Oleg Topalov could be credited with engineering one of the most daring prison breaks ever. The 33-year-old escaped from one of the highest security Russian prison using kitchen utensils.";
    14. for (int r = 0; r < rows; r++) {
    15. for (int column = 0; column < 1; column++) {
    16. QModelIndex mindex = model->index(r, column);
    17. model->setData(mindex,readString);
    18. }
    19. }
    20.  
    21. tableView->setModel(model);
    22. QHeaderView* hhdr = tableView->horizontalHeader();
    23. QHeaderView* vhdr = tableView->verticalHeader();
    24. for (int r = 0; r < rows; r++) {
    25. for (int c = 0; c < 1; c++) {
    26. vhdr->setSectionResizeMode(r, QHeaderView::ResizeToContents);
    27. hhdr->setSectionResizeMode(c, QHeaderView::Stretch);
    28. }
    29. }
    30.  
    31. tableView->setWordWrap(true);
    32. // tableView->setTextElideMode(Qt::ElideLeft);
    33. // tableView->resizeColumnsToContents();
    34. // tableView->resizeRowsToContents();
    35.  
    36. tableView->show();
    37.  
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 8th May 2013 at 13:15.

  11. #11
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to Wrap the text in QTableView

    Thank u very much....

    Actually I am reading data from html. I am displaying the image and text in rows. So I am using setItemDelegate to display. You can see htmldelegate.cpp which i posted before. As I am using setItemDelegate, its not wrapping the text. When I commented that line, its wrapping but its not displaying the image from html.

Similar Threads

  1. How to wrap text in a label?
    By sasi in forum Qt Programming
    Replies: 5
    Last Post: 22nd October 2016, 04:14
  2. Replies: 2
    Last Post: 9th April 2013, 06:26
  3. text indent with wrap
    By linuxsong in forum Qt Quick
    Replies: 1
    Last Post: 26th July 2011, 14:14
  4. Custom Delegate and Text Wrap
    By enlightened_j in forum Newbie
    Replies: 2
    Last Post: 17th September 2010, 06:21
  5. Wrap Text in PUSHBUTTON
    By BalaQT in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2009, 13:54

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.