PDA

View Full Version : How to Wrap the text in QTableView



mythili
8th May 2013, 11:13
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);

Santosh Reddy
8th May 2013, 11:56
Don't call tableView->resizeColumnsToContents();


//ui->tableView->resizeColumnsToContents(); //<<<<<< Don't Call
ui->tableView->resizeRowsToContents();

mythili
8th May 2013, 12:01
If I am not calling resizeColumnsToContents, its not resizing to content. You can find the attached pic.9018

Santosh Reddy
8th May 2013, 12:17
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
9019


#include <QtGui>
#include <QApplication>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QTableView * tableView = new QTableView;
int rows = 10;

QStandardItemModel *model= new QStandardItemModel(rows,1);
model->setColumnCount(1);
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.";
for (int r = 0; r < rows; r++) {
for (int column = 0; column < 1; column++) {
QModelIndex mindex = model->index(r, column);
model->setData(mindex,readString);
}
}

tableView->setModel(model);
tableView->setWordWrap(true);
tableView->setTextElideMode(Qt::ElideLeft);
// tableView->resizeColumnsToContents();
tableView->resizeRowsToContents();

tableView->show();

return app.exec();
}

mythili
8th May 2013, 13:03
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.

Santosh Reddy
8th May 2013, 13:12
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 :)

mythili
8th May 2013, 13:28
Please check my code
/***************************************htmldelegat e.cpp*******************************************/
#include "htmldelegate.h"

HtmlDelegate::HtmlDelegate()
{
}
void HtmlDelegate::paint(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);

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

Santosh Reddy
8th May 2013, 13:38
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?

mythili
8th May 2013, 13:48
I have used ur code by commenting the 'resizeColumnsToContents'. Its not as per my requirement. I am using 'setSectionResizeMode'. Does it affects word wrap??

Santosh Reddy
8th May 2013, 14:06
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


#include <QtGui>
#include <QtWidgets>
#include <QApplication>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QTableView * tableView = new QTableView;
int rows = 10;

QStandardItemModel *model= new QStandardItemModel(rows,1);
model->setColumnCount(1);
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.";
for (int r = 0; r < rows; r++) {
for (int column = 0; column < 1; column++) {
QModelIndex mindex = model->index(r, column);
model->setData(mindex,readString);
}
}

tableView->setModel(model);
QHeaderView* hhdr = tableView->horizontalHeader();
QHeaderView* vhdr = tableView->verticalHeader();
for (int r = 0; r < rows; r++) {
for (int c = 0; c < 1; c++) {
vhdr->setSectionResizeMode(r, QHeaderView::ResizeToContents);
hhdr->setSectionResizeMode(c, QHeaderView::Stretch);
}
}

tableView->setWordWrap(true);
// tableView->setTextElideMode(Qt::ElideLeft);
// tableView->resizeColumnsToContents();
// tableView->resizeRowsToContents();

tableView->show();

return app.exec();
}

mythili
8th May 2013, 14:15
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.