Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Unable to keep searching on QplainTextEdit while Qdialog is open
QtWS25 Last Chance

Unable to keep searching on QplainTextEdit while Qdialog is open

Scheduled Pinned Locked Moved Solved Qt for Python
6 Posts 3 Posters 272 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.
  • SayanS Offline
    SayanS Offline
    Sayan
    wrote on last edited by
    #1

    I am trying to replicate default Notepad find operation.
    screen-capture.gif

    But in my case the QDialog is getting closed after the first operation. Can somebody please help me in keeping the QDialog open while the MainWindow is performing the search operations or any alternative.

    Here is my code:

    class MainWindow(QMainWindow, Ui_Widget):
    def init(self, app):
    super().init()
    self.app = app
    self.setupUi(self)
    self.setWindowTitle( "Untitled - Notepad")

        self.actionFind.triggered.connect(self.find)
    
    
    def find(self):
        cursor = self.plainTextEdit.textCursor()
        selected_text = cursor.selectedText()
        if (selected_text != ""):
            self.text = selected_text
        else:
            clipboard = QClipboard()
            self.text = clipboard.text(QClipboard.Mode.Clipboard)
    
    
        self.find_dialog = FindDialog(self.text)
        ret = self.find_dialog.exec()
        if(ret==QDialog.Accepted):
            self.text = self.find_dialog.f_search_lineEdit.text()
            self.plainTextEdit.find(self.text)
    

    class FindDialog(QDialog, Ui_FindDialog):
    def init(self, text):
    super().init()
    self.setupUi(self)
    self.setWindowTitle("Find")

        self.search_text = text
    
        #Connections
        self.f_find_next_pushButton.clicked.connect(self.ok)
        self.f_cancel_find_pushButton.clicked.connect(self.cancel)
    
    
        self.f_search_lineEdit.setText(self.search_text)
    
    def cancel(self):
        self.reject()
    
    def ok(self):
        self.search_text = self.f_search_lineEdit.text()
        self.accept()
    

    Any help will be appreciated. Thank you in advance.

    JonBJ 1 Reply Last reply
    0
    • SayanS Sayan

      @JonB Thank you for your suggestion.

      Replacing self.find_dialog.exec() to self.find_dialog.show() is making QmainWindow accessible. And removing self.accept() kept QDialog open even after clicking on Find Next button. But still not sure how to perform the search operation.

      Is there a way I can access the QPlainTextEdit from QDialog? Or I should follow some different approach. Any suggestion is appreciated.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @Sayan
      You would have to pass the edit to the dialog, as a parameter or via a setter method.

      That is why I suggested above you might be better having the calling main window do the searching. Maybe have the dialog send a signal, including the string to search for, and the main window has a slot on it, does the searching and moves the cursor.

      1 Reply Last reply
      1
      • SayanS Sayan

        I am trying to replicate default Notepad find operation.
        screen-capture.gif

        But in my case the QDialog is getting closed after the first operation. Can somebody please help me in keeping the QDialog open while the MainWindow is performing the search operations or any alternative.

        Here is my code:

        class MainWindow(QMainWindow, Ui_Widget):
        def init(self, app):
        super().init()
        self.app = app
        self.setupUi(self)
        self.setWindowTitle( "Untitled - Notepad")

            self.actionFind.triggered.connect(self.find)
        
        
        def find(self):
            cursor = self.plainTextEdit.textCursor()
            selected_text = cursor.selectedText()
            if (selected_text != ""):
                self.text = selected_text
            else:
                clipboard = QClipboard()
                self.text = clipboard.text(QClipboard.Mode.Clipboard)
        
        
            self.find_dialog = FindDialog(self.text)
            ret = self.find_dialog.exec()
            if(ret==QDialog.Accepted):
                self.text = self.find_dialog.f_search_lineEdit.text()
                self.plainTextEdit.find(self.text)
        

        class FindDialog(QDialog, Ui_FindDialog):
        def init(self, text):
        super().init()
        self.setupUi(self)
        self.setWindowTitle("Find")

            self.search_text = text
        
            #Connections
            self.f_find_next_pushButton.clicked.connect(self.ok)
            self.f_cancel_find_pushButton.clicked.connect(self.cancel)
        
        
            self.f_search_lineEdit.setText(self.search_text)
        
        def cancel(self):
            self.reject()
        
        def ok(self):
            self.search_text = self.f_search_lineEdit.text()
            self.accept()
        

        Any help will be appreciated. Thank you in advance.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @Sayan said in Unable to keep searching on QplainTextEdit while Qdialog is open:

        But in my case the QDialog is getting closed after the first operation.

        In FindDialog you connect to cancel or ok and each of these calls reject() or accept(). So that will close the dialog. Is that what you mean? Find Next should not cause the dialog to close, it does not represent an "OK"/"accept" operation.

        Instead, at a guess, it should probably send a signal which the main window has a slot on. That slot would do the find and move the highlight there. Or, perhaps, the dialog would do the finding if it has access to the document.

        I notice in Notepad that while the Find dialog is open the user can still perform editing etc. operations on the document. That would mean you need to change your dialog to being modeless, i.e. do not call exec().

        Please use the forum's Code tags (</> icon button) around code blocks, especially if Python.

        1 Reply Last reply
        1
        • SayanS Offline
          SayanS Offline
          Sayan
          wrote on last edited by
          #3

          @JonB Thank you for your suggestion.

          Replacing self.find_dialog.exec() to self.find_dialog.show() is making QmainWindow accessible. And removing self.accept() kept QDialog open even after clicking on Find Next button. But still not sure how to perform the search operation.

          Is there a way I can access the QPlainTextEdit from QDialog? Or I should follow some different approach. Any suggestion is appreciated.

          JonBJ SGaistS 2 Replies Last reply
          0
          • SayanS Sayan

            @JonB Thank you for your suggestion.

            Replacing self.find_dialog.exec() to self.find_dialog.show() is making QmainWindow accessible. And removing self.accept() kept QDialog open even after clicking on Find Next button. But still not sure how to perform the search operation.

            Is there a way I can access the QPlainTextEdit from QDialog? Or I should follow some different approach. Any suggestion is appreciated.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Sayan
            You would have to pass the edit to the dialog, as a parameter or via a setter method.

            That is why I suggested above you might be better having the calling main window do the searching. Maybe have the dialog send a signal, including the string to search for, and the main window has a slot on it, does the searching and moves the cursor.

            1 Reply Last reply
            1
            • SayanS Sayan

              @JonB Thank you for your suggestion.

              Replacing self.find_dialog.exec() to self.find_dialog.show() is making QmainWindow accessible. And removing self.accept() kept QDialog open even after clicking on Find Next button. But still not sure how to perform the search operation.

              Is there a way I can access the QPlainTextEdit from QDialog? Or I should follow some different approach. Any suggestion is appreciated.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Sayan hi,

              The suggestion made by @JonB is the right way to implement the search. The dialog shall not care about the widget that does the actual search. This allows you to reuse the same dialog in different situation.

              So as @JonB suggested, emit a signal from the dialog that contains the search string and do the search by the widget that manages the QTextEdit.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              SayanS 1 Reply Last reply
              1
              • SGaistS SGaist

                @Sayan hi,

                The suggestion made by @JonB is the right way to implement the search. The dialog shall not care about the widget that does the actual search. This allows you to reuse the same dialog in different situation.

                So as @JonB suggested, emit a signal from the dialog that contains the search string and do the search by the widget that manages the QTextEdit.

                SayanS Offline
                SayanS Offline
                Sayan
                wrote on last edited by
                #6

                Thank you @JonB and @SGaist for directing me in the right direction. I heartily appreciate you both.

                1 Reply Last reply
                0
                • SayanS Sayan has marked this topic as solved on

                • Login

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