Thankyou for your answers.
Would you happen to know how I access the scrollbar for the QtreeView so I can modify it via stylesheet?
Thankyou for your answers.
Would you happen to know how I access the scrollbar for the QtreeView so I can modify it via stylesheet?
Qt Code:
#include <QApplication> #include <QTreeView> #include <QStandardItemModel> int main(int argc, char *argv[]) { for (int row = 0; row < 20; ++row) { for (int column = 0; column < 4; ++column) { model.setItem(row, column, item); } } QTreeView v; v.setModel(&model); v.setStyleSheet( " QScrollBar:vertical {" " border: 2px solid grey;" " background: #32CC99;" " width: 15px;" " margin: 22px 0px 22px 0px;" " }" " QScrollBar::handle:vertical {" " border: 2px solid red;" " background: #ffffff;" " min-height: 20px;" " }" " QScrollBar::add-line:vertical {" " border: 2px solid grey;" " background: #32CC99;" " height: 20px;" " subcontrol-position: bottom;" " subcontrol-origin: margin;" " }" " QScrollBar::sub-line:vertical {" " border: 2px solid grey;" " background: #32CC99;" " height: 20px;" " subcontrol-position: top;" " subcontrol-origin: margin;" " }" " QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical {" " border: 2px solid grey;" " width: 3px;" " height: 3px;" " background: white;" " }" " QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {" " background: none;" " } " ); v.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
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.
headerData() is a method of the model that your model can overwrite similar to how you overwrite data().
Cheers,
_
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:
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.
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,
_
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!
A Python object could be something entirely different than a QObject.
Maybe PyQt maps QVariant to soemthing called "object".
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.
I see.
In case you can't get setHeaderData() to work you could still consider deriving from QFileSystemModel and reimplementing headerData().
Cheers,
_
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.
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?deriving from QFileSystemModel and reimplementing headerData()
Thanks!
Cheers!
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:
#!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import QtGui, QtCore class FileSystemModel(QtGui.QFileSystemModel): def __init__(self): QtGui.QFileSystemModel.__init__(self) def headerData(self, section, orientation, role): if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole: if section == 0: result = self.tr('Foo') elif section == 1: result = self.tr('Bar') elif section == 2: result = self.tr('Baz') elif section == 3: result = self.tr('Bob') else: else: result = super(QtGui.QFileSystemModel, self).headerData(section, orientation, role) return result def main(): m = FileSystemModel() m.setRootPath('/') w.setModel(m); w.resize(640, 480) w.move(300, 300) w.setWindowTitle('Simple') w.show() sys.exit(app.exec_()) if __name__ == '__main__': main()To copy to clipboard, switch view to plain text mode
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.
Instead of making a small adjustment you want to create huge overhead in code and memory usage and manually synchronize between two models?
Cheers,
_
Bookmarks