Solved wordWrap(true) problem in listView
-
I want to implement horizontal line word wrap for the long text that I publish in
listView
. For this I have already usedui->listView->wordWrap(true);
but I get error log where in last line mentions
/home/neo/Qt/5.6/gcc_64/include/QtWidgets/qlistview.h:117: note: candidate expects 0 arguments, 1 provided
now if I remove the argument
true
then my code builds successfully but word wrap never happens if the text is too long. Current situation is if the text inlistView
is too long then a horizontal scrollbar is shown to display whole text, but I don't want that.Can you guys point me in right direction to where I am going wrong? Thank you.
-
ui->listView->setWordWrap( true );
wordWrap returns current state and does not take any parameters
-
@alex_malyu I tried using
ui->listView->setWordWrap( true );
and it threw argument error. So i tried changing it toui->listView->setWordWrap( );
but still it doesn't work. Can you elaborate a little how exactly i should use it? -
@Shawny said:
and it threw argument error.
Show an error.
look at qlistview.h, which you pointed atsetWordWrap Function is declared as
void setWordWrap( bool on );
Which means it require boolean value true or false.So the call has to be made as
ui->listView->setWordWrap( true );If you get an error it should be related to different problem.
-
listView
is QListView and the way i used it is shown below in a function. If i useui->listView->wordWrap();
in place ofui->listView->wordWrap(true);
then code doesn't throws error but word wrapping functionality wont work either.// when send button is pressed void MainWindow::on_pushButton_clicked() { QStandardItem* rowlist = new QStandardItem(QString(ui->textEdit->toPlainText())); model->appendRow(rowlist); ui->listView->setModel(model); ui->listView->wordWrap(true); }
Error log looks like:
/home/neo/Qt/chatty/mainwindow.cpp:-1: In member function 'void MainWindow::on_pushButton_clicked()': /home/neo/Qt/chatty/mainwindow.cpp:29: error: no matching function for call to 'QListView::wordWrap(bool)' ui->listView->wordWrap(true); ^ /home/neo/Qt/chatty/mainwindow.cpp:29: candidate is: /home/neo/Qt/5.6/gcc_64/include/QtWidgets/QListView:1: In file included from ../5.6/gcc_64/include/QtWidgets/QListView:1:0, /home/neo/Qt/build-chatty-Desktop_Qt_5_6_0_GCC_64bit-Debug/ui_mainwindow.h:17: from ./ui_mainwindow.h:17, /home/neo/Qt/chatty/mainwindow.cpp:2: from ../chatty/mainwindow.cpp:2: /home/neo/Qt/5.6/gcc_64/include/QtWidgets/qlistview.h:117: bool QListView::wordWrap() const bool wordWrap() const; ^ /home/neo/Qt/5.6/gcc_64/include/QtWidgets/qlistview.h:117: note: candidate expects 0 arguments, 1 provided
-
@Shawny said:
/home/neo/Qt/chatty/mainwindow.cpp:29: error: no matching function for call to 'QListView::wordWrap(bool)'
ui->listView->wordWrap(true);You still are calling wrong function.
It has to be
ui->listView->setWordWrap(true);Just replace
ui->listView->wordWrap(true);
with
ui->listView->setWordWrap(true);
-
Sorry I didn't properly took notice of it. But I did changed it now and now code runs successfully but word wrapping is still not happening. Dunno whats wrong.
Do I need to put some extra declarations to make this work?
-
Hi @Shawny!
The documentation saysPlease note that even if wrapping is enabled, the cell will not be expanded to make room for the text. It will print ellipsis for text that cannot be shown, according to the view's textElideMode.
-
ohh okay so any idea how I can perform text wrap in listView apart from this method?
Currently I came across many opinions on internet one of which suggests to use item delegate to perform this action.
But this seems to be very complex method just to get text wrap done. I think I have no other alternative. Let me know if you have any other thing in mind or maybe some useful references. Thank you.
-
@Shawny
hi
The reason it dont word wrap is that the default row height is like 1 line so it
don't try to wordwrap as not enough space in the height.Delegates can be complex but in this case you do it with one function, which
might be enough for what you want.class MyDelegate : public QStyledItemDelegate { public: MyDelegate(QObject* parent) : QStyledItemDelegate(parent) {} QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { QFontMetrics fm(option.font); const QAbstractItemModel* model = index.model(); QString Text = model->data(index, Qt::DisplayRole).toString(); QRect neededsize = fm.boundingRect( option.rect, Qt::TextWordWrap,Text ); return QSize(option.rect.width(), neededsize.height()); } }; and you also need ui->listView->setWordWrap(true); ui->listView->setItemDelegate(new MyDelegate(this));
The concept is that it check how high the row should be for the text and expand cell.
it will still show ... for last word sometimes though. -
Works great! Thank you for this. But this syntax is super confusing. I will have to read some documentation on delegates to know how it works. However, text wrap is working in
listView
\m/ -
@Shawny
good to hear :)Its a good idea to read about delegates as they are used for most operations outside
of the normal settings. -
Understood. Thank you.