Results 1 to 19 of 19

Thread: QTreeView and StylingSheets for QTreeView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default QTreeView and StylingSheets for QTreeView

    Hi Folks,

    Im having trouble understanding stylesheets and how to set styling of components of a QTreeView.
    I did look at the docs here:
    http://doc.qt.io/qt-5/stylesheet-reference.html &
    http://doc.qt.io/qt-4.8/stylesheet-examples.html

    The component I'd like to style are the Header(Title, Color, Height, Border), The ArrowIcon (Closed and Open Color), and the Scrollbar (Color, Width).
    Also would like to learn how to style things in general. Is there a system of thinking when styling widgets? How would I go about knowing what tags to call. example, i dont know what the arrow thing is called to style it, or the scrollbar.
    I used this for the ScrollArea, but/and i tried to use it with the scroll for the QTreeView, but nothing happened:
    Qt Code:
    1. QscrollBar::vertical{width: 12px;}
    2. QscrollBar::handle:vertical{background: #505050}
    To copy to clipboard, switch view to plain text mode 
    I tried:
    Qt Code:
    1. QTreeView::vertical{width: 12px;}
    2. QTreeView::handle:vertical{background: #505050}
    To copy to clipboard, switch view to plain text mode 

    I guess what I'm asking for is the syntax...maybe.
    Another thing I would like to do is understand the MODEL system. Im using QFileSystemModel as the model set to my TreeView. I was able to set some filters to get the data I want, but how do I not show the last folders in a directory tree?


    Cheers!
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTreeView and StylingSheets for QTreeView

    It is "QScrollBar" not "QscrollBar".
    http://doc.qt.io/qt-5/stylesheet-exa...ing-qscrollbar provides full examples and comes with the note that if one property or sub-control is customized, all the other properties or sub-controls must be customized as well.

    http://doc.qt.io/qt-5/stylesheet-exa...zing-qtreeview shows how to replace the branch open/closed icon and lines with images in a QTreeView.

  3. #3
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView and StylingSheets for QTreeView

    Thankyou for your answers.
    Would you happen to know how I access the scrollbar for the QtreeView so I can modify it via stylesheet?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTreeView and StylingSheets for QTreeView

    Qt Code:
    1. #include <QApplication>
    2. #include <QTreeView>
    3. #include <QStandardItemModel>
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. QStandardItemModel model(20, 4);
    11. for (int row = 0; row < 20; ++row) {
    12. for (int column = 0; column < 4; ++column) {
    13. QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
    14. model.setItem(row, column, item);
    15. }
    16. }
    17.  
    18. v.setModel(&model);
    19. v.setStyleSheet(
    20. " QScrollBar:vertical {"
    21. " border: 2px solid grey;"
    22. " background: #32CC99;"
    23. " width: 15px;"
    24. " margin: 22px 0px 22px 0px;"
    25. " }"
    26. " QScrollBar::handle:vertical {"
    27. " border: 2px solid red;"
    28. " background: #ffffff;"
    29. " min-height: 20px;"
    30. " }"
    31. " QScrollBar::add-line:vertical {"
    32. " border: 2px solid grey;"
    33. " background: #32CC99;"
    34. " height: 20px;"
    35. " subcontrol-position: bottom;"
    36. " subcontrol-origin: margin;"
    37. " }"
    38. " QScrollBar::sub-line:vertical {"
    39. " border: 2px solid grey;"
    40. " background: #32CC99;"
    41. " height: 20px;"
    42. " subcontrol-position: top;"
    43. " subcontrol-origin: margin;"
    44. " }"
    45. " QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical {"
    46. " border: 2px solid grey;"
    47. " width: 3px;"
    48. " height: 3px;"
    49. " background: white;"
    50. " }"
    51. " QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
    52. " background: none;"
    53. " } "
    54. );
    55. v.show();
    56. return app.exec();
    57. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView and StylingSheets for QTreeView

    Oh...so kind! You didn't have to write the whole thing, you could have just said QScollbar was the object. Hahaha. But Thankyou very much!

    One more thing I can't find, is the Header object for QTreeView. I can GET it via "self.model.HeaderData(0,Qt.Horizontal)", but I can't SET it via "self.model.setHeaderData(0,Qt.Horizontal, SOMEOBJECT HERE)". I don't know what to put as the third argument (SOMEOBJECT HERE) . It asks for a Qobject. What object goes there? Also, what is the styling name for the header in QTreeView so I can access it to style it.

    Thanks again.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView and StylingSheets for QTreeView

    headerData() is a method of the model that your model can overwrite similar to how you overwrite data().

    Cheers,
    _

  7. #7
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView and StylingSheets for QTreeView

    Thanks Anda. I am using HeaderData(), which works, it does return "Name" for index 0. The issue is I can't figure out setHeadData(). It takes in 3 args. An index, the orientation, and an qobject. I don't know what to put as the qobject.
    I was expecting this to work:
    Qt Code:
    1. model.setHeaderData(0, Qt.Horizontal, "Browser")
    To copy to clipboard, switch view to plain text mode 

    But I get an error, something like, "expected QObject, got String"

    I did try this also... ...QObject.tr("Browser") as the 3rd arg, and I didn't get an error, but the header didnt change. It stayed as "Name"
    Last edited by Nfrancisj; 9th July 2016 at 11:32.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView and StylingSheets for QTreeView

    The third argument of QAbstractItemModel::setHeaderData() is a QVariant, not a QObject.

    QObject::tr() obviously does not return a QObject because it returns a QString and a QString is not a QObject.

    Is this model your own model class from the other thread or is this a standard model?
    If it is you model, why don't you simply make headerData() return the values that you want?

    Cheers,
    _

  9. #9
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView and StylingSheets for QTreeView

    Well, I'm not just trying stuff on a whim and hoping it works ... I looked it up and the docs said the data type should be object. The compiler also complains it's NOT a object. I looked up sample code online, and on stackoverflow, and they use object::tr()(in c++). I see that the c++ doc says Variant, but the pyside doc says object. I'll try Variant. This might be a case where c++ and pyside take a different argument type.

    I'm using a qfilesystemmodel to get my source directory. I haven't tried creating my own model yet.

    Thanks again,
    Cheers!

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView and StylingSheets for QTreeView

    Quote Originally Posted by Nfrancisj View Post
    I looked it up and the docs said the data type should be object.
    A Python object could be something entirely different than a QObject.
    Maybe PyQt maps QVariant to soemthing called "object".

    Quote Originally Posted by Nfrancisj View Post
    I looked up sample code online, and on stackoverflow, and they use object::tr()(in c++)
    That creates a translated string.
    A QString can be wrapped automatically into a QVariant, so the compiler can convert the QString into a QVariant when handling that line.
    The example could have also used explicit QString or an int, etc.


    Quote Originally Posted by Nfrancisj View Post
    I'm using a qfilesystemmodel to get my source directory. I haven't tried creating my own model yet.
    I see.

    In case you can't get setHeaderData() to work you could still consider deriving from QFileSystemModel and reimplementing headerData().

    Cheers,
    _

  11. #11
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView and StylingSheets for QTreeView

    Well...no joy! I tried QVariant, but still didn't change the header. When you have a free moment, would you mind trying to create a qFileSystemModel, and try to change the header please? Just wondering if setHeaderData() actually works on a Qfilesystemmodel, or if I'm doing something wrong somewhere. Wondering if the default headers are even editable.

    deriving from QFileSystemModel and reimplementing headerData()
    Can you explain this a bit more please? How do I reimplement headerData() ? Do you mean subClass QFileSystemModel? Or create a QstandardItemModel and copy the info from my QFileSystemModel? is that possible?

    Thanks!
    Cheers!

Similar Threads

  1. Help ...QTreeView
    By Raymond in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2009, 10:18
  2. QTreeView
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2009, 20:51
  3. QTreeView. Qt.4.4.1
    By janEUcitzen in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2008, 09:53
  4. QTreeView help
    By bepaald in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 22:22
  5. QTreeView
    By merry in forum Qt Programming
    Replies: 5
    Last Post: 29th May 2007, 09:38

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.