Filtering in QFileDialog - qt

I would like to filter the files that are shown in a QFileDialog more specifically than just by file extensions. The examples I found in the Qt documentation only show filters like Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml) and such. In addition to this I would also like to specify a filter for files that should not show up in the file dialog, e.g. XML files (*.xml) but not Backup XML files (*.backup.xml).
So the problem I have is that I would like to show some files in the file dialog that have certain file extension, but I would not like to show other files with a specific file name suffix (and the same file extension).
For example:
Files to show:
file1.xml
file2.xml
Files not to show:
file1.backup.xml
file2.backup.xml
I would like to ask if it is possible to define filters like these for a QFileDialog?

I believe what you can do is:
Create a custom proxy model. You can use QSortFilterProxyModel as a base class for your model;
In the proxy model override the filterAcceptsRow method and return false for files which have the ".backup." word in their names;
Set new proxy model to the file dialog: QFileDialog::setProxyModel;
Below is an example:
Proxy model:
class FileFilterProxyModel : public QSortFilterProxyModel
{
protected:
virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
};
bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
return fileModel->fileName(index0).indexOf(".backup.") < 0;
// uncomment to call the default implementation
//return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
dialog was created this way:
QFileDialog dialog;
dialog.setOption(QFileDialog::DontUseNativeDialog);
dialog.setProxyModel(new FileFilterProxyModel);
dialog.setNameFilter("XML (*.xml)");
dialog.exec();
The proxy model is supported by non-native file dialogs only.

The solution of #serge_gubenko is working well. Create your own ProxyModel by inheriting from the QSortFilterProxyModel.
class FileFilterProxyModel : public QSortFilterProxyModel
{
protected:
virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
};
bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
// Your custom acceptance condition
return true;
}
Just make sure to set DontUseNativeDialog before setting the Proxy model (Edit: #serge_gubenkos answer does that now). Native dialogs do not support custom ProxyModels.
QFileDialog dialog;
dialog.setOption(QFileDialog::DontUseNativeDialog);
dialog.setProxyModel(new FileFilterProxyModel);
dialog.setNameFilter("XML (*.xml)");
dialog.exec();
It took quite some time for me to find this out. This was written here

Okay, I've used it with QFileDialog object. And this only shows me the files listed in the appropriate directory. It is excellent to just choose the files to be processed. For example, an XML file, a PNG image, etcetera.
Here I present my example
OlFileDialog QFileDialog (this);
QString slFileName;
olFileDialog.setNameFilter (tr ("Files (* xml)"));
olFileDialog.setFileMode (QFileDialog :: anyfile);
olFileDialog.setViewMode (QFileDialog :: Detail);
if (olFileDialog.exec ())
olFileDialog.selectFile (slFileName);
else
return;
The dialog box will display only presents xml files.

Related

Understanding QML_INTERFACE

I've a really hard time understanding what the QML_INTERFACE and QML_IMPLEMENTS_INTERFACES macros are supposed to do. Naively I assumed that the macro does what it says it does, registering a non-instantiable ("uncreatable") type to the QML type system. A type I can then use as a custom property to bind to a specific implementation.
Let's get concrete (no pun intended) and elaborate with a simple example, an interface with a single QString property:
class QmlInterface : public QObject {
Q_OBJECT
QML_INTERFACE
Q_PROPERTY(QString name READ get WRITE set NOTIFY changed)
public:
virtual ~QmlInterface();
virtual QString get() const = 0;
virtual void set(QString const& name) = 0;
signals:
void changed();
};
Q_DECLARE_INTERFACE(QmlInterface, "QmlInterface")
And a class which implements that interface (declarations omitted for simplicity):
class QmlImplA : public QmlInterface {
Q_OBJECT
QML_ELEMENT
QML_IMPLEMENTS_INTERFACES(QmlInterface)
public:
QString get() const final;
void set(QString const& name) final;
private:
QString name_;
};
An instance of this implementation is then created in C+ and an interface pointer gets passed to the QML engine right before loading the .qml file
QQmlApplicationEngine engine;
// url to .qml file
std::unique_ptr<QmlInterface> qml_impl_a{std::make_unique<QmlImplA>()};
engine.setInitialProperties({{"qml_impl_a", QVariant::fromValue(qml_impl_a.get())}});
engine.load(// url);
I then assumed I could just import the interface in my .qml file and use it with the underlying implementation like this:
import QtQuick
import QtQuick.Controls
import QmlInterface
ApplicationWindow {
required property QmlInterface qml_impl_a
// ...
}
However this doesn't work. QML complains that QmlInterface is not a type
Upon googling for some QML_INTERFACE examples I found a passage in Cross-Platform Development with Qt 6 and Modern C++ which says that:
QML_INTERFACE registers an existing Qt interface type. The type is not instantiable from QML, and you cannot declare QML properties with it.
Wait... what? Is this true? The official documentation doesn't mention this anywhere. As this would practically render interfaces completely useless I assume the book is wrong here?
For now I got my example working by making the interface a normal QML_ELEMENT and adding QML_UNCREATABLE to prevent instantiation from inside QML. This works and does what I intended in the first place. I still wonder though did I do something wrong or are the macros supposed to do something else entirely and are just named badly?

How to make a Qt QML variable accessible anywhere

I would like to have a qt QML var accessible globally, and anywhere else in my qml files. Is this possible?
I know that upon creating a variable in a C++ object can be accessed in QML by exposing its getter function, but this only works if you know the type of the data type e.g. string, int, bool.
Is there a variable data type (or class) that can handle a QML var in C++, so that I can only call it in the other parts of the QML files?
AS Amfasis said, you can use the rootContext, so you can access it from anywhere in QML - as long as you do not shadow the name. Alternatively you can also register a Singleton to QML.
For both, you first need to create a QObject
public class MyContextObject: public QObject {
Q_OBJECT
Q_PROPERTY(QVariant myVar READ myVar NOTIFY myVarChanged)
QVariant m_myVar;
public:
MyContextObject(QObject* parent = nullptr) : QObject(parent) {}
QVariant myVar() { return m_myVar; }
void setMyVar(QVariant var) {
if (var == m_myVar) return;
m_myVar = var;
emit myVarChanged();
}
signals:
void myVarChanged();
}
This object you create in your main and set it as a contextProperty
MyContextObject* mctx = new MyContextObject();
view.rootContext()->setContextProperty("myCtx", mctx);
To set it from C++ use the setter. On the QML-side just bind to myCtx.myVar
Expose the setter also, if you want to modify it from QML also
This is not tested, I don't have a Qt development environment available right now.
To expose it as singleton, you can use this function:
https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType-1

Getting the index for a given item in a QTreeView / Model application

My Qt desktop app was built on the editabletreemodel sample application. In the sample application in treemodel.cpp there is a mthod called getItem() which takes a QModelIndex as a parameter and returns a pointer to a TreeItem.
This is what the method looks like:
TreeItem *TreeModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item)
return item;
}
return rootItem;
}
I would like to do the reverse - that is return a QModelIndex for a given TreeItem.
How can I do this?
Difficult... very difficult. And directly not possible. And if you use the TreeItem as in the Qt example code you are out of luck. What you can do is:
Add a unique identifier to your TreeItem
Modify your data function to get this unique id for a user defined role. You are the user who has to define this new role ;-)
Use **QModelIndexList QAbstractItemModel::match(...) to find the index for the TreeItem, which returns the unique id for your user role.

Beginning Qt Model / View with Nested Object Collections

I'm primarily a .Net developer and have been investigating Qt for a while now. I'm now at the stage of trying to implement the model / view framework in Qt. I think I have a grasp of the basic principles but am unclear of how to hang things together in a more complex UI where widgets need to communicate with each other. Given the following:
// 'domain' model classes
class NestedDomainModel1
{
public:
NestedDomainModel1();
QString name() const;
void setName(const QString& newName);
// other properties
private:
QString m_name;
};
class NestedDomainModel2
{
public:
NestedDomainModel2();
QString name() const;
void setName(const QString& newName);
// other properties
};
class MyDomainModel
{
public:
MyDomainModel();
void addNestedModel1(const NestedDomainModel1& modelToAdd);
NestedDomainModel& nestedObjectModel1At(int index);
int nestedObjectModel1Count() const;
// repeat for model 2
private:
QList<NestedDomainModel1> m_nestedModels1;
QList<NestedDomainModel2> m_nestedModels2;
};
// 'GUI' classes
class MainWindow : public QMainWindow
{
private:
MyDomainModel* m_model;
MyTreeViewWidget* m_treeWidget; // -> this sits in a left dock window
MyInfoDisplayWidget* m_infoWidget; // -> this sits in a right dock window and display details about the item selected in the tree
};
class MyDomainModelTreeModel : public QAbstractItemModel
{
public:
explicit MyDomainModelTreeModel(MyDomainModel* model);
// required overrides for QAbstractItemModel
private:
MyDomainModel* m_model;
};
class MyTreeViewWidget : public QWidget
{
public:
// Take a pointer to the domain model and create a model for the 'view'.
// Will create a tree like:
// Nested Objects 1
// |- object 001
// |- object 002
// |- you get the idea
// Nested Objects 2
// |- other object 001
// |- more of the same
explicit MyTreeViewWidget(MyDomainModel* model);
public slots:
// Used to notify widget when an item is added to the underlying model.
void nestedModel1Added();
void nestedModel2Added();
signals:
void nestedModel1Selected(NestedDomainModel1& selectedModel);
void nestedModel2Selected(NestedDomainModel2& selectedModel);
private slots:
// connect to tree view event when an item is selected and if all ok, emit one of the selected events
void onTreeItemSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
private:
QTreeView* m_treeView;
MyDomainModelTreeModel* m_treeModel;
};
class MyNestedClass1ViewModel : QAbstractItemModel
{
public:
explicit MyNestedClass1ViewModel(NestedDomainModel1* model);
setModel(NestedDomainModel1* model);
// required overrides for QAbstractItemModel
private:
NestedDomainModel1* m_model
};
class MyInfoDisplayWidget : public QWidget
{
public:
explicit MyInfoDisplayWidget(QWidget* parent = 0);
public slots:
// this is connected to the 'tree' widget signal in MainWindow
void setModel(NestedDomainModel1& selectedModel);
};
The basic premise of the UI is something similar in feel to Visual Studio. The tree is similar to the Solution Explorer and the 'info display' is similar to the properties window.
Is this how you use the model / view framework? For those familar with WPF / Silverlight development, is the model / view framework similar to MVVM (at a high level) in that it is the 'model of the view' and wraps / contains the domain model?
Is this how you connect the widgets using the model / view framework (ie. one widget passes a pointer or reference of the model to another)? Or should I be using the SelectionModel? Does that work since the tree model contains different types of objects?
How do you identify the root nodes? For instance, when a MyNestedObject1 is created and needs to be added to tree do I rely on the knowledge that root node is at a model index QModelIndex(0, 0) (ie. row 0 with an invalid parent index)?
I'm finding the terminology you're using a bit awkward, for example MyNestedClass1ViewModel is just a model. I'm not sure what a ViewModel would be.
What you're missing in this example is an actual view. MyTreeViewWidget is just a dumb widget that isn't actually a view in Qt terms at all, it's essdentialy just a dumb 'canvas' that you want to display data in. So the way to do this is:
You have underlying data in ordinary objects such as NestedDomainModel2. These are not Models in the Qt sense though and I wouldn't name them as such. They're just ordinary objects and don't implement any of the MVC interfaces.
Your MyNestedClass1ViewModel, which is a Qt model class. It accesses the underlying data objects above (1) in the implementation of it's data() and setData() methods.
A view class subclassed from QAbstractItemView. This is what you're missing. It has all the magic hooks to plug into the API of the model class from (2) above. It gets signals from the model telling it when there have been changed, which invoke methods such as dataChanged(), rowsInserted(). You implement these methods to make appropriate changes in your display widget below in point (4).
Your display widget. It doesn't implement any of the model/view API itself and is updated by your view. If it's interactive and can be used to change model data, you do that by calling setData(), insertRows(), removeRows(), etc on the model. The display changes will automatically propagate back to the widget via the view. Be careful not to generate infinite loops of changes propagating from widget->model->view->widget->model->view etc.
I have done a similar thing to use a QGraphicsScene/QGraphicsView to display items in a model. Despite it's name QGraphicsView isn't part of the model/view framework, so I implemented a custom view class which drew the model data on the QGraphicsScene.
Here's my code, in Python. It draws worlds on a map for an SF wargame:
class WorldItemView(QtGui.QAbstractItemView):
""" Hidden view which interfaces between the model and the scene.
"""
def __init__(self, model, parent=None):
QtGui.QAbstractItemView.__init__(self, parent)
self.hide()
self.setModel(model)
self.my_model = model
self.scene = MapScene(self.my_model)
self.resetWorlds()
def dataChanged(self, topLeft, bottomRight):
top_row = topLeft.row()
bottom_row = bottomRight.row()
#debug_log("Top row " + str(top_row) + " Bottom row " + str(bottom_row))
for row in range(top_row, (bottom_row + 1)):
self.scene.worldChanged(row)
def rowsInserted(self, parent, start, end):
for row in range(start, (end + 1) ):
pmi = self.my_model.getPMI(row)
self.scene.insertWorld(pmi)
def rowsAboutToBeRemoved(self, parent, start, end):
for row in range(start, (end + 1)):
self.scene.removeWorld(row)
def resetWorlds(self):
self.scene.clearWorlds()
# Add worlds to scene
last_row = self.my_model.rowCount() - 1
self.rowsInserted(None, 0, last_row)
I hope that helped.

Detecting missing resources in QT Stylesheets

I am using QCSS stylesheets in QT to skin several buttons with images from the QT resource system:
QFrame#DialogButtonTitle_SaveAsNew
{
background-image: url(images:DialogButtonTitle_SaveAsNew.png);
}
This works great, but I would really like to write a warning to our logs if the image file referenced from the CSS could not be found (and the button is thus naked). Any way to catch such errors?
I believe you can do it like this:
Read about the QAbstractFileEngine and QAbstractFileEngineHandler classes.
Extend Qt's own implementation, QFSFileEngine. I believe it also handles the ":" namespace.
Reimplement the QAbstractFileEngine::open() method.
Register your engine using a custom QAbstractFileEngineHandler. The create() method should check the file name to see if it is being read from a resource file.
Haven't tested, but I think it should work. Code:
bool MyEngine::open(QIODevice::OpenMode mode)
{
bool r = QFSFileEngine::open(mode);
if (!r) {
qWarning() << "Failed to open" << fileName();
}
return r;
}
QAbstractFileEngine *MyEngineHandler::create(const QString &fileName) const
{
return fileName.startsWith("images:") ? new MyEngine(fileName) : 0;
}
Edit.
This will not work. The resource file system, “:”, is handled by a private file engine called QResourceFileEngine, not by QFSFileEngine.
Based on #andref answer, I came up with this, which works for me (TM):
class LoggingEngineHandler : public QAbstractFileEngineHandler
{
public:
LoggingEngineHandler()
: QAbstractFileEngineHandler()
, m_lookUpInProgress(false)
, m_lookUpPaths(QRegExp("^(images|meshes|app|sounds):"))
{
// empty
}
QAbstractFileEngine* create(const QString &fileName) const override
{
if (!fileName.contains(m_lookUpPaths))
return 0;
if (m_lookUpInProgress)
return 0;
m_lookUpInProgress = true;
QFileInfo info = QFileInfo(fileName);
m_lookUpInProgress = false;
if (!info.exists())
{
assert(!Utilities::isRunByUser("designer"));
LOG_WARN("Required resource file does not exist: %1%", QUtil_s(fileName));
}
return 0;
}
protected:
mutable bool m_lookUpInProgress;
QRegExp m_lookUpPaths;
};
It's possible that Qt will call one of their message functions when something like this happens (although I don't know for sure). If it does, you could install a message handler function and append some or all of the messages to your log file. There is some information about doing so in the documentation for qInstallMsgHandler.

Resources