headerData() is a method of the model that your model can overwrite similar to how you overwrite data().
Cheers,
_
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,
_
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.
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,
_
Oh..Proxy Model is something new. Never heard of that.
But thanks again for all your help guys.
Bookmarks