PyQt5 Getters and Setters












0















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!!!










share|improve this question




















  • 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













  • 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
















0















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!!!










share|improve this question




















  • 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













  • 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














0












0








0








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!!!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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













  • 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





    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













  • 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












2 Answers
2






active

oldest

votes


















0














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!")





share|improve this answer
























  • 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





















0














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)





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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!")





    share|improve this answer
























    • 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


















    0














    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!")





    share|improve this answer
























    • 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
















    0












    0








    0







    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!")





    share|improve this answer













    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!")






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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





















    • 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















    0














    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)





    share|improve this answer




























      0














      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)





      share|improve this answer


























        0












        0








        0







        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)





        share|improve this answer













        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 17:04









        FernAndrFernAndr

        399213




        399213






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()