Unable to keep searching on QplainTextEdit while Qdialog is open
-
I am trying to replicate default Notepad find operation.
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.
-
@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.
-
@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 tocancel
orok
and each of these callsreject()
oraccept()
. 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. -
@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.
-
@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.
-
@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.
-
S Sayan has marked this topic as solved on