简体   繁体   English

如何在qtdesigner中使用qss中的qrc资源?

[英]How to use qrc resources in qss in qtdesigner?

I need to create QLineEdit with the search icon on the left side and i'm doing it in QtDesigner.我需要使用左侧的搜索图标创建 QLineEdit,我正在 QtDesigner 中进行操作。

I've added my search_icon.svg to my resources.qrc file and assigned this qrc as a resource of my application using resource browser of QtDesigner.我已将我的 search_icon.svg 添加到我的 resources.qrc 文件中,并使用 QtDesigner 的资源浏览器将此 qrc 分配为我的应用程序的资源。 The contents of resources.qrc look like this: resources.qrc 的内容如下所示:

<RCC>
  <qresource prefix = "/icons">
    <file alias = "search_icon.svg">search_icon.svg</file>
  </qresource>
</RCC>

Then I referenced search_icon.svg in "Edit Style Sheet" window of QtDesigner using qss:然后我使用qss在QtDesigner的“编辑样式表”window中引用了search_icon.svg:

QLineEdit {
    background-image: url(:/icons/search_icon.svg);
    background-position: left;
    padding: 2 2 2 25;
} 

but search_icon.svg hadn't been displayed on QLineEdit.但在 QLineEdit 上没有显示 search_icon.svg。

QtDesigner's mainWindow.ui and resources.qrc as same as search_icon.svg are in the same directory. QtDesigner的mainWindow.ui和resources.qrc同search_icon.svg在同一个目录下。

Do I need to compile resources.qrc somehow so that search_icon.svg can be used in qss of "Edit Style Sheet" window of QtDesigner?我是否需要以某种方式编译 resources.qrc 以便 search_icon.svg 可以在 QtDesigner 的“编辑样式表”window 的 qss 中使用?

What you need is to add the.qrc to the ui following the following steps:您需要按照以下步骤将 .qrc 添加到 ui 中:

  • On the right side there should be a dockwidget called "Resource Browser"(If it is hidden then you must enable it by clicking on View->Resource Browser).在右侧应该有一个名为“资源浏览器”的dockwidget(如果它是隐藏的,那么您必须通过单击查看->资源浏览器来启用它)。

    在此处输入图像描述

  • Then click on the left icon called "Edit Resources" and the following window will open:然后单击左侧名为“编辑资源”的图标,将打开以下 window:

    在此处输入图像描述

  • Then you press the然后你按在此处输入图像描述 button, select the.qrc and press the "OK" button.按钮,select.qrc 并按下“确定”按钮。

With the above, the.qrc icons will be visible in the Qt Designer preview which specifically adds the following to the.ui:使用上述内容,.qrc 图标将在 Qt 设计器预览中可见,该预览专门将以下内容添加到 .ui:

  <include location="qresources.qrc"/>
 </resources>

Another direct way to load the icon from the.qrc is to open the Qt StyleSheet form by right-clicking on the widget and selecting the "Add Resource" option and the "Resource Browser" will appear, so you must follow the previous steps.从 .qrc 加载图标的另一种直接方法是打开 Qt StyleSheet 表单,方法是右键单击小部件并选择“添加资源”选项,将出现“资源浏览器”,因此您必须按照前面的步骤操作。


If you are going to convert the.ui to.py that contains a.qrc then you must also convert it, assuming the files are main.ui and main.qrc then you should follow the next steps:如果要将 .ui 转换为包含 a.qrc 的 .py,则还必须将其转换,假设文件是 main.ui 和 main.qrc,那么您应该按照以下步骤操作:

pyuic5 main.ui -o foo.py -x
pyrcc5 main.qrc -o main_rc.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM