QUiLoader and ignored dynamic properties - qt

I'm loading the .ui file, where one of the widgets (QComboBox) has a dynamic property (http://qt-project.org/doc/qt-5/properties.html#dynamic-properties). The UI file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PopulateScriptConfig</class>
<widget class="QWidget" name="PopulateScriptConfig">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="langGroup">
<property name="title">
<string>Language</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QComboBox" name="langCombo">
<property name="ScriptingLangCombo" stdset="0">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="codeGroup">
<property name="title">
<string>Implementation</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPlainTextEdit" name="codeEdit"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
The important part is:
<widget class="QComboBox" name="langCombo">
<property name="ScriptingLangCombo" stdset="0">
<bool>true</bool>
</property>
</widget>
I'm loading the file with QUiLoader::load(). I have extended the QUiLoader class, but only to access createWidget() method, where I can query each widget like this:
QWidget* UiLoader::createWidget(const QString& className, QWidget* parent, const QString& name)
{
QWidget* w = QUiLoader::createWidget(className, parent, name);
qDebug() << w->dynamicPropertyNames();
return w;
}
As a result I see empty list displayed, so it seems like the dynamic property is completly ignored.
Note, that UI editor in QtCreator recognizes this dynamic property correclty.
This question was already asked/reported 2 times before, but no solution was provided:
https://bugreports.qt-project.org/browse/QTBUG-11791?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab
http://www.qtcentre.org/threads/32013-QUiLoader-Problem-No-Dynamic-Properties-Support
Any ideas?
P.S. I've made sure that I load correct file. 3 times.

It turned out it was my mistake and Qt is working correctly. Rookie mistake.
The problem is that I tried to access dynamic properties in overriden method of QUiLoader::createWidget(), but this is invoked for each subwidget and dynamic properties are set after all widgets were created. Dynamic properties are not yet available at the moment of createWidget() call.
The very simple project I did for testing this (I mentioned it in the comment) was also working correctly, I just made a mistake while printing debug information (I was printing it from wrong widget). That's all.
QUiLoader works just fine with dynamic properties after all.

Related

loadUi() loads everything but no Images in PyQt

I have created my .ui file in Qt Designers I am then using loadUI() to load it into my app below.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.uic import loadUi
class Life2Coding(QDialog):
def __init__(self):
super(Life2Coding, self).__init__()
loadUi('new.ui', self)
app = QApplication(sys.argv)
widget=Life2Coding()
widget.show()
sys.exit(app.exec_())
However when I go to run this the images will not show up, which is odd as in the Qt designer when I click preview the images actually show up.
Also on a side note if its helpful I am using a xz.prc file for a prefix / path which is how the Qt designer is writing the code linking to the images.
Please could you suggest ways in which I might get the images from new.ui show in my app? Also all widgets work including labels its just the images that do not show up when I run this.
Thanks
Carl
.ui file in its native format
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1343</width>
<height>965</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>730</x>
<y>740</y>
<width>521</width>
<height>171</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="xz.qrc">:/newPrefix/me.jpg</pixmap>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>60</x>
<y>530</y>
<width>651</width>
<height>271</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="xz.qrc">:/newPrefix/logo.png</pixmap>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>930</x>
<y>70</y>
<width>331</width>
<height>301</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">image: url(:/newPrefix/logo.png);</string>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>280</x>
<y>180</y>
<width>68</width>
<height>19</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<resources>
<include location="xz.qrc"/>
</resources>
<connections/>
</ui>
xz.prc
<RCC>
<qresource prefix="newPrefix">
<file>logo.png</file>
<file>me.jpg</file>
</qresource>
</RCC>
xz_rc.py
This is after it has been converted from xz.prc
from PyQt5 import QtCore
qt_resource_data = b
qt_version = QtCore.qVersion().split('.')
if qt_version < ['5', '8', '0']:
rcc_version = 1
qt_resource_struct = qt_resource_struct_v1
else:
rcc_version = 2
qt_resource_struct = qt_resource_struct_v2
def qInitResources():
QtCore.qRegisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data)
def qCleanupResources():
QtCore.qUnregisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data)
qInitResources()
The .qrc file maybe what Qt designer uses to connect to images however when I am loading the .ui file into my .py app it is having trouble using the .qrc file so instead I need to create a .py version of .qrc.
Go to the directory that .qrc is located in using command prompt and write in this command. Note xz is just what I called it yours may be called thing else.
pyrcc5 xz.qrc -o xz_rc.py
Then import this into your .py app no need to add .py on the end as we are importing this as a module.
import xz.rc
Now your .py app shall display images thanks to having access to a format it can now understand.

Nested structures with QRadioButtons

I would like to achieve something similar to this picture:
except that the top level ("Adress bar", "forms" and "User names...") should be radio buttons.
The idea is that the sublevels should get enabled or disabled depending on the state of the radiobuttons. And the sublevels should be shifted to the right as on the picture.
Can that be done Qt in an elegant way?
I would say a simple QVBoxLayout for the top level and each "sublevel" has a QHBoxLayout with a fixed sized spacer item as the first child and a QVBoxLayout containing the sub options.
Disabling all sub options can then simply be done by disabling the "sublevel" widget.
Just put those sub-items (like "Browsing history", "Favorites", ...) into a separated QWidget and connect that widget's QWidget::setEnabled() slot with the "Adress bar" radio-button's QAbstractButton::toggled() signal.
This is a Qt Designer's .ui file (with working signal-slot connection, try Ctrl+R in Designer) that demonstrates the idea:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>170</width>
<height>178</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QRadioButton" name="radioButton">
<property name="text">
<string>RadioButton</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>CheckBox</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_2">
<property name="text">
<string>CheckBox</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_3">
<property name="text">
<string>CheckBox</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_2">
<property name="text">
<string>RadioButton</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_3">
<property name="text">
<string>RadioButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>radioButton</sender>
<signal>toggled(bool)</signal>
<receiver>widget</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>84</x>
<y>17</y>
</hint>
<hint type="destinationlabel">
<x>84</x>
<y>77</y>
</hint>
</hints>
</connection>
</connections>
</ui>

Setting QtreeWidget Height

I am new to qt So I don't have much knowledge of sizepolicy and streches of widgets.
I have a vertical box layout in which I have first added a Qlabel and then a QTreeWidget and then again a Qlabel. Now the problem is QTreeWidget is taking full space in vertical direction and then after leaving a very big space after QTreeWidget, although only rows exist currently in it, QLabel is added. I want that second Qlabel is added immediately after two rows visible and now when more rows are added then It shift downwards. Can someone suggest me how to do this?
I think this is what you meant:
UI has in a layout: Label, QTreeWidget, Label, Spacer
(Spacer is important becaus else Qt might just expand the labels to fill the window.)
Important: In Designer TreeWidget hight to preffered, vertical scrollbar off.
UI Example:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>517</width>
<height>568</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="my_tree" name="treeView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAsNeeded</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>517</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>my_tree</class>
<extends>QTreeView</extends>
<header>my_tree.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
A Overloaded QTreeView (in your case QTreeWidget) is used to get the wanted sizehints:
EDIT -- This is a uggly header only definition i called my_tree.h --
#ifndef MY_TREE_H
#define MY_TREE_H
#include <QHeaderView>
#include <QTreeView>
class my_tree: public QTreeView
{
Q_OBJECT
public:
my_tree(QWidget* parent):QTreeView(parent)
{
// Important: if something is shown/hidden we need a new size
connect(this,SIGNAL(expanded( const QModelIndex & )),SLOT(onExpandCollapsed()));
connect(this,SIGNAL(collapsed( const QModelIndex & )),SLOT(onExpandCollapsed()));
};
// QWidget interface
public:
QSize sizeHint() const {return my_size(); };
QSize minimumSizeHint() const { return my_size(); };
private:
QSize my_size() const
{ //QSize tst(sizeHintForColumn(0) + 2 * frameWidth(), sizeHintForRow(0) + 2 * frameWidth());
int neededHight= 2 * frameWidth()+ this->header()->height();
QModelIndex root = this->rootIndex();
QAbstractItemModel* m = this->model();
//if(this->rootIsDecorated())
{
neededHight += recursiveHeightHint(root,m);
}
QSize temp = QTreeView::sizeHint();
temp.setHeight(neededHight);
return QSize(1,neededHight);
}
// we need the size of all visible items -> isExpanded
// the root item is usually shown as a non-Valid index -> !i.isValid()
int recursiveHeightHint(QModelIndex i,QAbstractItemModel* m) const
{
int temp=sizeHintForIndex(i).height();
if(this->isExpanded(i) || !i.isValid())
{
if(m->hasChildren(i))
{
int numRows = m->rowCount(i);
for(int count =0;count<numRows;count++)
temp+=recursiveHeightHint(m->index(count,0,i),m);
}
}
return temp;
}
private slots:
void onExpandCollapsed(){updateGeometry();}
};
#endif // MY_TREE_H
PS: in Designer first place the base Widget and then define it as placeholder for the self designed one.
I used this entry to get to this solution:
QListWidget adjust size to content

How to make Qt widgets resizeable?

I want to create an application which consists of a QLineEdit and two QTableView widgets in vertical layout.
Sample code:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>401</width>
<height>301</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QTableView" name="tableView_2"/>
</item>
<item>
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>lineEdit</sender>
<signal>returnPressed()</signal>
<receiver>Dialog</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>200</x>
<y>14</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>149</y>
</hint>
</hints>
</connection>
</connections>
</ui>
But these QTableView widgets are not resizable. I didn't found any property in Qt for this.
So how can I make these QTableView widgets resizable or auto expand and contract?
means if I reduce size of first QTableView widget then automatically size of second QTableView widget should increase means every widget should expand or contract automatically if I change size of any widget inside Vertical Layout at run time.
The sample code was designed in Qt4 Designer on Ubuntu 14.04.
Break the current layout, select the two widgets, click Layout Vertically in Splitter and finally select the form and click the layout vertically.
This is because you don't have a layout on your QDialog. What you did was you dragged a vertical layout on top of the dialog, now the dialog contains the layout which is absolute positioned and is not linked to the dialog. What you want to do is move the all the widgets from the vertical layout on top of a QSplitter, right click on the QDialog and at the Lay out context menu entry choose Lay Out Vertically, or alternatively click the QDialog and press Ctrl + 2. The result should be the following:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>405</width>
<height>305</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QLineEdit" name="lineEdit"/>
<widget class="QTableView" name="tableView_2"/>
<widget class="QTableView" name="tableView"/>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
What I recommend is reading the Qt Layout Management Documentation for more info on this topic.

How to make QDockWidget to expand when the window is resized?

I have a simple form: QDockWidget on the right side, QGroupBox on the left side of QMainWindow. QGroupBox is layed out using vertical layout.
I want QDockWidget to be expanded and occupy all the free space when the main window is enlarged or maximized.
How do I do it?
Bellow is the source code of my .ui file.
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>653</width>
<height>381</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>GroupBox</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QDockWidget" name="dockWidget">
<attribute name="dockWidgetArea">
<number>2</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents"/>
</widget>
</widget>
<resources/>
<connections/>
</ui>
You will need to reimplement QWidget::changeEvent (or a resizeEvent for resizing) to react to QEvent::WindowStateChange. If one of those events occurs, get the QWidget::windowState and resize your dock to whatever you want.

Resources