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.
  • S Offline
    S Offline
    Stainopel
    wrote on 22 Nov 2020, 00:25 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) ")
    
    P 1 Reply Last reply 22 Nov 2020, 03:07
    0
    • S Stainopel
      22 Nov 2020, 00:25

      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) ")
      
      P Offline
      P Offline
      Pl45m4
      wrote on 22 Nov 2020, 03:07 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

      S 1 Reply Last reply 22 Nov 2020, 09:30
      5
      • P Pl45m4
        22 Nov 2020, 03:07

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

        S Offline
        S Offline
        Stainopel
        wrote on 22 Nov 2020, 09:30 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

        M 1 Reply Last reply 22 Nov 2020, 09:38
        0
        • S Stainopel
          22 Nov 2020, 09:30

          @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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 22 Nov 2020, 09:38 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.

          S 1 Reply Last reply 22 Nov 2020, 10:00
          2
          • M mrjj
            22 Nov 2020, 09:38

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

            S Offline
            S Offline
            Stainopel
            wrote on 22 Nov 2020, 10:00 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'

            M P 2 Replies Last reply 22 Nov 2020, 10:12
            0
            • S Stainopel
              22 Nov 2020, 10:00

              @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'

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 22 Nov 2020, 10:12 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 )

              S 1 Reply Last reply 22 Nov 2020, 10:45
              2
              • M mrjj
                22 Nov 2020, 10:12

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

                S Offline
                S Offline
                Stainopel
                wrote on 22 Nov 2020, 10:45 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
                • S Stainopel
                  22 Nov 2020, 10:00

                  @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'

                  P Offline
                  P Offline
                  Pl45m4
                  wrote on 22 Nov 2020, 12:45 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

                  S 1 Reply Last reply 22 Nov 2020, 15:45
                  2
                  • P Pl45m4
                    22 Nov 2020, 12:45

                    @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

                    S Offline
                    S Offline
                    Stainopel
                    wrote on 22 Nov 2020, 15:45 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

                    5/9

                    22 Nov 2020, 10:00

                    • Login

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