Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54262 > unrolled thread

Qt connect and first connect or unicode

Started byMohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org>
First post2013-09-17 08:42 +0430
Last post2013-09-18 08:04 +0430
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Qt connect and first connect or unicode Mohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org> - 2013-09-17 08:42 +0430
    Re: Qt connect and first connect or unicode Steven D'Aprano <steve@pearwood.info> - 2013-09-17 09:05 +0000
      Re: Qt connect and first connect or unicode Vincent Vande Vyvre <vincent.vandevyvre@swing.be> - 2013-09-17 12:32 +0200
      Re: Qt connect and first connect or unicode Mohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org> - 2013-09-18 08:04 +0430

#54262 — Qt connect and first connect or unicode

FromMohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org>
Date2013-09-17 08:42 +0430
SubjectQt connect and first connect or unicode
Message-ID<mailman.47.1379391172.18130.python-list@python.org>
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()))

        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()))))

        QtCore.QObject.connect(self.checkBox_4,
QtCore.SIGNAL(_fromUtf8("toggled(bool)")), lambda:
self.materialsInstance.setFilterDict("C",self,"stock",persianToInteger(unicode(self.lineEdit_3.text()))))

##LineEdit
        QtCore.QObject.connect(self.lineEdit,
QtCore.SIGNAL(_fromUtf8("editingFinished()")), lambda:
self.materialsInstance.setFilterDict("L",self,"name",self.lineEdit.text()))

        QtCore.QObject.connect(self.lineEdit_2,
QtCore.SIGNAL(_fromUtf8("editingFinished()")), lambda:
self.materialsInstance.setFilterDict("L",self,"bought_price",persianToInteger(unicode(self.lineEdit_2.text()))))

        QtCore.QObject.connect(self.lineEdit_3,
QtCore.SIGNAL(_fromUtf8("editingFinished()")), lambda:
self.materialsInstance.setFilterDict("L",self,"stock",persianToInteger(unicode(self.lineEdit_3.text()))))        

        QtCore.QObject.connect(self.lineEdit,
QtCore.SIGNAL(_fromUtf8("editingFinished()")), lambda:
self.materialsInstance.responseToRequestForData(self))

        QtCore.QObject.connect(self.lineEdit_2,
QtCore.SIGNAL(_fromUtf8("editingFinished()")), lambda:
self.materialsInstance.responseToRequestForData(self))

        QtCore.QObject.connect(self.lineEdit_3,
QtCore.SIGNAL(_fromUtf8("editingFinished()")), lambda:
self.materialsInstance.responseToRequestForData(self))        

        QtCore.QObject.connect(self.lineEdit,
QtCore.SIGNAL(_fromUtf8("returnPressed()")), lambda:
self.materialsInstance.responseToRequestForData(self))

        QtCore.QObject.connect(self.lineEdit_2,
QtCore.SIGNAL(_fromUtf8("returnPressed()")), lambda:
self.materialsInstance.responseToRequestForData(self))

        QtCore.QObject.connect(self.lineEdit_3,
QtCore.SIGNAL(_fromUtf8("returnPressed()")), lambda:
self.materialsInstance.responseToRequestForData(self))        

##PushButton:
        QtCore.QObject.connect(self.pushButtonSearch,
QtCore.SIGNAL(_fromUtf8("clicked()")), lambda:
self.materialsInstance.responseToRequestForData(self))



    def setFilterDict(self,widget,obj,field,lineEditContent):
        if field not in obj.materialsInstance.filterNameDict.keys():

obj.materialsInstance.filterNameDict.update({field:lineEditContent})


####################################################
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.

Yours,
Mohsen

[toc] | [next] | [standalone]


#54271

FromSteven D'Aprano <steve@pearwood.info>
Date2013-09-17 09:05 +0000
Message-ID<52381b5b$0$29869$c3e8da3$5496439d@news.astraweb.com>
In reply to#54262
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.




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#54276

FromVincent Vande Vyvre <vincent.vandevyvre@swing.be>
Date2013-09-17 12:32 +0200
Message-ID<mailman.57.1379413974.18130.python-list@python.org>
In reply to#54271
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>

[toc] | [prev] | [next] | [standalone]


#54352

FromMohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org>
Date2013-09-18 08:04 +0430
Message-ID<mailman.100.1379475272.18130.python-list@python.org>
In reply to#54271
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web