在 QTreeWidget/QTreeView 中调整行的高度

发布于 2024-08-29 03:46:08 字数 752 浏览 3 评论 0原文

我在调整 QTreeWidget 中的行高度时遇到一些问题。我将 QStyledItemDelegate 与 QPlainTextEdit 一起使用。在 QPlainTextEdit 中编辑文本期间,我通过以下方式检查更改:

rect = self.blockBoundingRect(self.firstVisibleBlock())

如果文本的高度发生变化,我会调整编辑器大小,并且需要 QTreeWidget 中的行也调整大小。但我不知道如何通知 TreeWidget 或代表有关更改的信息。我尝试使用索引初始化编辑器,以便将来使用,但委托每次都会创建新的编辑器,而我无法使用信号。我还使用以下函数来捕获调整大小事件,但它没有:

bool QAbstractItemDelegate::editorEvent ( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index )

How can ibound editor's size changes with TreeWidget?

还有一件事,默认情况下,TreeWidget 中的所有项目(单元格)都具有 -1 或某个大值作为默认宽度。我需要单元格中的整个文本都可见,那么如何仅通过可见范围限制单元格宽度并使其高度扩展?我希望它具有与 MSWord 中的表格相同的行为。

先感谢您, 哔叽

I have some problems with sizing row's height in QTreeWidget. I use QStyledItemDelegate with QPlainTextEdit. During editing text in QPlainTextEdit i check for changes with a help of:

rect = self.blockBoundingRect(self.firstVisibleBlock())

and if text's height changes i resize editor size and need row in QTreeWidget also resizing. But i don't know how to inform TreeWidget or a Delegate about changes. I tried to initialize editor with index, that i could use in a future, but Delegate creates new editor every time and i failed to use signals. Also I used following function to catch resize event, but it' doesn't:

bool QAbstractItemDelegate::editorEvent ( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index )

How can i bound editor's size changes with TreeWidget?

And one more thing, by default all items (cells) in TreeWidget have -1 or some big value as default width. I need whole text in cell to be visible, so how can i limit cells width only by visible range and make it expand in height? I want for it the same behavior as in instance a table in MSWord.

Thank you in advance,
Serge

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2024-09-05 03:46:08

我相信您必须在不关闭编辑器的情况下通知模型有关数据更改的信息,这应该强制树视图根据其显示的字段的新内容重新计算其行高。您可以做的是覆盖委托中的 eventFilter 方法,并尝试在按键时发出 commitData 信号;像这样的:

bool YourStyledItemDelegate::eventFilter(QObject* object, QEvent* event)
{
    bool result = QStyledItemDelegate::eventFilter(object, event);
    QWidget* editor = qobject_cast<QWidget*>(object);
    if (editor)
    {
        if (event->type() == QEvent::KeyPress)
        {
            emit commitData(editor); //<- this should force row to recalculate its size
        }
    }
    return result;
}

希望这能让您了解如何继续,问候

I believe you would have to notify model about the data change without closing editor, this should force treeview to recalculate its row height according to the new content of the field it's showing. What you could do is override eventFilter method in your delegate and try to emit commitData signal on key press; smth like this:

bool YourStyledItemDelegate::eventFilter(QObject* object, QEvent* event)
{
    bool result = QStyledItemDelegate::eventFilter(object, event);
    QWidget* editor = qobject_cast<QWidget*>(object);
    if (editor)
    {
        if (event->type() == QEvent::KeyPress)
        {
            emit commitData(editor); //<- this should force row to recalculate its size
        }
    }
    return result;
}

hope this would give you an idea on how to proceed, regars

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文