8

I need to implement rows moving via drag-n-drop in QTreeView and show the drop indicator between rows. I am wondering if there is a way to override indicator drawing, so it is displayed for all levels of hierarchy between rows only (not the rectangle around the item), the line must be as wide as the entire row (not as the one column).

Kara
  • 6,115
  • 16
  • 50
  • 57
mentalmushroom
  • 2,261
  • 1
  • 26
  • 34
  • I am facing similar problem. It seems that nobody knows (or wants to tell :P), so as usual in such cases, when i have some more time, I will dig through qt sources to find out what is possible and what's not. As soon as I know something I will try to answer your question. – j_kubik Mar 07 '12 at 01:59

1 Answers1

15

It is possible by modyfing style used to draw widget. My attempt seemed to work well, but it's a bit of cheating the qt's style system, so i cannot guarante that it will work under all possible styles on all platforms. So here it is:

class myViewStyle: public QProxyStyle{
public:
    myViewStyle(QStyle* style = 0);

    void drawPrimitive ( PrimitiveElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget = 0 ) const;
};

myViewStyle::myViewStyle(QStyle* style)
     :QProxyStyle(style)
{}

void myViewStyle::drawPrimitive ( PrimitiveElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget) const{
    if (element == QStyle::PE_IndicatorItemViewItemDrop && !option->rect.isNull()){
        QStyleOption opt(*option);
        opt.rect.setLeft(0);
        if (widget) opt.rect.setRight(widget->width());
        QProxyStyle::drawPrimitive(element, &opt, painter, widget);
        return;
    }
    QProxyStyle::drawPrimitive(element, option, painter, widget);
}

myView::myView(QWidget *parent) :
    QTreeView(parent)
{
    setStyle(new myViewStyle(style()));
}
j_kubik
  • 6,062
  • 1
  • 23
  • 42
  • Thanks alot. Your code works fine in an Xfce-DE. "Ported" your code to python, cause I did not find a simple solution included in QT. Learned something. – JackLeEmmerdeur Feb 18 '18 at 16:12
  • 1
    I know it's been 6 years, but could you share your python code? I have a similar question. – Mrtnchps Jun 01 '18 at 17:14
  • 3
    @Mrtnchps You can find the python code here: https://web.archive.org/web/http://apocalyptech.com/linux/qt/qtableview/ – Martin Hennings Feb 26 '19 at 13:05
  • Works as is on OSX Ventura + Qt5 and no special other style. – jlaurens Mar 29 '23 at 14:06
  • Memory management is missing, your myViewStyle instance is never deleted. See https://stackoverflow.com/questions/37786568/qstyle-ownership/37789558#37789558 – jlaurens Mar 29 '23 at 21:50