Modify labels at QTreeView
-
Hi all,
I'm doing an application that can handle dynamic translations at runtime. So everything works fine except for the headers in my QTreeView. I'd like to have the opportunity to access them and modify it text accordingly with the language selected but I can't find how.
QPointer<QFileSystemModel> model = new QFileSystemModel; QPointer<NoIconOrExtensionFileDelegate> delegate = new NoIconOrExtensionFileDelegate; QString home = QDir::homePath(); QDir registryFolder{home}; // If registry directory doesn't exist, is created if(!registryFolder.exists(home)) { registryFolder.mkdir(home); } model->setRootPath(home); ui->registryTreeView->setModel(model); ui->registryTreeView->setItemDelegate(delegate); ui->registryTreeView->setRootIndex(model->index(home)); // Hide size and type colums ui->registryTreeView->hideColumn(1); ui->registryTreeView->hideColumn(2); ui->registryTreeView->setColumnWidth(0, 450); ui->registryTreeView->setSelectionBehavior (QAbstractItemView::SelectRows); mJsonOperator.setPath(home);In that piece of code I tried to mess a bit with the header labels with:
model->setHeaderData(0, Qt::Horizontal, "Test"); model->setHeaderData(1, Qt::Horizontal, "Test2"); model->setHeaderData(4, Qt::Horizontal, "Test3"); model->setHeaderData(5, Qt::Horizontal, "Test4");But it doesn't seem to work for me modifying anything. Any help is welcome. Thanks in advance.
Regards.
-
Hi all,
I'm doing an application that can handle dynamic translations at runtime. So everything works fine except for the headers in my QTreeView. I'd like to have the opportunity to access them and modify it text accordingly with the language selected but I can't find how.
QPointer<QFileSystemModel> model = new QFileSystemModel; QPointer<NoIconOrExtensionFileDelegate> delegate = new NoIconOrExtensionFileDelegate; QString home = QDir::homePath(); QDir registryFolder{home}; // If registry directory doesn't exist, is created if(!registryFolder.exists(home)) { registryFolder.mkdir(home); } model->setRootPath(home); ui->registryTreeView->setModel(model); ui->registryTreeView->setItemDelegate(delegate); ui->registryTreeView->setRootIndex(model->index(home)); // Hide size and type colums ui->registryTreeView->hideColumn(1); ui->registryTreeView->hideColumn(2); ui->registryTreeView->setColumnWidth(0, 450); ui->registryTreeView->setSelectionBehavior (QAbstractItemView::SelectRows); mJsonOperator.setPath(home);In that piece of code I tried to mess a bit with the header labels with:
model->setHeaderData(0, Qt::Horizontal, "Test"); model->setHeaderData(1, Qt::Horizontal, "Test2"); model->setHeaderData(4, Qt::Horizontal, "Test3"); model->setHeaderData(5, Qt::Horizontal, "Test4");But it doesn't seem to work for me modifying anything. Any help is welcome. Thanks in advance.
Regards.
@guille31794 If you want translation why aren't you using
tr("Test")etc.? -
@guille31794 If you want translation why aren't you using
tr("Test")etc.?@JonB because that setHeaderData method always return false. Doesn't do anything, and the labels that are set by default aren't translatable, or at least they don't appear in my updated translation files.
-
@JonB because that setHeaderData method always return false. Doesn't do anything, and the labels that are set by default aren't translatable, or at least they don't appear in my updated translation files.
@guille31794
Then I don't know, but maybeQFileSystemModelignores/does not implement anysetHeaderData(). Try overriding QVariant QFileSystemModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const to return what you want instead and verify that works?I don't know much about translation, but did you enter the translations for these texts in whatever file you have to do that?
-
I didn't have much time, but perhaps somebody can figure how to translate my current chunk of code to use a QTreeWidget. Maybe that way (inheriting from QTreeView) I can do what I want with my header labels.
-
I didn't have much time, but perhaps somebody can figure how to translate my current chunk of code to use a QTreeWidget. Maybe that way (inheriting from QTreeView) I can do what I want with my header labels.
@guille31794 said in Modify labels at QTreeView:
@JonB because that setHeaderData method always return false
It isn't an issue of either
QTreeVieworQTreeWidget, it's because you are usingQFileSystemModel.First get it working with your texts without translation. I suggested to you that
QFileSystemModel::setHeader()won't do anything, and that you should instead subclass and do your work in whatheaderData()returns. I see QTreeView / QFileSystemModel set header labels says the same. Did you try this like I suggested? Or don't you have enough time? Once that is done I would hope that from therereturn tr("Test1")does the translation? -
@guille31794 said in Modify labels at QTreeView:
@JonB because that setHeaderData method always return false
It isn't an issue of either
QTreeVieworQTreeWidget, it's because you are usingQFileSystemModel.First get it working with your texts without translation. I suggested to you that
QFileSystemModel::setHeader()won't do anything, and that you should instead subclass and do your work in whatheaderData()returns. I see QTreeView / QFileSystemModel set header labels says the same. Did you try this like I suggested? Or don't you have enough time? Once that is done I would hope that from therereturn tr("Test1")does the translation?Hi,
QFileSystemModel implements headerData and returns specific values for each columns hence your calls to setHeaderData have no effects.
Either make a subclass of QFileSystemModel or make a custom QIdentityProxyModel.