How to create a browser for system drives, folders and files - c++

I want to create as the following:
Unfortunately, Qt does not supported ready widget for that.
Is there is a plugin or any way to do that?

Use QFileSystemModel on a QTreeView. If you look at the first of those two links, it actually contains example code doing exactly that.

would personally suggest not use QWidgets for this task if you can avoid it. Instead, try to utilize the new shiny QML way of building Qt UI. It might be only my personal opition, but QTreeView has several flaws in my opinion.
QML
Here you can find a simple example how it is done with QML these days. It is using the FolderListModel from Qt.labs.folderlistmodel 2.1.
FolderListModel provides access to information about the contents of a folder in the local file system, exposing a list of files to views and other data components.
Note: This type is made available by importing the Qt.labs.folderlistmodel module. Elements in the Qt.labs module are not guaranteed to remain compatible in future versions.
import Qt.labs.folderlistmodel 2.1
The folder property specifies the folder to access. Information about the files and directories in the folder is supplied via the model's interface.
C++ and QWidgets
Should you insist on doing in C++ with the old QWidget set, your choice is probably to use QTreeView as it is a tree view after all and then combine that with QFileSystemModel.
The code would be something like this:
QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(QDir::currentPath());
QTreeView *tree = new QTreeView(splitter);
tree->setModel(model);
tree->setRootIndex(model->index(QDir::currentPath()));

Related

Qt QWidget multiple instances

We are multiple people working on one Qt app. I for one am implementing a library, which will be instanciated in multiple other parts of the app. This library has a display class+form along with it.
Until now I had simply created one single instance of the library, running on a dummy, and passed debug data to one instance of the display+form, and worked like that.
But now that core debugging is finished, goal is to have everything instantiable - not just the core library code, but also the form itself, and embed that form into other displays. Each caller/user would be reponsible for passing output data of the core library instance they are using to an instance of the form. Each instance of the form would separately display the information generated by a specific library instance, possibly with different display options - they are all independent.
Similarly, it is possible to enter values in my display. Goal is to be able to enter different values in different displays instantiated accross the app, and sending those to specific instances (caller's responsiblity).
Question is of course : how to do that ? Internet talks about promoting, but I still don't see anywhere in Qt Designer where to include so-called promoted objects in other objects.
TL;DR : : I want some existing form to appear in the menu on the left in Qt Designer to be able to instantiate it multiple times in other forms. How to do this ?
Thanks in advance for the help !
Charles
You can promote any QWidget to your control from within Qt Designer. Add a QWidget, right-click and promote.
Ideally, you should create a designer plugin for your control, make the relevant properties designable, and build the plugin as well as the library. That way you'll be able to drag your control from the palette, and it will behave like the real thing.
Qt QWidget multiple instances
You answered yourself. Qt Creator: "File->New->Qt->Qt Designer Form Class" with QWidget as a base class will suit you. Then you can promote a simple QWidget in the UI into this custom widget, to create an instance. Each instance will manage its own UI.

Qt: How can I use my widgets created with code in the *.ui file

I have a project in Qt made with the QWizard and QWizardPage classes. There are two ways to create a widget i.e: a Label:
One is going to the *.ui file and search the element and put it where you want (visual way). Then you can access it on your code with ui->nameOfLabel.
The other one is going to your code and creating it like QLabel codedLabel;
Actually I'm using the second way (it's easier for me to create, show and use) but my question is: Is there any way that I can see my label codedLabel on the *.ui file?
I would like to move it to a space in the screen and in that case, it would be much easier for me to be able to do it through the visual way (but having the label created in the code instead of the ui).
Thank you so much.
Widgets created at runtime from your source code and being added to a widget as child CANNOT be seen in Qt Designer when you edit the .ui file of the widget they will be added to.
However, there could be an alternative (reading what you are trying to achieve: having some child widgets being present or not based on the context):
Create the widget from the .ui within Qt Designer and hide it (QWidget::hide()) or even remove it (QLayout::removeWidget()) programmatically if not needed at runtime.
If the real reason why you want to see it is because you want to "move it to a space in the screen and in that case, it would be much easier for me to be able to do it through the visual way". Then I recommend that you simply create an empty QWidget (or QLayout) in Qt Designer (graphically: easy to place where you want to) and later (programmatically) add your QLabel to it (rather than adding it to the main top-level widget): then, it goes in the place you determined from Qt Designer tool.
You should not need any complex code to programmatically display your QLabel in a specific place, just choose the right parent to have it be displayed in the right place!

How to use a QTreeView Model to display a list of files/folders get through mtp stack

I'm currently building an application which need to access to a device through the MTP stack.
I'm developping the code in c++ and Qt to easily be able to port the code on different platform.
I have based the Ui on TreeView to display the folder and ListView to display the list of files of the selected folder.
Most of example are using :
model->setRootPath("F:/Qt/GUI/files");
treeView->setModel(model);
treeView->setRootIndex(model->index("F:/Qt/GUI/files"));
All these models are based on model which accessing to the local HD but in my case, I need to do it through list of folders/files using MTP Api. The backend is working, I mean accessing to the device is ok but I'm still thinkin on how to display the tree of folder and create a model.
Thanks
Please, refer to the Qt Model/View tutorial and Model/View programming.
In general you shall create new model and in the bare minimum override a couple of pure virtual functions.
Since it's a Tree structure, you should subclass QAbstractItemModel.

Creating tabbed document interfaces in Qt Designer?

I am trying to write a program that will use a tabbed document interface (TDI) like seen in Notepad++ or most web browsers. I know how to build GUIs using Qt Designer, and code in Qt C++ (after a few days of playing around).
I have created an example of what each page widget will look like using Designer, and now I want to add the ability to create and testroy tabs at runtime, each containing a unique instance of the page widget. However, I have no idea how to do this without adding a class that extends QWidget, and building the page widget with code. I could go down this route, but I'm sure there must be a better way of creating a TDI; but I can't find any tutorials or examples of how to do this.
Does anyone have any suggestions?
For creating tab interfaces you should look into QTabWidget.
It is a container widget included in Qt Designer which automatically handles operations on tabs. It has several build in methods for manipulating its tabs and theirs contents.
Each page of QTabWidget is handled separately and can have different layouts and functionality.
If you want to include several different objects to one page assign a layout to it and then assign the objects to the layout.

How could Qt apply style from an external Qt Stylesheet file?

I would like the users to be able to customize the default look of our applications by simply loading their OWN Qt Style-sheet files. How do we accomplish that? Can anybody give me a head start?
Say the user have its stylesheet named stylesheet.qss and is located in the application folder.
You could load the style sheet when starting the application, using the -stylesheet argument :
myapp->stylesheet = stylesheet.qss;
But this require your user to know how to start an application with arguments.
What you could also do is to add a settings dialog in your app, where the user can choose a stylesheet path.
You can then open this file, load the content, and set it to your application with QApplication::setStyleSheet() :
QFile file("stylesheet.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
qApp->setStyleSheet(styleSheet);
Qt is providing an example online which might be helpful.
You just set the style sheet for the entire application based on configuration provided by the customer.
http://doc.qt.io/qt-5/qapplication.html#styleSheet-prop
You could set/get this configuration from any number of places, a properties dialog in the application is probably the most natural approach.