Qt connect and first connect or unicode

  • Thread starter Mohsen Pahlevanzadeh
  • Start date
M

Mohsen Pahlevanzadeh

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
 
S

Steven D'Aprano

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

Vincent Vande Vyvre

Le 17/09/2013 11:05, Steven D'Aprano a écrit :
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.
 
M

Mohsen Pahlevanzadeh

Le 17/09/2013 11:05, Steven D'Aprano a écrit :
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top