tags:

views:

339

answers:

1

Hi everyone,

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

+1  A: 

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

serge_gubenko
Thanks, it works, but not the way i need it, after commitment the cursor is set at the beginning, i can fix it somehow, but anyway the bigger problem appeared to set fixed size to the cell, that content of each cell was wordwraped and shown without ellipsis
serge
whenever you're committing data to the model your delegate's setEditorData method get's called; it's responsible for feeding editor widget with new data. I guess this is the place to look at to fix the cursor issue.
serge_gubenko
thanks again, but i'm struggling with setting size to the cell, and all for nothing
serge