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);
Re: How to Wrap the text in QTableView
Don't call tableView->resizeColumnsToContents();
Code:
//ui->tableView->resizeColumnsToContents(); //<<<<<< Don't Call
ui->tableView->resizeRowsToContents();
1 Attachment(s)
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.Attachment 9018
1 Attachment(s)
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
Attachment 9019
Code:
#include <QtGui>
#include <QApplication>
int main(int argc, char **argv)
{
int rows = 10;
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++) {
model->setData(mindex,readString);
}
}
tableView->setModel(model);
tableView->setWordWrap(true);
tableView->setTextElideMode(Qt::ElideLeft);
// tableView->resizeColumnsToContents();
tableView->resizeRowsToContents();
tableView->show();
return app.exec();
}
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.
Re: How to Wrap the text in QTableView
Quote:
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
Quote:
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 :)
Re: How to Wrap the text in QTableView
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);
/************************************************** ************************************************** ***********************************/
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?
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??
Re: How to Wrap the text in QTableView
Quote:
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
Code:
#include <QtGui>
#include <QtWidgets>
#include <QApplication>
int main(int argc, char **argv)
{
int rows = 10;
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++) {
model->setData(mindex,readString);
}
}
tableView->setModel(model);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < 1; c++) {
vhdr
->setSectionResizeMode
(r,
QHeaderView::ResizeToContents);
}
}
tableView->setWordWrap(true);
// tableView->setTextElideMode(Qt::ElideLeft);
// tableView->resizeColumnsToContents();
// tableView->resizeRowsToContents();
tableView->show();
return app.exec();
}
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.