Recently, we need to implement a small and full-featured desktop version software, so we selected and tried PyQt5 GUI library. In use, it is found that its functions are indeed complete, but there is not much information in this regard. Sometimes the relevant information about the functions that I want to achieve can not be found, and some have to read the C++ implementation code. PyQt5 documents also refer to documents in C++ version. For a period of time, we will record the relevant solutions for future reference, and also help later generations.
Generally speaking, GUI software needs to be switched between different working interfaces, of course, the use of layout and other functions is indispensable. The layout function of Qt is easy to understand. The basic method used is:
1. Establishment of container components;
2. Set up the layout class and set it as the layout of container parts.
3. Adding other components to the layout class.
If there is a need for nesting, it is the addition of container components in Article 3 of the above steps, and back to Step 1. I believe this is easier to understand.
After each work interface has been completed, what if the top-level components are switched in the window? In order to do this, I have spent a lot of time. Looking for methods in the QMainWindow class, there are many methods in the class, because the add-on part uses setCentralWidget (), which naturally comes to mind whether it is removeCentralWidget (), and I am surprised that it is not!! Finally, take Central Widget () is found. (It’s him, it’s him, it’s him…)
That’s why it’s easy to switch parts. First, call takeCentralWidget (), clear the original parts, and then call setCentralWidget () to set the current parts.
To make GUI interface, dialog boxes are naturally essential. For Qt5, developpaer also has https://www.jb51.net/article/163557.htm (PyQt5 pop-up window), which is easy to understand. But I need to implement a dialog box that can fill in two data. That’s only customized. To be honest, this online resource is really scarce. Finally, it is implemented. The basic code is as follows (see comment for detailed explanation):
ClassMyDialog (QDialog): # Inheritance of the QDialog class def __init__(self): super().__init__() self.initUI() # self.exec() def initUI(self): Self.set Windows Title ("New Group") # Window Title Self. setGeometry (400, 400, 200, 200) Window location and size Self.lab_a = QLabel ('group name:') Self.lab_b = QLabel ('Competition Events:') Self.name_edit = QLineEdit ()# Single-line text input box for receiving user input Self. game_item = QComboBox ()# Create a drop-down list box For g in get_games(): # Add a selection to the drop-down list box (retrieved from a database query) self.game_item.addItem(g.name,g.id) Self. buttons = QDialogButtonBox (QDialogButtonBox. Ok | QDialogButtonBox. Cancel) # window to create confirmation and cancellation buttons 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): # Defines the method of obtaining user input data return self.name_edit.text(),self.game_item.itemData(self.game_item.currentIndex())
Its usage is not difficult, the code example is as follows:
V = MyDialog ()# Create a dialog instance If v.exec_(): Execution method becomes a modal dialog box. After the user clicks OK, he returns to 1. name,game = v.get_data()
The above example of PyQt5 window switching and custom dialog box is all the content shared by the editor. I hope to give you a reference, and I hope you will support developpaer more.