1 import sys
2 from PyQt5.QtWidgets import *
3 from PyQt5.QtCore import Qt, pyqtSignal
4
5 class FindDialog(QDialog):
6 findNext = pyqtSignal(str, Qt.CaseSensitivity)
7 findPrevious = pyqtSignal(str, Qt.CaseSensitivity)
8
9 def __init__(self, parent = None):
10 super().__init__(parent)
11 self.label = QLabel(self.tr("Find &what:"))
12 self.lineEdit = QLineEdit()
13 self.label.setBuddy(self.lineEdit)
14
15 self.caseCheckBox = QCheckBox(self.tr("Match &case"))
16 self.backwardCheckBox = QCheckBox(self.tr("Search &backward"))
17
18 self.findButton = QPushButton(self.tr("&Find"))
19 self.findButton.setDefault(True)
20 self.findButton.setEnabled(False)
21
22 self.closeButton = QPushButton(self.tr("Close"))
23 self.lineEdit.textChanged.connect(self.enableFindButton)
24 self.findButton.clicked.connect(self.findClicked)
25 self.closeButton.clicked.connect(self.close)
26
27 self.topLeftLayout = QHBoxLayout()
28 self.topLeftLayout.addWidget(self.label)
29 self.topLeftLayout.addWidget(self.lineEdit)
30
31 self.leftLayout = QVBoxLayout()
32 self.leftLayout.addLayout(self.topLeftLayout)
33 self.leftLayout.addWidget(self.caseCheckBox)
34 self.leftLayout.addWidget(self.backwardCheckBox)
35
36 self.rightLayout = QVBoxLayout()
37 self.rightLayout.addWidget(self.findButton)
38 self.rightLayout.addWidget(self.closeButton)
39 self.rightLayout.addStretch()
40
41 self.mainLayout = QHBoxLayout()
42 self.mainLayout.addLayout(self.leftLayout)
43 self.mainLayout.addLayout(self.rightLayout)
44 self.setLayout(self.mainLayout)
45 self.setWindowTitle(self.tr("Find"))
46 self.setFixedHeight(self.sizeHint().height())
47
48 def findClicked(self):
49 text = self.lineEdit.text()
50 cs = (Qt.CaseSensitive if self.caseCheckBox.isChecked()
51 else Qt.CaseInsensitive)
52
53 if self.backwardCheckBox.isChecked():
54 self.findPrevious.emit(text, cs)
55 else:
56 self.findNext.emit(text, cs)
57
58 def enableFindButton(self, text):
59 self.findButton.setEnabled(not text == '')
60
61 if __name__ == '__main__':
62 app = QApplication(sys.argv)
63 dialog = FindDialog()
64 dialog.show()
65 sys.exit(app.exec())