import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, pyqtSignal
class FindDialog(QDialog):
findNext = pyqtSignal(str, Qt.CaseSensitivity)
findPrevious = pyqtSignal(str, Qt.CaseSensitivity)
def __init__(self, parent = None):
super().__init__(parent)
self.label = QLabel(self.tr("Find &what:"))
self.lineEdit = QLineEdit()
self.label.setBuddy(self.lineEdit)
self.caseCheckBox = QCheckBox(self.tr("Match &case"))
self.backwardCheckBox = QCheckBox(self.tr("Search &backward"))
self.findButton = QPushButton(self.tr("&Find"))
self.findButton.setDefault(True)
self.findButton.setEnabled(False)
self.closeButton = QPushButton(self.tr("Close"))
self.lineEdit.textChanged.connect(self.enableFindButton)
self.findButton.clicked.connect(self.findClicked)
self.closeButton.clicked.connect(self.close)
self.topLeftLayout = QHBoxLayout()
self.topLeftLayout.addWidget(self.label)
self.topLeftLayout.addWidget(self.lineEdit)
self.leftLayout = QVBoxLayout()
self.leftLayout.addLayout(self.topLeftLayout)
self.leftLayout.addWidget(self.caseCheckBox)
self.leftLayout.addWidget(self.backwardCheckBox)
self.rightLayout = QVBoxLayout()
self.rightLayout.addWidget(self.findButton)
self.rightLayout.addWidget(self.closeButton)
self.rightLayout.addStretch()
self.mainLayout = QHBoxLayout()
self.mainLayout.addLayout(self.leftLayout)
self.mainLayout.addLayout(self.rightLayout)
self.setLayout(self.mainLayout)
self.setWindowTitle(self.tr("Find"))
self.setFixedHeight(self.sizeHint().height())
def findClicked(self):
text = self.lineEdit.text()
cs = (Qt.CaseSensitive if self.caseCheckBox.isChecked()
else Qt.CaseInsensitive)
if self.backwardCheckBox.isChecked():
self.findPrevious.emit(text, cs)
else:
self.findNext.emit(text, cs)
def enableFindButton(self, text):
self.findButton.setEnabled(not text == '')
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = FindDialog()
dialog.show()
sys.exit(app.exec())