It looks like you're new here. If you want to get involved, click one of these buttons!
Hi Matthias,
I have been having trouble getting images to show up in QLabels in a QDialog box, and I was wondering whether I am on the right track. The QDialog box is meant to be generated by an external party, so my script should use it as-is without calling any functions on its internals.
I am using KLayout 0.24-python-eval on Windows 7.
The instructions I could find online for adding images recommend using a QRC file to manage the images. An example of a UI file from QtDesigner, called single_icon.ui, is below. It uses the QLabel's pixmap property to state the image file being used.
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MyDialogBox</class>
<widget class="QDialog" name="MyDialogBox">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>270</width>
<height>91</height>
</rect>
</property>
<property name="windowTitle">
<string>MyDialogBox</string>
</property>
<widget class="QLabel" name="mylabel">
<property name="geometry">
<rect>
<x>50</x>
<y>20</y>
<width>181</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="myicon.qrc">:/my_image.png</pixmap>
</property>
</widget>
</widget>
<resources>
<include location="myicon.qrc"/>
</resources>
<connections/>
</ui>
The resource file, myicon.qrc, is:
<RCC>
<qresource prefix="/">
<file>my_image.png</file>
</qresource>
</RCC>
When it comes to using the dialog box in KLayout, the image does not show up. The script is as follows.
module MyImageButton
include RBA
ui_file = RBA::QFile::new("#{File.dirname(__FILE__)}/single_icon.ui")
ui_file.open(RBA::QIODevice::ReadOnly)
@dialog = RBA::QFormBuilder::new.load(ui_file, RBA::Application::instance.main_window)
ui_file.close
a = RBA::Action.new
a.title = "Open Dialog"
a.on_triggered do
puts "Pixmap: #{@dialog.mylabel.pixmap}"
@dialog.exec
end
menu = RBA::Application.instance.main_window.menu
menu.insert_item("@toolbar.end", "a", a)
end
It creates a button that opens the dialog box, but the dialog box is empty and prints nothing for the pixmap. Is the pixmap meant to return something?
The four files are in the same directory. Do you know how the paths should be specified?
I noticed that the KLayout source uses the QRC file, such as the Help/About box. Is this something that can be done through Ruby scripting?
Thank you and regards,
Z Lim
Comments
Hi zlim,
Resources are not loaded within Python automatically. But you can compile the resource file with rcc.
Here is the recipe:
Compile the qrc file into a binary resource file for dynamic loading:
rcc -binary myicon.qrc >myicon.rcc
Register the resource file at the beginning of your code:
pya.QResource.registerResource_file("path_to_your_file/myicon.rcc")
Use the icon as usual through the path
:/my_image.png
Please note that the function is originally called "registerResource", but the first parameter is ambiguous (a
const uchar *
can be a string as well as aQString
). Hence the name is made disambiguous by appending_file
and_data
for the version taking binary in-memory data.Matthias
Hi Matthias,
Thank you very much for your advice. I'm still having some trouble getting the image to show up in the dialog box, though. I compiled the qrc file using the rcc command you provided and put the output in the same directory as the other files. I then added the line:
to the beginning of the script that creates the button on the toolbar. The line does print "true" to the console when I run the script, but when I press the button, the dialog box does not have the image in it.
When the button is pressed, I try to print the pixmap to the console, but that is empty. I use:
Should it return something if pixmap is specified only in the UI file?
I did not change anything in the UI file or the QRC file.
Do you think there is anything I have missed?
Please note that my script is in Ruby.
Thank you for your help so far.
Regards,
Z Lim
Hi Z Lim,
basically the solution seems to work. I don't know what's actually wrong in your case.
I have tried your files with an image of mine.
It's important to put the .rcc and .ui file to the place where the macro is installed (in may case `~/.klayout/macros').
When I press the button I get:
The complete script looks like this:
Matthias
Hi Matthias,
Thank you very much for your help. I found a way to display images with the RCC resource file, but it does not use the QLabel's pixmap field. Instead, I found I could select the image with some HTML code in the QLabel's text field. In my case, the HTML code looked something like:
The label in the UI file from Qt Designer thus looked like:
Regards,
Z Lim
Hi Z Lim,
you mean, using the pixmap property still won't work? That is strange since it works with HTML.
That's strange since it means the resource is there but the ui loader is not able to access it, while the QLabel is. Are you sure you have registered the resource before
QFormBuilder#load
is used?I need to check whether that is a Windows specific issue. It works on my Linux with Qt 4.8.6.
Matthias