简体   繁体   English

如何在 QTableView/QAbstractTableModel 中使用多行文本/换行符?

[英]How to use multiline text / linebreaks in QTableView/QAbstractTableModel?

I have subclassed QAbstractTableModel and QTabelView (and QSortFilterProxyModel) and I'd like to allow multi-line text to be displayed to and entered by the user in text cells (currently, hitting return will confirm the edit instead of inserting a line break).我已将 QAbstractTableModel 和 QTabelView(以及 QSortFilterProxyModel)子类化,并且我希望允许用户在文本单元格中显示和输入多行文本(目前,点击返回将确认编辑而不是插入换行符)。

There should be some simple flag to set, I just don't know which one...应该有一些简单的标志来设置,我只是不知道是哪一个......

QItemDelegate can actually draw the text over multiple lines (if it contains explicit '\n' characters), while the default QStyledItemDelegate doesn't do that (at least not on Linux, I didn't test other platforms). QItemDelegate实际上可以在多行上绘制文本(如果它包含明确的 '\n' 字符),而默认的QStyledItemDelegate不会这样做(至少在 Linux 上没有,我没有测试其他平台)。 So we'll need to use QItemDelegate for the rendering.所以我们需要使用QItemDelegate进行渲染。

But we also need to change the widget used for editing, which can't be a QLineEdit anymore, it needs to be a QPlainTextEdit .但是我们还需要更改用于编辑的小部件,它不能再是QLineEdit了,它需要是QPlainTextEdit So we'll have to derive from QItemDelegate and change the type of widget being created for editing.因此,我们必须从QItemDelegate派生并更改为编辑而创建的小部件的类型。

#ifndef MULTILINEDELEGATE_H
#define MULTILINEDELEGATE_H

#include <QItemDelegate>

class MultilineDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    using QItemDelegate::QItemDelegate;

public:
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
};

#endif
#include "multilinedelegate.h"
#include <QPlainTextEdit>

QWidget *MultilineDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return new QPlainTextEdit(parent);
}

void MultilineDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (auto *textEdit = qobject_cast<QPlainTextEdit *>(editor)) {
        textEdit->setPlainText(index.data(Qt::EditRole).toString());
    } else {
        QItemDelegate::setEditorData(editor, index);
    }
}

void MultilineDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if (auto *textEdit = qobject_cast<QPlainTextEdit *>(editor)) {
        model->setData(index, textEdit->toPlainText());
    } else {
        QItemDelegate::setModelData(editor, model, index);
    }
}

And now all that remains to be done is using that delegate on the appropriate columns.现在剩下要做的就是在适当的列上使用该委托。 For instance tableView->setItemDelegateForColumn(MyModel::AddressColumn, new MultilineDelegate(this));例如tableView->setItemDelegateForColumn(MyModel::AddressColumn, new MultilineDelegate(this));

This also looks better if the rows grow vertically as needed: tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);如果行根据需要垂直增长,这看起来也会更好: tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

Tested with Qt 5.15, hopefully it should work with Qt 6 as well.使用 Qt 5.15 进行测试,希望它也适用于 Qt 6。

Not entirely sure what you are asking but I'll assume you already have your cell set up with a widget that can accept multi-line input (ie QTextEdit using setItem on your QTableView ).不完全确定您要问什么,但我假设您已经为您的单元格设置了一个可以接受多行输入的小部件(即QTextEdit在您的QTableView上使用setItem )。

If you have that, I believe you would want to create a custom QItemDelegate derived class and override the default Enter behaviour to insert a line break rather than committing the delegate's data as is the default behaviour如果你有,我相信你会想要创建一个自定义QItemDelegate派生类并覆盖默认的 Enter 行为以插入换行符,而不是像默认行为那样提交委托的数据

Seehttp://qt-project.org/doc/qt-4.8/qitemdelegate.html#eventFilter to get you started in the right direction.请参阅http://qt-project.org/doc/qt-4.8/qitemdelegate.html#eventFilter以帮助您朝着正确的方向开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM