Qt - QFile not opening a .qss file - c++

I'm trying to use an external stylesheet for my project and I'm having trouble opening it using the QFile class. I've imported it into the .qrc file, a portion of it looks like this:
<qresource prefix="stylesheets">
<file>Resources/Stylesheet.qss</file>
</qresource>
This is how I am opening and using the file:
QFile stylesheet(":/stylesheets/Resources/Stylesheet.qss");
if (stylesheet.open(QIODevice::ReadOnly | QIODevice::Text))
{
newGameDialog.setStyleSheet(stylesheet.readAll());
stylesheet.close();
}
What could be wrong with this? I am using Visual Studio 2013 with the latest Qt and the VS Qt Add-in. I've also set the project to support QML in "Qt Project Settings".
The .qss file looks like this (it works if set directly as a QString). I'm not sure if the "import" line is needed:
import Qt 5.3.1
QDialog
{
background-color: 'white';
}
It is reading it fine, but the style is not applying. Here it is in debug mode:

QDialog does not support "background-color", only "backgroud:".
(Further ideas if that doesnt work:
Maybe forgot to specify Q_OBJECT for newGameDialog class?
Alternatively use Qt Designer to create a QDialog, copy your stylesheet source in the stylesheet property field and see if it works or if the designer shows an error or does apply the style correctly when test-instanciating the dialog (Ctrl+R i think).)

Related

How to include an icon in the title bar of window in Qt?

I want to be able to replace the existing standard icon with my own but I am unsure how to do so. I only started Qt one week ago so I don't know much about the application. In addition to this I looked at the Qt website but their method for adding an icon did not work on my computer.
Besides using a RC file (Windows) you may specify the icon directly using qmake.
win32: RC_ICONS = icon.ico
Make sure it detects the path correctly. In the provided case the icon resides in the project root directory right beside the project file.
For window icons you typically, if application wide applicable, use some method of QApplication.
...
QApplication app(argc, argv);
...
app.setWindowIcon(QIcon(":/icon");
...
which is consuming the resource file using the alias icon. Here is an example (res.qrc):
<RCC>
<qresource prefix="/">
<file alias="icon">icon.png</file>
</qresource>
</RCC>
More information on resources are here.
Perhaps try the function setWindowIcon function on the QWidget or QApplication.

Qt qrc resource file - can't load icon

I have a Qt5 desktop project and I added a "resource.qrc" file with the Qt Creator editor which created the following line into the project's .pro file:
RESOURCES = resource.qrc
I put a blank prefix and a png file (14x14) and I tried to use it like this:
QPixmap pixmap = QPixmap ("://my_image.png");
ui->combobox->addItem(QIcon(pixmap), "itemname");
The problem is: the icon won't show up!
The following works:
QPixmap pixmap(14,14);
pixmap.fill(QColor("red"));
ui->combobox->addItem(QIcon(pixmap), "itemname");
so the problem must be in the resource embedding process.. I noticed that the generated "exe" hasn't a resource section inside it... I don't have static linked external libraries, so I don't think I need the Q_INIT_RESOURCE(resource) macro (it gives me undefined external)
Update:
I'm posting here my qrc file:
<RCC>
<qresource prefix="/">
<file>my_image.png</file>
</qresource>
</RCC>
it's pretty simple and I don't understand why at runtime icons are not showing up
I had this same issue recently, where I malformed the resource string. If you are using a current version of Qt Creator, you can open your .qrc file for edit and then right-click the resource (in this case image) that you are trying to address, then click "Copy Resource Path to Clipboard". And voila, you have the correct resource string every time.
Qt Creator is awesome.
Hope this helps!
#Nikos C. gives you useful advice, but I think your main problem was that you didn't use the correct link to the resource.
In your code, you have:
QPixmap pixmap = QPixmap ("://my_image.png");
but, according to the documentation, it should be
QPixmap pixmap = QPixmap (":/my_image.png");
or you can give aliases to your resources, and use those instead.
Issue is solved is use rcc.exe
C:\root\QT>c:\root\QT\4.7.4\bin\rcc.exe Headless.qrc -o qtresources.cpp
During compilation you should have images in the path.
Create the qtresources.cpp file include this file in makefile or project. You should able to see the image.

Does Meego Nokia N950 support C++ QML binding?

I want to communicate between C++ and QML on Meego. It happens using C++ QML binding as described in this link.
When I run an app on the Symbian platform it works, and data from C++ is available to manipulate in some QML file.
But on Meego it always says myItem.qml file not found.
See the following code snippet:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDeclarativeView view;
MyClass myClass;
view.rootContext()->setContextProperty("myObject", &myClass);
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
return app.exec();
}
Does Meego does not support QML C++ binding?
I also tried to give the exact path of the QML file but never found that file, even myItem is in project structure.
Is there some thing associated with path settings in .pro file etc
Please help on this as I am stuck here with this and cannot move forward in my app using Qt Quick.
Yes, but your problem isn't related to C++ and QML bindings. Your application (specifically the view) can't find the .qml file to load.
By default, I think on meego the default path to look in is ../qml. So by default you should be packaging into /opt/YOURNAME with the binary in /opt/YOURNAME/bin and the qml files in /opt/YOURNAME/qml.
However, you can also put the qml files into a resource and use the :/ paths to access a resource instead.
IE, in your resource file:
<RCC>
<qresource prefix="/">
<file>qml/foo.qml</file>
</qresource>
</RCC>
And in C++:
viewer.setSource(QUrl("qrc:/qml/foo.qml"));

Qt4 css - How to create file .QSS?

I have a question.
How do I create in my project folder with a file. .QSS ?
At the option of creating new files do not have this extension!
I would be very grateful for the help.
Thank you.
Control+N
-> General
-> Chooose
In name write style.qss (you may also choose the directory)
Add to project, or SVN
Drink a coffie or beer
Just create it with a text editor like a notepad and save it with the .qss extension. I suggest to add it in your resource file.
You can load it using the following sample code
QFile file(":/qss/style.qss");
if(file.open(QFile::ReadOnly)) {
QString StyleSheet = QLatin1String(file.readAll());
qApp->setStyleSheet(StyleSheet);
}
Yeah, Qt Creator (I'm assuming that is what you are using) is a little lacking in this area. Just create the folder and file on your file system. Then, in Qt Creator, right click on the project and select Add Existing Files...

How to set application icon in a Qt-based project?

How do you set application icon for application made using Qt? Is there some easy way? It's a qmake-based project.
For Qt 5, this process is automated by qmake. Just add the following to the project file:
win32:RC_ICONS += your_icon.ico
The automated resource file generation also uses the values of the following qmake variables: VERSION, QMAKE_TARGET_COMPANY, QMAKE_TARGET_DESCRIPTION, QMAKE_TARGET_COPYRIGHT, QMAKE_TARGET_PRODUCT, RC_LANG, RC_CODEPAGE.
For Qt 4, you need to do it manually. On Windows, you need to create a .rc file and add it to your project (.pro). The RC file should look like this:
IDI_ICON1 ICON DISCARDABLE "path_to_you_icon.ico"
The .pro entry should also be win32 specific, e.g.:
win32:RC_FILE += MyApplication.rc
Verified in Linux (Qt 4.8.6) and Windows (Qt 5.6):
1) Add the icon file to your project folder;
2) In the main function call setWindowIcon() method. For example:
QApplication a(argc, argv);
a.setWindowIcon(QIcon("./images/icon.png"));
To extend Rob's answer, you can set an application icon for macOS by adding and modifying the following line onto your .pro file.
macx: ICON = <app_icon>.icns
Note that the ICON qmake variable is only meant to target macOS.
For Windows, use
RC_ICONS = <app_icon>.ico if you're attaching a .ico file
or RC_FILE = <app_icon>.rc if you want to attach your icon through a .rc file. (Be sure to add IDI_ICON1 ICON DISCARDABLE "myappico.ico" into the rc file. Indentation not mine.)
For further reading, see Setting the Application Icon.
For Qt5 on Windows, move your ico file to project folder and add this line to your .pro file:
RC_ICONS = myappico.ico
Offical link: https://doc.qt.io/qt-5/appicon.html
Now that Qt has upgraded to 5.0.1, there is new method to add an application icon. First, you need prepare a resource file, named as .qrc
1) Without Qt Designer, I assume there is a QMainWindow instance whose name is MainWin. You can use:
QIcon icon(":icon/app.icon");
MainWin.setWindowIcon(icon);
2) With Qt Designer, you can modify the property of QMainWindow. Choose icon resource from .qrc and insert it into the row of windowIcon.
The above method can be used in Qt4.7, Qt4.8.x.