問題描述
我正在通過帶有參數樣式表的命令行運行我的 Qt 應用程序.控件的樣式有效,但在我嘗試為 MainWindow 加載背景圖像時無效.我試過了:
I'm running my Qt app through command line with a parameter -stylesheet. The styles for the controls work but not when I'm trying to load a background image for the MainWindow. I tried:
QMainWindow{
background-image:url(:image_256_8bit_latest_back.png);
}
還嘗試刪除背景中的:",但沒有任何區別.有人能告訴我這個樣式表有什么問題嗎?
Also tried removing the ":" in background, but doesn't make a difference. Can somebody tell me what's wrong with this StyleSheet?
推薦答案
您嘗試使用的圖像在哪里?
Where is located the image you are trying to use ?
您是否將其作為應用程序的資源?
Did you put it as a resource of your application ?
如果您想使用屬于資源一部分的圖像,您的項目中應該有一個資源文件 (*.qrc
).該文件應包含如下內容:
If you want to use an image which is part of your resources, you should have a resource file (*.qrc
) in your project. This file should contain something like this :
<RCC>
<qresource prefix="/images">
<file alias="sunset.jpg">sunset.jpg</file>
</qresource>
</RCC>
然后,您可以在 QMainWindow
的構造函數中編寫此代碼:
Then, you could write this code in the constructor of your QMainWindow
:
setStyleSheet("background-image: url(:/images/sunset.jpg);");
如果您不想使用 Qt 資源系統,您可以將圖像的路徑放在磁盤上:
If you don't want to use the Qt resource system, you can just put the path to your image on your disk :
setStyleSheet("background-image: url(res/images/sunset.jpg);");
但如果您使用相對路徑請小心:Qt 將從當前位置開始,這可能會改變,特別是如果您使用 Qt Creator 進行開發.
Be careful though if you are using a relative path : Qt will start from the current location, which might change, particularly if you are developping with Qt Creator.
使用 Qt Creator,當您在調試模式下運行應用程序時,當前路徑在 debug/
中.當您在發布模式下運行您的應用時,當前路徑位于 release/
(除非您更改了設置).
With Qt Creator, when you run your app in debug mode, the current path is in debug/
. When you run your app in release mode, the current path is in release/
(unless you changed the settings).
這篇關于無法在 Qt 樣式表中設置背景圖像的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!