How could Qt apply style from an external Qt Stylesheet file? - c++

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.

Related

Qt - Create a simple theming system (changing colors of GUI)

I want to be able to create an simple internal theming system for my program (by internal I mean I don't need to be able to load custom themes from user system... all themes will be embedded inside the program itself.)
I can think of some ways:
simply put all color inside the main stylesheet of the program, every time the colors need to change, retrieve main stylesheet, parse and change the colors, and then reapply it again (or have multiple stylesheet with different colors, and apply them when its needed).
Create a top widget that only hold the colors for each widget, and then a child widget that hold other stylesheet for other styling, and all the UI as child of this widget... in this way we need to only change the first stylesheet.
... (not sure about other ways, but I'm sure there is some... maybe using QStyle or something..?)
I'm not sure what is the best way to achieve what I'm after, that will be best at both performance and the code itself...
I know the first way will be heavy in performance side, to each time change the whole stylesheet and rerender the whole program. (I think at least)
the second option though, I'm not sure how it will work, I mean its any better then the first one?!
or there is any other better methods..?
you should create a .qss file and add it in your .qrc like you add your icons.
then write all your stylesheet there. you can have multiple Style.qss files and these files are your themes and whenever you change it your theme will change.
add your Style.qss File in your Resources(.qrc)
write these codes in main.cpp
Load the application style
QFile styleFile(":/style.qss");
styleFile.open(QFile::ReadOnly);
Apply the loaded stylesheet
QString style(styleFile.readAll());
a.setStyleSheet(style);
For more information :
https://github.com/GTRONICK/QSS
https://github.com/ColinDuquesnoy/QDarkStyleSheet

Implementing a custom header bar for chromiumembedded

I have built the chromiumembedded cefsimple project successfully with visual studio. I need to know whether I could implement a custom header bar instead of below.
Eg: with a separate favicon and Title being set as a custom manner .. etc.
Your kind help is really appreciated.
Thank You !
If you are based on CefSimple, take a look at simple_handler_win.cpp. In SimpleHandler::OnTitleChange(), you can set the window title to what you want, instead of what CefSimple wants by changing the SetWindowText() call. This will appear shortly after launch, replacing the app name.
To change the app icons, just replace the icons in the projects cefsimple.rc.
If you want to change them dynamically, that will be a bit more work.

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

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()));

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 do I embed my GUI in Windows explorer?

I want to add an option to explorer (in windows) like the 'preview pane' but only an 'edit pane'. The idea is that I have text files that I want to edit, but I dont want to open an application to edit the file. I want to just click over in the edit pane and make the edits. Any suggestions what API's I can use to extend explorer in this way? (More than just 'look at the shell api: I have and I dont know which will allow me to accomplish this).
You have to implement PropertyHandler.
See Windows SDK \Samples\winui\Shell\AppShellIntegration\PropertyHandlers.
Each property is described by property schema XML file. This property schema must be registered with PSRegisterPropertySchema(). Property handler implements IInitializeWithXXX, IPropertyStore and optionally IPropertyStoreCapabilities. You have to register CLSID of your implementation for each file extension you want to handle (.txt in your case).