How record Qt Widgets StyleSheet in a sepatated file - qt

Is it possible to get the stylesheet from a separated file for a Qt Widgets application, like .CSS does to .HTML ?

Yes, it is possible. You may load any file with QFile and use setStyleSheet method in your application or any widget. by #SaZ

Related

Qt Designer UI template files

I recently started playing with Qt and PyQt, then I learned about Qt Designer and the .ui files they generate in xml format.
I was wondering if there is a resource website for downloading the template .ui files? That would make life much easier to start with.
As i know, it's not such practice in Qt to make template .ui files and then reuse them. If you want to reuse some ready UI components you can make Qt Designer Plugin and then use it. See documentation: Adding Qt Designer Plugins
There's no point to template .ui files because the only truly reusable element in them is styling. Layouts etc. are very application-specific. Certainly Qt style .css/.qss templates make sense. If any exist, you will find them with Google.

Load an external stylesheet in QT from another external stylesheet

I'm new to QT and I'm trying to see if it's possible to load an external stylesheet from a global stylesheet just like in web dev, something like using the syntax "link rel="stylesheet".
Right now we have a global CSS sheet that is being called via C++ code with all the global elements and specific widget modifications, it has 2k lines of code and it's becoming unmanageable. I know I can keep the global file and start adding resource stylesheets or pure code put directly into QT Creator Style Sheet editor for each widget. However, the head of development really wants to separate that my code from his team, so avoid any changes in QT UI file as much as possible, so if something breaks he will know when it was me or him.
Is this possible? can I call a stylesheet from a stylesheet? I couldn't find this syntax in the QT guide.

qt how can I reload the resources files

I'm using stylesheet for my new widget. I want to add a reload button just for designing. So I add a stylesheet.txt in Resouces/.../xxx.qrc file as the style sheet to apply for my widget. And I have a QPushButton to trigger setStyleSheet() with a QFile to open the stylesheet.txt. And I want to edit the txt outside the program with defaut editor in system. But I found that the resources files are not refreshed which means when I edit the txt, the txt doesn't reload in the program. Any idea how can I reload the file or any solution, please?
Resources are embedded into the binaries upon compilation. If you edit one of the resource files you'll have to rebuild the application. So, if you want dynamically change styles without recompiling/restarting, just load style-sheet from an external file, but not from resources.
From the Qt Doc:
"The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable. This is useful if your application always needs a certain set of files (icons, translation files, etc.) and you don't want to run the risk of losing the files."
The resources are stored in the binaries, you can only update them if you rebuild your application. Use a other file to load your stylesheet.
General suggestion: do not put resources in .qrc during debug / design. I recommend to use a QDir::setSearchPaths instead:
void Application::setDirs()
{
#ifdef QT_DEBUG
QDir dir( QGuiApplication::applicationDirPath() );
dir.cd( "C:/DotaClient" );
QDir::setSearchPaths( "qml", QStringList( dir.absolutePath() ) );
#else
QDir::setSearchPaths( "qml", QStringList( ":/DotaClient/" ) );
#endif
}
Access:
m_mainView->setSource( QUrl( "qml:Root/Root.qml" ) );
Or something like background-image:url(images:Root/root_bg.png); in QSS.
In this case, file Root.qml will be looked in C:/DotaClient/Root/Root.qml in debug build (with possibility of dynamic reloading), and in :/DotaClient/Root/Root.qml (in resources) in release build.

Qt Linguist: translation of text changed dynamically in tr()

I am using qt linguist and qt 5.2. I have a problem that when I add something to .ui files I can see it after lupdate in ts file but when I change name of this button dynamically in my code like:
ui->label->setText(tr("foo"));
I can't see it in ts file, is it possible to make translation in that case? How I can do this?
Basically, tr() can be detected anywhere in the codes.For ui files, linguist would detect the changes immediately, however in codes I encounterd the same problem.
Based on my experience, try to reBuilt/qmake it.
(I am using Qt4.8 & 5.1.)

Customise QPushButton with an image

I am using Qt5 and having some problems in setting the back ground image of a QPushButton. I am using Qt Creator and Design to drag and drop the QPushButton. After that I created a source folder and placed an image in that directory and I googled and tried doing
ui->pushButton_play->setStyleSheet(QString::fromUtf8("background-image: url(:/source/play.png);"));
but no success...am I creating the folder in a wrong place?..I created in D:\Qt5_PROJECT_NAME\source or will I make the url(:\\source\\play.png). Nothing is working...please help...
Did you add the images file into .qrc file? A URL start with : has indicated that It will be searched in .qrc file. If .qrc file didn't used, URL should not start with :. Maybe you should access the image file as
ui->pushButton_play->setStyleSheet(QString::fromUtf8("background-image: url(source/play.png);"));
This is The Qt Resource System documentation.

Resources