PyQt5 Getters and Setters
In an attempt to tech myself a method for binding in python, I have the following code:
wname = QLineEdit()
dname = "Test Value"
sattr = getattr(wname, "text")
How to assign the value of dname to wname using the info from above?
I do understand that setText() would typically be used here; however, I am exploring a method for binding and I would like to know how to utilitze the getattr()
function for extracting the setter and getter for the widget passed. Thanks!!!
python python-3.x pyqt pyqt5
|
show 1 more comment
In an attempt to tech myself a method for binding in python, I have the following code:
wname = QLineEdit()
dname = "Test Value"
sattr = getattr(wname, "text")
How to assign the value of dname to wname using the info from above?
I do understand that setText() would typically be used here; however, I am exploring a method for binding and I would like to know how to utilitze the getattr()
function for extracting the setter and getter for the widget passed. Thanks!!!
python python-3.x pyqt pyqt5
1
setattr(wname, 'text', dname)
orgetattr(wname, 'setText')(dname)
. The former only really makes sense if the attribute is a qt property.
– ekhumoro
Nov 19 '18 at 16:41
pyqt is written in C++, it does not use property, so getting the getter and the setter will not be possible as you think, as ekhumoro points out, something close to what you want is to use q-properties
– eyllanesc
Nov 19 '18 at 16:51
Perhaps I should have clarified that I will be using Qt widgets for my binding.
– Elcid_91
Nov 19 '18 at 16:52
@Elcid_91 that does not change anything, the Qt Widgets are QObject that you also have the q-properties
– eyllanesc
Nov 19 '18 at 16:53
@Elcid_91 A possible option is that you inherit the widgets and establish those properties.
– eyllanesc
Nov 19 '18 at 16:55
|
show 1 more comment
In an attempt to tech myself a method for binding in python, I have the following code:
wname = QLineEdit()
dname = "Test Value"
sattr = getattr(wname, "text")
How to assign the value of dname to wname using the info from above?
I do understand that setText() would typically be used here; however, I am exploring a method for binding and I would like to know how to utilitze the getattr()
function for extracting the setter and getter for the widget passed. Thanks!!!
python python-3.x pyqt pyqt5
In an attempt to tech myself a method for binding in python, I have the following code:
wname = QLineEdit()
dname = "Test Value"
sattr = getattr(wname, "text")
How to assign the value of dname to wname using the info from above?
I do understand that setText() would typically be used here; however, I am exploring a method for binding and I would like to know how to utilitze the getattr()
function for extracting the setter and getter for the widget passed. Thanks!!!
python python-3.x pyqt pyqt5
python python-3.x pyqt pyqt5
edited Nov 19 '18 at 16:52
eyllanesc
78.3k103156
78.3k103156
asked Nov 19 '18 at 16:27
Elcid_91Elcid_91
4931722
4931722
1
setattr(wname, 'text', dname)
orgetattr(wname, 'setText')(dname)
. The former only really makes sense if the attribute is a qt property.
– ekhumoro
Nov 19 '18 at 16:41
pyqt is written in C++, it does not use property, so getting the getter and the setter will not be possible as you think, as ekhumoro points out, something close to what you want is to use q-properties
– eyllanesc
Nov 19 '18 at 16:51
Perhaps I should have clarified that I will be using Qt widgets for my binding.
– Elcid_91
Nov 19 '18 at 16:52
@Elcid_91 that does not change anything, the Qt Widgets are QObject that you also have the q-properties
– eyllanesc
Nov 19 '18 at 16:53
@Elcid_91 A possible option is that you inherit the widgets and establish those properties.
– eyllanesc
Nov 19 '18 at 16:55
|
show 1 more comment
1
setattr(wname, 'text', dname)
orgetattr(wname, 'setText')(dname)
. The former only really makes sense if the attribute is a qt property.
– ekhumoro
Nov 19 '18 at 16:41
pyqt is written in C++, it does not use property, so getting the getter and the setter will not be possible as you think, as ekhumoro points out, something close to what you want is to use q-properties
– eyllanesc
Nov 19 '18 at 16:51
Perhaps I should have clarified that I will be using Qt widgets for my binding.
– Elcid_91
Nov 19 '18 at 16:52
@Elcid_91 that does not change anything, the Qt Widgets are QObject that you also have the q-properties
– eyllanesc
Nov 19 '18 at 16:53
@Elcid_91 A possible option is that you inherit the widgets and establish those properties.
– eyllanesc
Nov 19 '18 at 16:55
1
1
setattr(wname, 'text', dname)
or getattr(wname, 'setText')(dname)
. The former only really makes sense if the attribute is a qt property.– ekhumoro
Nov 19 '18 at 16:41
setattr(wname, 'text', dname)
or getattr(wname, 'setText')(dname)
. The former only really makes sense if the attribute is a qt property.– ekhumoro
Nov 19 '18 at 16:41
pyqt is written in C++, it does not use property, so getting the getter and the setter will not be possible as you think, as ekhumoro points out, something close to what you want is to use q-properties
– eyllanesc
Nov 19 '18 at 16:51
pyqt is written in C++, it does not use property, so getting the getter and the setter will not be possible as you think, as ekhumoro points out, something close to what you want is to use q-properties
– eyllanesc
Nov 19 '18 at 16:51
Perhaps I should have clarified that I will be using Qt widgets for my binding.
– Elcid_91
Nov 19 '18 at 16:52
Perhaps I should have clarified that I will be using Qt widgets for my binding.
– Elcid_91
Nov 19 '18 at 16:52
@Elcid_91 that does not change anything, the Qt Widgets are QObject that you also have the q-properties
– eyllanesc
Nov 19 '18 at 16:53
@Elcid_91 that does not change anything, the Qt Widgets are QObject that you also have the q-properties
– eyllanesc
Nov 19 '18 at 16:53
@Elcid_91 A possible option is that you inherit the widgets and establish those properties.
– eyllanesc
Nov 19 '18 at 16:55
@Elcid_91 A possible option is that you inherit the widgets and establish those properties.
– eyllanesc
Nov 19 '18 at 16:55
|
show 1 more comment
2 Answers
2
active
oldest
votes
Not sure if I got the point, but by doing
sattr = getattr(wname, "text")
you are retrieving the function and not the text inside it. Therefore by calling
sattr("abc")
you will set the text. Same for writing:
sattr = gettar(wname, "setText")
sattr("Hello World!")
Yes, I am trying to receive the function, then utilize the retrieved function to set the value. There will be different widgets and I need to retrieve their individual setter and getter methods.
– Elcid_91
Nov 19 '18 at 16:43
add a comment |
Similarly to what Alessandro said, you need to get the setter with getattr
first, then call it with your text:
wname = QLineEdit()
dname = "Test Value"
sattr = getattr(wname, "setText")
sattr(dname)
If you want to build some code that gets both the getters and setters based on a property name, I guess you could do something like this:
def get_getters_and_setters(widget, name):
return getattr(widget, name), getattr(widget, 'set' + name[0].upper() + name[1:]
If, instead, you want something similar to the way getattr
and setattr
work with Python attributes but for already existing widget properties, I guess you could do something like the following, but it seems a bit of an overkill to me:
class CustomLineEdit(QtWidgets.QLineEdit):
@property
def textAttribute(self):
return self.text()
@textAttribute.setter
def textAttribute(self, value):
self.setText(value)
wname = CustomLineEdit()
dname = "Test Value"
setattr(wname, 'textAttribute', dname)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378917%2fpyqt5-getters-and-setters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not sure if I got the point, but by doing
sattr = getattr(wname, "text")
you are retrieving the function and not the text inside it. Therefore by calling
sattr("abc")
you will set the text. Same for writing:
sattr = gettar(wname, "setText")
sattr("Hello World!")
Yes, I am trying to receive the function, then utilize the retrieved function to set the value. There will be different widgets and I need to retrieve their individual setter and getter methods.
– Elcid_91
Nov 19 '18 at 16:43
add a comment |
Not sure if I got the point, but by doing
sattr = getattr(wname, "text")
you are retrieving the function and not the text inside it. Therefore by calling
sattr("abc")
you will set the text. Same for writing:
sattr = gettar(wname, "setText")
sattr("Hello World!")
Yes, I am trying to receive the function, then utilize the retrieved function to set the value. There will be different widgets and I need to retrieve their individual setter and getter methods.
– Elcid_91
Nov 19 '18 at 16:43
add a comment |
Not sure if I got the point, but by doing
sattr = getattr(wname, "text")
you are retrieving the function and not the text inside it. Therefore by calling
sattr("abc")
you will set the text. Same for writing:
sattr = gettar(wname, "setText")
sattr("Hello World!")
Not sure if I got the point, but by doing
sattr = getattr(wname, "text")
you are retrieving the function and not the text inside it. Therefore by calling
sattr("abc")
you will set the text. Same for writing:
sattr = gettar(wname, "setText")
sattr("Hello World!")
answered Nov 19 '18 at 16:41
Alessandro I.Alessandro I.
173
173
Yes, I am trying to receive the function, then utilize the retrieved function to set the value. There will be different widgets and I need to retrieve their individual setter and getter methods.
– Elcid_91
Nov 19 '18 at 16:43
add a comment |
Yes, I am trying to receive the function, then utilize the retrieved function to set the value. There will be different widgets and I need to retrieve their individual setter and getter methods.
– Elcid_91
Nov 19 '18 at 16:43
Yes, I am trying to receive the function, then utilize the retrieved function to set the value. There will be different widgets and I need to retrieve their individual setter and getter methods.
– Elcid_91
Nov 19 '18 at 16:43
Yes, I am trying to receive the function, then utilize the retrieved function to set the value. There will be different widgets and I need to retrieve their individual setter and getter methods.
– Elcid_91
Nov 19 '18 at 16:43
add a comment |
Similarly to what Alessandro said, you need to get the setter with getattr
first, then call it with your text:
wname = QLineEdit()
dname = "Test Value"
sattr = getattr(wname, "setText")
sattr(dname)
If you want to build some code that gets both the getters and setters based on a property name, I guess you could do something like this:
def get_getters_and_setters(widget, name):
return getattr(widget, name), getattr(widget, 'set' + name[0].upper() + name[1:]
If, instead, you want something similar to the way getattr
and setattr
work with Python attributes but for already existing widget properties, I guess you could do something like the following, but it seems a bit of an overkill to me:
class CustomLineEdit(QtWidgets.QLineEdit):
@property
def textAttribute(self):
return self.text()
@textAttribute.setter
def textAttribute(self, value):
self.setText(value)
wname = CustomLineEdit()
dname = "Test Value"
setattr(wname, 'textAttribute', dname)
add a comment |
Similarly to what Alessandro said, you need to get the setter with getattr
first, then call it with your text:
wname = QLineEdit()
dname = "Test Value"
sattr = getattr(wname, "setText")
sattr(dname)
If you want to build some code that gets both the getters and setters based on a property name, I guess you could do something like this:
def get_getters_and_setters(widget, name):
return getattr(widget, name), getattr(widget, 'set' + name[0].upper() + name[1:]
If, instead, you want something similar to the way getattr
and setattr
work with Python attributes but for already existing widget properties, I guess you could do something like the following, but it seems a bit of an overkill to me:
class CustomLineEdit(QtWidgets.QLineEdit):
@property
def textAttribute(self):
return self.text()
@textAttribute.setter
def textAttribute(self, value):
self.setText(value)
wname = CustomLineEdit()
dname = "Test Value"
setattr(wname, 'textAttribute', dname)
add a comment |
Similarly to what Alessandro said, you need to get the setter with getattr
first, then call it with your text:
wname = QLineEdit()
dname = "Test Value"
sattr = getattr(wname, "setText")
sattr(dname)
If you want to build some code that gets both the getters and setters based on a property name, I guess you could do something like this:
def get_getters_and_setters(widget, name):
return getattr(widget, name), getattr(widget, 'set' + name[0].upper() + name[1:]
If, instead, you want something similar to the way getattr
and setattr
work with Python attributes but for already existing widget properties, I guess you could do something like the following, but it seems a bit of an overkill to me:
class CustomLineEdit(QtWidgets.QLineEdit):
@property
def textAttribute(self):
return self.text()
@textAttribute.setter
def textAttribute(self, value):
self.setText(value)
wname = CustomLineEdit()
dname = "Test Value"
setattr(wname, 'textAttribute', dname)
Similarly to what Alessandro said, you need to get the setter with getattr
first, then call it with your text:
wname = QLineEdit()
dname = "Test Value"
sattr = getattr(wname, "setText")
sattr(dname)
If you want to build some code that gets both the getters and setters based on a property name, I guess you could do something like this:
def get_getters_and_setters(widget, name):
return getattr(widget, name), getattr(widget, 'set' + name[0].upper() + name[1:]
If, instead, you want something similar to the way getattr
and setattr
work with Python attributes but for already existing widget properties, I guess you could do something like the following, but it seems a bit of an overkill to me:
class CustomLineEdit(QtWidgets.QLineEdit):
@property
def textAttribute(self):
return self.text()
@textAttribute.setter
def textAttribute(self, value):
self.setText(value)
wname = CustomLineEdit()
dname = "Test Value"
setattr(wname, 'textAttribute', dname)
answered Nov 19 '18 at 17:04
FernAndrFernAndr
399213
399213
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378917%2fpyqt5-getters-and-setters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
setattr(wname, 'text', dname)
orgetattr(wname, 'setText')(dname)
. The former only really makes sense if the attribute is a qt property.– ekhumoro
Nov 19 '18 at 16:41
pyqt is written in C++, it does not use property, so getting the getter and the setter will not be possible as you think, as ekhumoro points out, something close to what you want is to use q-properties
– eyllanesc
Nov 19 '18 at 16:51
Perhaps I should have clarified that I will be using Qt widgets for my binding.
– Elcid_91
Nov 19 '18 at 16:52
@Elcid_91 that does not change anything, the Qt Widgets are QObject that you also have the q-properties
– eyllanesc
Nov 19 '18 at 16:53
@Elcid_91 A possible option is that you inherit the widgets and establish those properties.
– eyllanesc
Nov 19 '18 at 16:55