Altering requests from TreeView to Model for data...



  • Hey

    The idea:

    Alter treeView requerst to model for data. Ie model(index,role) I would like to alter the Role in my treeview to fine tune what I would like to display in given row based on user filters. Ie say I have a namespace configuration. Ie, each item has a Qt::userRole+namespace QVariant. If a user configure tree to displaynamespace then the feeding function instead of using model.index(index,Qt::DisplayRole) should use model.index(index,Qt::UserRole+namespace).

    So the question is, who/where do I look for that function? Cant figure it out o.O.

    Or perhaps there is a better way?

    I though that treeview edition is a good way because if I have 5 treeviews accessing thesame model(), then they all should be able to pass different qtDisplayRole roles for different sets of data.

    Regards
    Dariusz



  • the answer is delegate:
    subclass QStyledItemDelegate and reimplement paint()

    class MyDelegate : public QStyledItemDelegate{
    Q_OBJECT
    Q_DISABLE_COPY(MyDelegate)
    public:
    explicit MyDelegate(QObject* parent = Q_NULLPTR) : QStyledItemDelegate(parent){}
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
    {
        Q_ASSERT(index.isValid());
        QStyleOptionViewItem opt = option;
        initStyleOption(&opt, index);
        opt.text = displayText(index.data(Qt::UserRole+something), opt.locale);
        const QWidget *widget = option.widge);
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
    }
    };
    

    Now from the view you can call setItemDelegate or setItemDelegateForColumn to set the delegate



  • Hey
    Hohohohohooh that's way better than I thought! Amazing thanks!

    I already used item delegate to set the edit widget text with setEditorData and setModelData for my undo stack but did not realize how to use paint properly to paint the widgets.

    Do you know maybe if QStyledItemDelegate can somehow refresh/notify the views that use it to refresh their views? Or how do I fire refresh signal on it?



  • @Dariusz said in Altering requests from TreeView to Model for data...:

    Do you know maybe if QStyledItemDelegate can somehow refresh/notify the views that use it to refresh their views? Or how do I fire refresh signal on it?

    The delegate should not notify the view of changes ever. The flow of edits is:
    View asks delegate to start editing -> delegate creates the editor (createEditor() + setEditorData()) -> when editing is finished, the delegate updates the model (setModelData) -> the model signals the change (dataChanged())-> the view detects it needs to update -> the view asks the delegate to repaint


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.