Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget / QTableView multiline text reverts to single line display when editing?
QtWS25 Last Chance

QTableWidget / QTableView multiline text reverts to single line display when editing?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Trapezoid_Dreams
    wrote on 10 Feb 2021, 02:18 last edited by
    #1

    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

    rowsize.png
    rowsize2.png

    J 1 Reply Last reply 10 Feb 2021, 07:44
    0
    • T Trapezoid_Dreams
      10 Feb 2021, 02:18

      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

      rowsize.png
      rowsize2.png

      J Online
      J Online
      JonB
      wrote on 10 Feb 2021, 07:44 last edited by
      #2

      @Trapezoid_Dreams
      As you say, you can use QAbstractItemView::setItemDelegate() & QStyledItemDelegate::createEditor() to provide your own editor instead of the default one.

      1 Reply Last reply
      2
      • T Offline
        T Offline
        Trapezoid_Dreams
        wrote on 11 Feb 2021, 08:03 last edited by
        #3

        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 Reply Last reply
        1

        1/3

        10 Feb 2021, 02:18

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved