Example of PyQt5 window switch and custom dialog

  • 2021-06-29 11:31:32
  • OfStack

Recently, we need to implement a small and full-featured desktop software, so we selected and tried PyQt5, the GUI library.In use, I find that its functions are complete, but there is not much information about it. Sometimes I can not find the information about the functions I want to achieve, or I have to read the implementation code of C++.Documents from PyQt5 are also those directed to the C++ version.1. After a period of time, we will record the relevant solutions for future reference, and also help the latter.

Generally speaking, GUI software needs to switch between different work interfaces, of course, the use of layout and other functions.The layout function of Qt is easy to understand and the basic method used is:

1. Establish container parts;

2. Establish layout classes and set them as the layout of container parts;

3. Add other parts to the layout class.

If nesting is required, add container parts in step 3 above and go back to step 1.I believe this is easier to understand.

Once all the work interfaces are finished, what if you let the top-level multiple parts switch in the window?I spent a lot of time working on this method.Looking for methods in the QMainWindow class, there are a lot of methods in the class, because the setCentralWidget () is used to add parts, which naturally makes me wonder if it is removeCentralWidget () or not!!Finally, takeCentralWidget () was found. (Is he, is he, is he...)

This makes it clear that it is easy to switch parts by calling takeCentralWidget (), clearing the original parts, and then calling setCentralWidget () to set the current parts.

To make an GUI interface, dialogs are naturally essential.For Qt5, there are also sites like https://www.ofstack.com/article/163557.htm (the pop-up window of PyQt5), which is easy to understand.But I need to implement a dialog box where I can fill in two pieces of data. That's just customization!To be honest, this online resource is scarce.Finally, it is implemented, and the basic code is as follows (see notes for explanations):


class MyDialog(QDialog):    # inherit QDialog class 
 def __init__(self):
  super().__init__()
  self.initUI()
  # self.exec()

 def initUI(self):
  self.setWindowTitle(" New Team ")  #  Window Title 
  self.setGeometry(400,400,200,200) #  Window position and size 

  self.lab_a = QLabel(' Group Name :')
  self.lab_b = QLabel(' Competition Items :')

  self.name_edit = QLineEdit()  #  Single-line text input box to receive user input 
  self.game_item = QComboBox()  #  establish 1 Drop-down list boxes 

  for g in get_games():    #  Add a selection to the drop-down list box (retrieved from a query in the database) 
   self.game_item.addItem(g.name,g.id)

  self.buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) # Create confirmation and cancellation buttons in the window 

  self.glayout = QGridLayout()

  self.glayout.addWidget(self.lab_a,0,0)
  self.glayout.addWidget(self.lab_b,1,0)
  self.glayout.addWidget(self.name_edit,0,1)
  self.glayout.addWidget(self.game_item,1,1)

  self.glayout.addWidget(self.buttons,2,1)

  self.buttons.accepted.connect(self.accept)
  self.buttons.rejected.connect(self.reject)

  self.setLayout(self.glayout)

 def get_data(self):     #  Define how to get user input data 
  return self.name_edit.text(),self.game_item.itemData(self.game_item.currentIndex())

It's also easy to use, with the following code examples:


v = MyDialog() #  Create dialog instance 
if v.exec_(): #  Execute method, become modal dialog, user clicks OK After that, return 1
 name,game = v.get_data()

Related articles: