Custom dialogs

We have seen 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 based on the user's requirement. Qt provides the support for creating 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 have created in Chapter 3, Main Windows and Layout Management. The FindDialog class is inherited from the QDialog class and defines the properties for implementing a search function. 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 how it looks. The following screenshot 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 to be searched by using a line edit dialog. The two checkboxes, Match Case and Search Backward try to catch the user's choices. The button click events are connected to specific slots to perform the desired action. We have used the layout concepts 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 below lines of code.
def findDialog(self):
     myFindDialog = FindDialog()
     myFindDialog.exec_()

Thus, we can create and customize 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
3.143.235.23