Your property might need a NOTIFY signal to indicate that the value has changed.
Cheers,
_
Your property might need a NOTIFY signal to indicate that the value has changed.
Cheers,
_
I modified the ColorWidget as follows:
Qt Code:
sigDataChanged = pyqtSignal() def __init__(self, color=None): if color is not None: self._color = color else: self._color = "#ff0000" self.ldtColor.setReadOnly(True) self.btnColor.setIcon(icon) self.btnColor.setFixedWidth(24) self.btnColor.setFixedHeight(24) layout.addWidget(self.ldtColor) layout.addWidget(self.btnColor) self.setLayout(layout) self.setContentsMargins(0, 0, 0, 0) self.layout().setContentsMargins(0, 0, 0, 0) self.btnColor.clicked.connect(self.changeColor) def changeColor(self): print(self._color) colorInit.setNamedColor(self._color) if color.isValid(): self.ldtColor.setText(color.name()) self.setColor(color.name()) def updateIcon(self): newcolor.setNamedColor(self._color) pixmap.fill(newcolor) self.btnColor.setIcon(icon) def getColor(self): return self._color def setColor(self, newColor): self._color = newColor self.ldtColor.setText(newColor) self.updateIcon() self.sigDataChanged.emit() color = pyqtProperty(str, fget=getColor, fset=setColor, notify=sigDataChanged)To copy to clipboard, switch view to plain text mode
Basically, I just added a pyqtSignal that is emitted when the color has changed. However, this modification does not solve the problem: this modification is not stored into the model. If I read the documentation of pyqtProperty (http://pyqt.sourceforge.net/Docs/PyQ...roperties.html), I can read:
notify – the optional unbound notify signal. It is ignored by Python.
What does it means: whatever its value, pyqt will not interpret it? Therefore, can I solve my problem?
Thanks for your time, it is greatly appreciated!
Cheers,
Vincent
Hi all,
I found a way to circumvent the problem by mapping the model to the QLineEdit widget of my custom but I do not know how to proceed.
The QLineEdit is set in readOnly mode only and its content is programmatically modified. What signal needs to be emitted to tell the QDataWidgetMapper the content has been modified? Because, when a model is mapped on a QLineEdit, everything is automatic and I need to understand hows it works to be able to reproduce its behaviour.
Cheers,
Vincent
I played a bit around and what seems to work is to connect the colorwidget's change signal to the mapper's submit slot.
Cheers,
_
Could you be more precise or ideally write the piece of code you mentionned because I do not see what you mean.
Cheers,
Vincent
I haven't done any PyQt programming myself, but maybe something like
Qt Code:
self.dataMapper.addMapping(self.colorWidget, 1, "color") self.colorWidget.sigDataChanged.connect(self.dataMapper.submit);To copy to clipboard, switch view to plain text mode
Cheers,
_
I did not manage to make it working properly. By changing the policy of the dataMapper to Manual, I get plenty of errors. But I may not use the method correctly. I just did something like:
but it is not working.
It tried few other things and I noticed something which appear very strange to me. When I change the color attribute of my colorWidget with the QColorDialog, I then write the name of the color in the QLineEdit and modify the icon of the QPushButton. If I change the selected object within my treeView, the change is not stored in the model (we already knew that). However, if I set the focus on the QLineEdit and I press manually enter (to force the widget to emit the returnPressed signal), then the color name is stored into the model (the setData method is called). I tried to reproduce programmaticaly this behaviour by changing the focus on the QLineEdit and by emitting manually the returnPressed signal, but it does not solve my problem. I still have to interact with my widget to force the setData method to be called. Strange isn't it? I start to feel really dumb. This problem should have a simple solution, but I don't find it.
In attachment the last version of the example (it may be clearer for you to directly test the code).
Any help would be highly welcomed!
Thanks in advance for your time.
Cheers,
Vincent
So you connect to a local slot and that one calls submit(), should work as well.
Is the slot being invoked?
it definitely works in C++. I have a color editing widget and connect its signal to the datawidgetmapper's submit() slot and whenever I change color, the model is update accordingly.
Qt Code:To copy to clipboard, switch view to plain text modeQt Code:
#include "coloreditor.h" #include <QColorDialog> #include <QHBoxLayout> #include <QLineEdit> #include <QPushButton> { hbox->addWidget(m_lineEdit); hbox->addWidget(button); connect(button, SIGNAL(clicked()), this, SLOT(onButtonClicked())); } { return m_color; } { if (color == m_color) { return; } m_color = color; m_lineEdit->setText(m_color.name()); emit colorChanged(color); } void ColorEditor::onButtonClicked() { if (!color.isValid()) { return; } setColor(color); }To copy to clipboard, switch view to plain text modeQt Code:
ui->tableView->setModel(model); mapper->setModel(model); mapper->addMapping(ui->name, 0); mapper->addMapping(ui->color, 1, "color"); mapper->toFirst(); To copy to clipboard, switch view to plain text mode
Cheers,
_
I finally managed to make it working! Your code is exactly the same as I tried before my last post and I did not manage to make it working because of a recursivity problem (the sigDataChanged signal was directly emitted in the setColor method).
I had to change a little bit the code :
I just emit the sigDataChanged signal after the setColor method has been called. Finally, I update the model by connecting this signal to the submit slot of the QDataWidgetMapper in the appropriate PropertiesEditor class (as you mentionned).Qt Code:
sigDataChanged = pyqtSignal(str) def __init__(self, color=None): some python code here self.btnColor.clicked.connect(self.changeColor) def changeColor(self): colorInit.setNamedColor(self._color) if color.isValid(): self.ldtColor.setText(color.name()) self.setColor(color.name()) self.sigDataChanged.emit(self._color) def updateIcon(self): newcolor.setNamedColor(self._color) pixmap.fill(newcolor) self.btnColor.setIcon(icon) def getColor(self): return self._color def setColor(self, newColor): self._color = newColor self.ldtColor.setText(newColor) self.updateIcon() color = pyqtProperty(str, fget=getColor, fset=setColor)To copy to clipboard, switch view to plain text mode
Qt Code:
self.colorWidget.sigDataChanged.connect(self.dataMapper.submit)To copy to clipboard, switch view to plain text mode
Problem solved. I close the thread.
Thank you very much anda_skoa for your time and help.
Cheers,
Vincent
You are definitely right! I just check for changes in the setColor method, and the recursivity problem disappears.
PyQt and Qt behaves exactly the same way on this aspect.
Once again, thank you!
Cheers,
Vincent
Bookmarks