Custom dialogs

We saw some examples of the built-in dialogs in the previous section. The need may arise in a real application scenario to define and create a custom dialog that is based on the user's requirement. Qt provides the support to create custom-based dialogs and use it, in addition to the various built-in dialogs. In this section, we are going to explore how to create a custom Find dialog for our text editor application that we created in Chapter 3, Main Windows and Layout Management. The Find Dialog class is inherited from the QDialog class and defines the properties to implement search functionality. Once the find dialog functions are defined, it can be added to our text editor application, and the slots are implemented accordingly.

In order to create a Find dialog, we must create an outline of what it will look like. The following is a sample look of how we would want our Find dialog to appear:

Custom dialogs

This is a very simple Find dialog. We would want to capture the text that has to be searched using a line edit dialog. The two checkboxes, Match Case and Search Backward, try to catch the user's choices. The button press events are connected to specific slots to perform the desired action. We used the layout concepts that were introduced to you in the previous chapter to place the widgets:

class FindDialog(QDialog):
    
    def __init__(self):
        QDialog.__init__(self)
        self.findLabel = QLabel("Find &What:")
        self.lineEdit = QLineEdit()
        self.findLabel.setBuddy(self.lineEdit)

        self.caseCheckBox = QCheckBox("Match &Case")
        self.backwardCheckBox = QCheckBox("Search &Backward")

        self.findButton = QPushButton("&Find")
        self.findButton.setDefault(True)
        self.closeButton = QPushButton("Close")

        self.topLeftLayout = QHBoxLayout()
        self.topLeftLayout.addWidget(self.findLabel)
        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.findButton.clicked.connect(self.findText)
        self.setWindowTitle("Find")
        self.setLayout(self.mainLayout)
        self.show()

    def findText(self):
        mySearchText = self.lineEdit.text()
        if self.caseCheckBox.isChecked():
            caseSensitivity = Qt.CaseSensitive
        else:
            caseSensitivity = Qt.CaseInsensitive
        if self.backwardCheckBox.isChecked():
            #search functionality goes here...
            print("Backward Find ")
        else:
            #search functionality goes here...
            print("Forward Find")

In order to use this dialog, simply create an instance of MyFindDialog that will create a Find dialog, as shown in the following lines of code.

def findDialog(self):
         myFindDialog = FindDialog()
         myFindDialog.exec_()

Thus, we can create and customize the dialog according to the needs of our application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset