Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Comparing two strings in different widgets
QtWS25 Last Chance

Comparing two strings in different widgets

Scheduled Pinned Locked Moved Solved General and Desktop
pyqt5python3
9 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • StainopelS Offline
    StainopelS Offline
    Stainopel
    wrote on last edited by Stainopel
    #1

    Am trying to compare two strings in two widgets ( a text browser and a line edit) using pyqt5 and return "same files" or "different files" if the strings are the same or different, in another widget, but I can't seem to get it right. When I execute my GUI, when I click the compare button using this method, it keeps executing only the "else" part even when the two strings contained in the two widgets are exactly the same. Below is my code snippet, any help will be welcome

    def hashComp(self):

        v1 = self.textBrowser
        v2 = self.textBrowser_2 
    
        if v1 == v2 :
            self.textBrowser_3.setText("Same File(s) ")
        else:
            self.textBrowser_3.setText("Different File(s) ")
    
    Pl45m4P 1 Reply Last reply
    0
    • StainopelS Stainopel

      Am trying to compare two strings in two widgets ( a text browser and a line edit) using pyqt5 and return "same files" or "different files" if the strings are the same or different, in another widget, but I can't seem to get it right. When I execute my GUI, when I click the compare button using this method, it keeps executing only the "else" part even when the two strings contained in the two widgets are exactly the same. Below is my code snippet, any help will be welcome

      def hashComp(self):

          v1 = self.textBrowser
          v2 = self.textBrowser_2 
      
          if v1 == v2 :
              self.textBrowser_3.setText("Same File(s) ")
          else:
              self.textBrowser_3.setText("Different File(s) ")
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Stainopel said in Comparing two strings in different widgets:

      v1 = self.textBrowser
      v2 = self.textBrowser_2

      What do you expect from comparing the widgets? :-)

      Try self.textBrowser.text()
      (Edit: toPlainText())


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      StainopelS 1 Reply Last reply
      5
      • Pl45m4P Pl45m4

        @Stainopel said in Comparing two strings in different widgets:

        v1 = self.textBrowser
        v2 = self.textBrowser_2

        What do you expect from comparing the widgets? :-)

        Try self.textBrowser.text()
        (Edit: toPlainText())

        StainopelS Offline
        StainopelS Offline
        Stainopel
        wrote on last edited by
        #3

        @Pl45m4 The two widgets contain the results of two different button execution. These results are strings, and I want to compare the content of the widgets if they are same or different. And print the corresponding results when I click on another button to display the results of the comparison

        mrjjM 1 Reply Last reply
        0
        • StainopelS Stainopel

          @Pl45m4 The two widgets contain the results of two different button execution. These results are strings, and I want to compare the content of the widgets if they are same or different. And print the corresponding results when I click on another button to display the results of the comparison

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Stainopel
          Hi
          As @Pl45m4 says you are comparing the widgets directly.
          You need to use the .text() property to get the text and compare text to text.

          StainopelS 1 Reply Last reply
          2
          • mrjjM mrjj

            @Stainopel
            Hi
            As @Pl45m4 says you are comparing the widgets directly.
            You need to use the .text() property to get the text and compare text to text.

            StainopelS Offline
            StainopelS Offline
            Stainopel
            wrote on last edited by Stainopel
            #5

            @mrjj Okay, but please can you illustrate? I have been trying to use .text() suggested but I keep getting an error

            v1 = self.textBrowser.text()
            AttributeError: 'QTextBrowser' object has no attribute 'text'

            mrjjM Pl45m4P 2 Replies Last reply
            0
            • StainopelS Stainopel

              @mrjj Okay, but please can you illustrate? I have been trying to use .text() suggested but I keep getting an error

              v1 = self.textBrowser.text()
              AttributeError: 'QTextBrowser' object has no attribute 'text'

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @Stainopel
              Ah, i checked the docs and you are right. its called
              https://doc.qt.io/qt-5/qtextedit.html#toPlainText

              ( QTextBrowser is a QTextEdit )

              StainopelS 1 Reply Last reply
              2
              • mrjjM mrjj

                @Stainopel
                Ah, i checked the docs and you are right. its called
                https://doc.qt.io/qt-5/qtextedit.html#toPlainText

                ( QTextBrowser is a QTextEdit )

                StainopelS Offline
                StainopelS Offline
                Stainopel
                wrote on last edited by
                #7

                @mrjj Thanks so much for the documentation, it was a pointer in the right direction. I did find what I needed to make it work. Here is an upgrade to the code snippet, and it works real well now.

                    def hashComp(self):
                            v1 = self.textBrowser.toPlainText()
                            v2 = self.textBrowser_2.toPlainText()   
                            if v1 == v2 :
                                self.textBrowser_3.setText("Same File(s)")
                            else:
                               self.textBrowser_3.setText("Different File(s)")
                
                1 Reply Last reply
                2
                • StainopelS Stainopel

                  @mrjj Okay, but please can you illustrate? I have been trying to use .text() suggested but I keep getting an error

                  v1 = self.textBrowser.text()
                  AttributeError: 'QTextBrowser' object has no attribute 'text'

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #8

                  @Stainopel

                  Thought this is a QLineEdit since they all have the same name.

                  compare two strings in two widgets ( a text browser and a line edit)

                  This is why :) You said QTextBrowser and QLineEdit and they are all named textBrowser_x


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  StainopelS 1 Reply Last reply
                  2
                  • Pl45m4P Pl45m4

                    @Stainopel

                    Thought this is a QLineEdit since they all have the same name.

                    compare two strings in two widgets ( a text browser and a line edit)

                    This is why :) You said QTextBrowser and QLineEdit and they are all named textBrowser_x

                    StainopelS Offline
                    StainopelS Offline
                    Stainopel
                    wrote on last edited by
                    #9

                    @Pl45m4 yes I initially used a line edit but ended up changing it to a qtextedit instead. Then using the documentation I was able to get the expected results

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved