Results 1 to 19 of 19

Thread: QTreeView and StylingSheets for QTreeView

  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 10: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!

  12. #12
    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
    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.
    Could very likely be the case that these are not modifyable.
    Hence the suggestion to overwrite headerData().

    Quote Originally Posted by Nfrancisj View Post
    Can you explain this a bit more please? How do I reimplement headerData() ? Do you mean subClass QFileSystemModel?
    Yes.
    Derive from (create a subclass of) the model class, then provide your own headerData() method.

    Cheers,
    _

  13. #13
    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

    QFileSystemModel::headerData() is hard coded to return translated versions of "Name", "Size", "Type" or "Kind" and there is no implementation of setHeaderData(). You could use a simple subclass of QFileSystemModel. Something like:
    Qt Code:
    1. #!/usr/bin/python
    2. # -*- coding: utf-8 -*-
    3.  
    4. import sys
    5. from PyQt4 import QtGui, QtCore
    6.  
    7. class FileSystemModel(QtGui.QFileSystemModel):
    8. def __init__(self):
    9. QtGui.QFileSystemModel.__init__(self)
    10.  
    11. def headerData(self, section, orientation, role):
    12. if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
    13. if section == 0:
    14. result = self.tr('Foo')
    15. elif section == 1:
    16. result = self.tr('Bar')
    17. elif section == 2:
    18. result = self.tr('Baz')
    19. elif section == 3:
    20. result = self.tr('Bob')
    21. else:
    22. result = QtCore.QVariant()
    23. else:
    24. result = super(QtGui.QFileSystemModel, self).headerData(section, orientation, role)
    25. return result
    26.  
    27.  
    28. def main():
    29.  
    30. app = QtGui.QApplication(sys.argv)
    31.  
    32. m = FileSystemModel()
    33. m.setRootPath('/')
    34.  
    35. w = QtGui.QTreeView()
    36. w.setModel(m);
    37. w.resize(640, 480)
    38. w.move(300, 300)
    39. w.setWindowTitle('Simple')
    40. w.show()
    41.  
    42. sys.exit(app.exec_())
    43.  
    44.  
    45. if __name__ == '__main__':
    46. main()
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QTreeView and StylingSheets for QTreeView

    Instead of messing with the QFileSystemModel, I'd like to try and copy the data from it and transfer it to a QStandardItemModel.
    How do I do that? I saw on stackoverflow, there's a method "clone", but i cant seem to find it.

    I'm hoping there's a fancy Q Class/Method that will easily transfer data. .....but I'm guessing that I have to iterate through the model and add them based on the index, rows, and column.

    Cheers!
    Last edited by Nfrancisj; 14th July 2016 at 06:45.

  15. #15
    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

    Instead of making a small adjustment you want to create huge overhead in code and memory usage and manually synchronize between two models?

    Cheers,
    _

  16. #16
    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 really writing anything for production. This is all just to learn how things work, and what's possible. I already know how to subclass, just trying to figure out Models.

    btw, for other newbies who are following, great explanation on models here:
    http://youtu.be/T0HXWcpPItk
    Last edited by Nfrancisj; 14th July 2016 at 16:37.

  17. #17
    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

    ok, but in any real application you would go for either the subclass or a proxy model, never for duplicating data and keeping in manually synchronized.

    Cheers,
    _

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

    Default Re: QTreeView and StylingSheets for QTreeView

    Oh..Proxy Model is something new. Never heard of that.
    But thanks again for all your help guys.

  19. #19
    Join Date
    Oct 2019
    Posts
    7
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView and StylingSheets for QTreeView

    it's work look here : https://qss-stock.devsecstudio.com

Similar Threads

  1. Help ...QTreeView
    By Raymond in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2009, 09:18
  2. QTreeView
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2009, 19:51
  3. QTreeView. Qt.4.4.1
    By janEUcitzen in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2008, 08:53
  4. QTreeView help
    By bepaald in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 21:22
  5. QTreeView
    By merry in forum Qt Programming
    Replies: 5
    Last Post: 29th May 2007, 08: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.