QFileSystemModel and QTreeView - strange behavior when resetting view - c++

I wrote this on official forums of Qt, but it seems dead, so I am going to copy-paste it here.
I am writing small program for copying files. I use QTreeView and I have inherited from QFileSystemModel, so I was able to add checkboxes to every row in the QTreeView. I also use setNameFilters method connected with QLineEdit, so user can specify what file extensions he wants to display in the QTreeView. I have spotted the following behavior:
1) When I run the program and enter extensions to filter (without touching any node from the QTreeView) everything works fine and files with extensions I have provided are only displayed (and folders of course). When I change the extensions and the view is refreshed, on my "C:/" drive everything is updated and only new set of extensions is displayed. When I expand some other drive that I didn’t touch before, it also shows files correctly.
2) When I run the program and expand let say my "C:/" and "D:/" drives I see all directories and files (expected behavior). Then I write some extensions and the view is refreshed. I expand "C:/" drive and everything works fine, only files with extensions I have provided are displayed. Then I go to "D:/" drive and here is the problem. It displays all files. It ignores the filters I have provided. When I open the "E:/" drive that I have not opened before, the files are filtered correctly as in "C:/" drive.
I have concluded, that this behavior has something to do with setRootPath method, because for my QTreeView only in "C:/" drive the filters are working correctly. All other drives that were expanded before change of filters don’t work. Those not expanded work just fine.
The question is: How to get this working, so after user changes the filters and reset() method is fired, the whole QTreeView is refreshed and not only root path and not-expanded elements? Maybe there exists some root path that have all the drives as children and it will work as expected? Or maybe I should make some virtual folder in the QTreeView called "MyComputer" and set it to be a parent for all the drives? But how to get list of all the available drives?
I hope that what I wrote is clear for you and you can help me to get this working.
Edit:
Adding some code that is relevant. If you need more just ask.
//setting up the model and view
QString rPath = "C:/";
rTree_model = new TreeModel(this); //TreeModel inherits from QFileSystemModel
rTree_model->setRootPath(rPath);
ui->rTree->setModel(rTree_model); //applies the model for the qtreeview (ui->rTree)
//(...)
//action when extensions were provided by user
QString extensions = QString(ui->extensionBox->text()); //gets extensions provided by user
QStringList filters;
if(extensions.length() > 0) {
filters = extensions.split(";", QString::SkipEmptyParts); //splits extensions provided with ';' as separator
rTree_model->setNameFilters(filters); //applies filters
ui->rTree->reset(); //resets the view
}

Try changing your root path to My Computer instead of C:/. It seems to work with QFileSystemModel in Windows 7 x64 and Qt 4.8.2, but I can't guarantee anything for other platforms.
rTree_model = new TreeModel(this);
QString rPath = model->myComputer().toString(); //causes the QFileSystemWatcher to watch every drive?
rTree_model->setRootPath(rPath);
ui->rTree->setModel(rTree_model);

Related

QT 5 - load images from the same folder as the stylesheet (.qss)

In my QT app I allowed users to load .qss stylesheets as a custom theme.
This however is problematic since they could not load images in the styles initially.
After a bit of digging it happens that relative paths in the stylesheet are resolved towards the current working dir, and not towards the stylesheet path (which QT cannot know since we load it as a string).
So the only "hack" that I could come up with is to change the current working path to the folder where the stylesheet is located, like so:
if (KreatorSettings::instance().customTheme().isNull() == false) {
QString qsFileName(KreatorSettings::instance().customTheme());
QFile qfFile(qsFileName);
if (qfFile.exists()) {
if (qfFile.open(QFile::ReadOnly | QFile::Text)) {
QDir::setCurrent(QFileInfo(qsFileName).absolutePath()); // needed
qApp->setStyleSheet(QLatin1String(qfFile.readAll()));
loadedCustom = true;
}
}
This is an example of how the images are defined in the stylesheet:
QCheckBox::indicator:checked
{
image:url(icon_checkbox_checked.png);
}
This is obviously not the right way, since I need to keep that current working path for the "skin" to work.
Is there a way in qss to use path relative to the stylesheet or some other method to use paths that do not require either embedding resources or changing the current working dir? Maybe we can tell QT what is the stylesheet root folder so it resolves from there? I have no clue, so need your help.
I would hope there is something like in CSS, or else the QT team made some major mistake in the design making the *.qss files unfit for user customization.

How to load multiple font of same familiy

I am writing an application using the Qt framework. In the display, I have to show multiple information, but using different types of font of the same family, Montserrat.
What I have done so far to load the fonts is:
int ultralightid = QFontDatabase::addApplicationFont(":/Montserrat_UltraLight.tff");
QString UltraFont= QFontDatabase::applicationFontFamilies(ultralightid ).at(0);
QFont font1(UltraFont,QFont::Normal);
font1.setPixelSize(50);
int lightid = QFontDatabase::addApplicationFont(":/Montserrat_Light.tff");
QString LightFont= QFontDatabase::applicationFontFamilies(lightid).at(0);
QFont font2(LightFont,QFont::Normal);
font2.setPixelSize(150);
label1->setFont(font1);
label2->setFont(font2);
label1->setText("bla bla");
label2->setText("bla bla");
The font sizes are correct, but the font itself it is not. From what I have noticed (trying with Hairline_Montserrat,Light_Montserrat,UltraLight_Montserrat), it is as if the fonts have a sort of priority. If I declare them all, all the fonts are the Light one, if I comment that font type, all of them are Hairline one, otherwise (last priority) the labels use the ultralight font.
I have tried adding other font type (from other families) and in that case my code works correctly.
If I use
qDebug()<<QFontDatabase::applicationFontFamilies(ultralightid);
qDebug()<<QFontDatabase::applicationFontFamilies(lightid);
both of them print the family "Montserrat".
I use the qrc file and the AUTORCC flag in the CMAKE (it should be similar using qmake) and all the file are uploaded correctly.
Do you know if there is another way to add fonts of the same family? Or is there something I am doing wrong?
Here are the fonts:
https://www.onlinewebfonts.com/download/9d31c906a6cc6064bbe7d33d51058317 light
https://it.allfont.net/download/montserrat-light/ ultralight
This is an old question but I was just struggling with exactly the same problem when trying to load normal, bold, ... versions of a font family in Qt.
I solved the problem (although in a somewhat hacky way) by simply giving each of the ttf files a different family name. I used Typograf, simply open the font, right click to open properties and then click rename. There are probably many other tools that do this too.
You don't need to manage font files from one family separatelly.
I suggest this solution:
Create a folder with all ttf's of the same family.
Load all files from the folder via id = QFontDatabase.addApplicationFont(path)
Collect all font families from these files via QFontDatabase.applicationFontFamilies(id)
Check if only one and desired family is loaded, and the family name is exactly the same as requested, or warn the user about these errors.
Create font object font = QFont(family)
Then for example, font.setItalic(True). If Italic version of family is loaded, it will be used, otherwise it will be created from Regular by QT.

Filtering based on file extension not working in QFileSystemModel?

I am trying to filter file system model to show only files with extension .ncr (NCReport templates). The view instead shows all files. Any ideas how to make the filtering work? Thanks.
(I realize there's plenty other clumsiness here, suggestions welcome.)
fsmodel = new QFileSystemModel(this);
connect(fsmodel,SIGNAL(rootPathChanged(QString)),this,SLOT(fileSystemModelRootSetSuccessfully()));
// this call is required on windows to show anything in file view
QModelIndex rootIndex=fsmodel->setRootPath(reportdirstring);
// root index could be removed
Q_UNUSED(rootIndex);
fsmodel->setReadOnly(true);
ui->reportTemplateDirView->setModel(fsmodel);
ui->reportTemplateDirView->setRootIndex(fsmodel->index(reportdirstring));
ui->reportTemplateDirView->expandAll();
ui->reportTemplateDirView->header()->hide();
// selecting the first file entry with selectFileInDirView(); requires the qtreeview to be sorted
// sort in desc order since that is the only way to get the first item selected?
ui->reportTemplateDirView->sortByColumn(0,Qt::AscendingOrder);
fsmodel->sort(0,Qt::AscendingOrder);
QStringList filters;
filters << "*.ncr";
fsmodel->setNameFilters(filters);
fsmodel->setNameFilterDisables(false);
// hide report template directory view extra columns,
//type?
ui->reportTemplateDirView->setColumnHidden(1,true);
//size?
ui->reportTemplateDirView->setColumnHidden(2,true);
//date
ui->reportTemplateDirView->setColumnHidden(3,true);
#if QT_VERSION >= 0x040700
// as soon as QFileSystemModel has parsed the entire directory tree, tell the QTreeView to expand its hierarchy
// note that if there are a lot of files, this could be too inefficient.
// if problems arise, consider commenting this out or using a QDirModel, which could be equally inefficient though.
connect(fsmodel,SIGNAL(directoryLoaded(QString)),ui->reportTemplateDirView,SLOT(expandAll()));
connect(fsmodel,SIGNAL(directoryLoaded(QString)),this,SLOT(selectFileInDirView()));
#endif
// show a fake folder name + icon at the top of the folder tree of report template directory
QFileIconProvider iconProvider;
QIcon folderIcon=iconProvider.icon(QFileIconProvider::Folder);
ui->reportTemplatesLabel->setPixmap(QPixmap(folderIcon.pixmap(QSize(16,16))));
As Alexander noted, this actually works. The issue was that I was setting the filters to empty elsewhere. ^.^

QT QIcon properties for custom widget in designer

I have been working for a little while now on creating a QT custom designer widget for GUI menus. The idea being that you simply drag it into the designer, select the number of frames you'd like, how many buttons per frame, etc. and it generates and sizes everything for you.
The way the widget is structured there are properties to configure each button for the frame you are in. For example, you would use the button0Text field to enter text under Button0 while editing in frame 0, then use it again to edit Button0 which is in frame 1. Both buttons would retain the individual changes for each frame.
The Problem
Normally when I switch frames all of my properties are updated to reflect the status of the frame. The exception being QIcon. The correct icon is retained in the actual graphical representation and builds correctly, however the file path in the property list is always of the last edited for that property. I think this will be extremely confusing to an end user and I have found no way to fix it. So for example, if I set text and icons in frame 0 then switch to frame 1 the text in the property list will update to reflect the state of frame 1 but the icon path names will still show my last edit in frame 0 and not the actual icon in frame 1.
I have tried things as simple as:
setProperty("button0Icon", getButton0Icon());
That code works on properties like text, but not for the icon. I try executing it immediately after changing frames.
I have also tried:
#ifndef Q_WS_QWS
QDesignerFormWindowInterface *form = QDesignerFormWindowInterface::findFormWindow(this);
if(form){
QDesignerFormEditorInterface *editor = form->core();
QExtensionManager *manager = editor->extensionManager();
QDesignerPropertySheetExtension *sheet;
sheet = qt_extension<QDesignerPropertySheetExtension*>(manager, this);
int propertyIndex = sheet->indexOf("button0Icon");
sheet->setChanged(propertyIndex, true);
sheet->setProperty(propertyIndex, getButton0Icon());
}
#endif
And:
int propertyIndex = this->metaObject()->indexOfProperty("button0Icon");
QMetaProperty property = this->metaObject()->property(propertyIndex);
property.write(this, QIcon());
Nothing seems to update the property list in the designer.
I have all properties, including all QIcon properties properly declared in the header file with Q_PROPERTY and assigned getter and setter functions.
To be clear, the icon values are indeed retained through each frame when compiled. So it is functioning, just unclear for most users.
If anyone has any experience with this or any ideas please let me know. Thanks.
I have discovered that QIcon does not store file names/paths. The file names are only used for the creation of the QIcon. I think this is most likely the reason I do not get any feedback in the Property Browser for my QIcon properties.
Instead I have chosen to hide this property in the designer and add three new ones. Three QUrl properties, each of which is used to supply an image file. I use three because I want to construct a QIcon that contains Modes/States for normal, disabled, and pressed operations.
I take each of these QUrls and save them in QStringLists behind the scenes so their values are stored. I then construct my QIcon using the file names provided from the QUrls.
I would much prefer to be using the native QIcon in the designer for this, any thoughts or feedback are appreciated.

How to update file permissions in a QFileSystemModel

Is there a way to update a file's permissions in QFileSystemModel (c++)? Prior to allowing a user to rename a file listed in the model using a qtreeview, I make sure the file gets checked out of source control. At this point the file is no longer read only, but the model still believes it's read only. How can I force the model to update a file's permissions without losing the expand / collapse state of the tree?
Thanks!
Update:
The file is already flagged as writeable after checking out the file. The Model remains unaware of the change though.
QFile file(path.c_str());
QFileDevice::Permissions perms = file.permissions();
if (perms & QFile::WriteUser)
{
// Is already true
}
Just to be sure, I went ahead and used
file.setPermissions(file.permissions() | QFile::WriteUser);
with no luck changing the permissions reported for that file in the model.
Update:
int perms = fsModel->data(index, QFileSystemModel::Roles::FilePermissions).value<int>();
if (perms & QFile::WriteUser)
{
int i = 0;
}
Note: the above permissions never has the QFile::WriteUser flag set unless the file was writeable before the model was created.
setRootPath() is the key to solving this as well. It seems that you have to call it twice to get it to update the read only permissions. I stumbled across this when I changed my selection code to call:
m_pFileModel->setRootPath("");
m_pFileModel->setRootPath(path.c_str());
everytime an item was selected. Then when I doubleclicked an item, I saw the icon change to checked out. Granted it didn't immediately let me rename it, I had to double click it again, but it does work.
My Process:
Connect to the OnBeginEdit() signal and checkout the file / change permissions
When an item is selected:
m_pFileModel->setRootPath("");
m_pFileModel->setRootPath(path.c_str());
Inside OnBeginEdit()
Do the following TWICE if you didn't set the path to the current folder when the item was selected
m_pFileModel->setRootPath("");
m_pFileModel->setRootPath(path.c_str());
Keep in mind you will have to doubleclick twice or press F2 twice - once to checkout and second to actually change the file.