QTableWidget / QTableView multiline text reverts to single line display when editing?
-
wrote on 10 Feb 2021, 02:18 last edited by
I have a QTableWidget that is set to wordwrap (true) and after it loads some rows, I do: resizeRowsToContents()
This resizes the rows and seems to wordwrap the text as you'd expect, so it's now multiline (as seen in 1st attached image). This works for reading, but as soon as I enter the mode of editing an item, it shows it on a single line ( as seen in 2nd attached image).
I would like it to display as multiline text when editing, too.
Is this possible? The most I've been able to find so far is something to do with an editor related to the item delegate, but I'm not sure how to go about changing the behavior or why it's not wordwrapped even though wordwrap is turned on. I guess the wordwrap does not apply to the editor itself?
For reference, I am using Qt 5.11
-
I have a QTableWidget that is set to wordwrap (true) and after it loads some rows, I do: resizeRowsToContents()
This resizes the rows and seems to wordwrap the text as you'd expect, so it's now multiline (as seen in 1st attached image). This works for reading, but as soon as I enter the mode of editing an item, it shows it on a single line ( as seen in 2nd attached image).
I would like it to display as multiline text when editing, too.
Is this possible? The most I've been able to find so far is something to do with an editor related to the item delegate, but I'm not sure how to go about changing the behavior or why it's not wordwrapped even though wordwrap is turned on. I guess the wordwrap does not apply to the editor itself?
For reference, I am using Qt 5.11
wrote on 10 Feb 2021, 07:44 last edited by@Trapezoid_Dreams
As you say, you can useQAbstractItemView::setItemDelegate()
&QStyledItemDelegate::createEditor()
to provide your own editor instead of the default one. -
wrote on 11 Feb 2021, 08:03 last edited by
Hi, thank you, this helped me better wrap my head around what I need to be doing and what to look for. Found this example: https://www.off-soft.net/en/develop/qt/qtb2.html
What I ended up with based on the linked example, in case anyone else comes across this problem:
MultiLineDelegate.h
#pragma once #include <QStyledItemDelegate> #include <QPlainTextEdit> class MultiLineDelegate : public QStyledItemDelegate { Q_OBJECT public: MultiLineDelegate(QWidget *parent = NULL); virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; virtual void setEditorData(QWidget *editor, const QModelIndex &index) const; virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; };
MultiLineDelegate.cpp
#include "MultiLineDelegate.h" MultiLineDelegate::MultiLineDelegate(QWidget *parent) : QStyledItemDelegate(parent) { } QWidget *MultiLineDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QPlainTextEdit *editor = new QPlainTextEdit(parent); return editor; } void MultiLineDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QPlainTextEdit *mEditor = qobject_cast<QPlainTextEdit *>(editor); mEditor->setPlainText(index.data().toString()); } void MultiLineDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QPlainTextEdit *mEditor = qobject_cast<QPlainTextEdit *>(editor); model->setData(index, mEditor->toPlainText()); }
And to apply it (in my case, I just want it on one particular column):
ui.myTable->setItemDelegateForColumn(1, new MultiLineDelegate);
Still some kinks to work out. It has some spacing/margins I don't need and enter doesn't close the editor (which I assume is a version change in behavior because the linked example actually has code reimplementing the event filter specifically to make it so enter doesn't close the editor and I didn't use that).
But for anyone else who may run into this problem and want a rough idea of what to do, this is working for me for the basics of a multiline editor.
1/3