Qt connect and first connect or unicode

Mohsen Pahlevanzadeh mohsen at pahlevanzadeh.org
Tue Sep 17 23:34:14 EDT 2013


On Tue, 2013-09-17 at 12:32 +0200, Vincent Vande Vyvre wrote:
> Le 17/09/2013 11:05, Steven D'Aprano a écrit :
> > On Tue, 17 Sep 2013 08:42:35 +0430, Mohsen Pahlevanzadeh wrote:
> >
> >> Dear all,
> >>
> >> Unfortunately, i confused and need help... the following code is:
> >> ###################################################
> >> ##CheckBox:
> >>      QtCore.QObject.connect(self.checkBox,
> >> QtCore.SIGNAL(_fromUtf8("toggled(bool)")), lambda:
> >> self.materialsInstance.setFilterDict("C", self, "name",
> >> self.lineEdit.text()))
> >
> > I don't use Qt, but I'll try to help.
> >
> > First question, what does _fromUtf8 do? It appears to be a private
> > function. Is that your function? If you want to create a string from UTF-8
> > bytes, use:
> >
> > some_bytes.decode('utf-8')
> >
> > I don't think there is a need for a dedicated _fromUtf8 function.
> >
> > If you are using Python 3, then "toggled(bool)" is already a Unicode
> > string, and there is no need to convert from UTF-8.
> >
> > If you are using Python 2, then "toggled(bool)" is a byte string, and you
> > can turn it into a Unicode string by just using the u prefix:
> >
> > u"toggled(bool)"
> >
> > Again, no need to convert from UTF-8.
> >
> > The only time you need to convert from UTF-8 is when you are reading data
> > from a file, or other external source, that is encoded in UTF-8.
> >
> > Other than that, your first line of code seems like a straight-forward
> > call to set a callback function. When the button is pressed, the
> > instance's attribute materialsInstance calls the method setFilterDict.
> >
> >
> >>          QtCore.QObject.connect(self.checkBox_2,
> >> QtCore.SIGNAL(_fromUtf8("toggled(bool)")), lambda:
> >> self.materialsInstance.setFilterDict("C", self, "bought_price",
> >> persianToInteger(unicode(self.lineEdit_2.text()))))
> > Again, the same method is called, only this time with different
> > arguments. Hmmm, this strikes me as poor design. I think that a better
> > design would be for the object to have a few methods:
> >
> >      def toggle_checkbox(self, flag):
> >          # checkbox logic goes here, e.g.
> >          if flag:
> >              ...
> >          else:
> >              ...
> >
> > And then your callback is trivially
> >
> > lambda: self.toggle_checkbox(get_state)
> >
> >
> > I'm not sure how to get the checkbox state from Qt, you will need to
> > replace the "get_state" with the correct code.
> >
> > That, in my opinion, is easier to understand.
> >
> >
> >>          QtCore.QObject.connect(self.checkBox_4,
> >> QtCore.SIGNAL(_fromUtf8("toggled(bool)")), lambda:
> >> self.materialsInstance.setFilterDict("C",self,"stock",
> >> persianToInteger(unicode(self.lineEdit_3.text()))))
> > And more callbacks. Most of your code is probably irrelevant to the
> > problem you are having. You will help us to help you if you can read this
> > website:
> >
> > http://sscce.org/
> >
> >
> > and simplify your code.
> >
> >
> > [...many more callbacks...]
> >
> >
> >> ####################################################
> >> Description:
> >> I have 3 widget:
> >> 1. CheckBox:  a. name b. bought_price c. stock 2. LineEdit : a. name b.
> >> bought_price c. stock 3. One PushButton
> >>
> >> i have three slot: 1. responseToRequestForData()  2.setFilterDict()
> >> 3.unSetFilterDict()
> >> responseToRequestForData(): start to search in DB setFilterDict(): fill
> >> a dict from my LineEdit
> >>
> >> Problem:
> >> My name is filled in dict but its value doesn't fill up. b and c don't
> >> have any problem.
> > I'm afraid I don't understand this. Even your followup post doesn't
> > really help:
> >
> >> I see same output in console:
> >> {'stock': 2, 'name': PyQt4.QtCore.QString(u''), 'bought_price': 23}
> >
> > What result where you expecting?
> >
> > If I remember correctly, Qt strings are mutable unicode strings, so
> > 'name' is associated with an empty Qt string. If they are mutable, that
> > means that once the dict is set:
> >
> > {'name': QString(u'contents of text field'), ...}
> >
> >
> > if the text field changes, so will the string. Is that what is happening?
> >
> > (Or perhaps I have mis-remembered about Qt strings.)
> >
> > You have two callbacks that appear to set the "name" key in the dict.
> > Perhaps one of them is setting it to an empty value.
> >
> >
> >
> >
> 
> _fromUtf8 is needed for the API compatibility, at the beginning of the code of Moshen it is these lines:
> 
> try:
>      _fromUtf8 = QtCore.QString.fromUtf8
> except AttributeError:
>      _fromUtf8 = lambda s: s
> 
> 
> Moshen, I thing if you pass the instance of your GUI at the class materialsInstance you'll can simplify your connections
> 
> self.lineEdit.returnPressed.connect(self.materialsInstance.responseToRequestForData)
> self.lineEdit.editingFinnished.connect(self.materialsInstance.responseToRequestForData)
> 
> For the checkBoxes, I'm not sure you need all these arguments but I don't know enough your code.
> 
> 
> -- 
> Vincent V.V.
> Oqapy <https://launchpad.net/oqapy> . Qarte 
> <https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>
i solved, I didn't think Qt hard up.i change priority of connect in my
script and every thing was OK. ohhhh day of hard code....Thanks GOD.




More information about the Python-list mailing list