PyQt5 Learning Record (5) - - Basic Comprehensive Use of QDockWidget and QListWidget

Keywords: Qt

brief introduction

This article introduces what QDockWidget is and how it is used. By the way, it introduces a demo integrated with QListWidget.

QDockWidget

QDockWidget is a common style of software interaction, as shown in the following figure:

The VCS Operations box in the figure above can be considered a Dock Widget, and the most important feature is that it can be moved. And it can be inlaid into the main window. When your software needs such a control that can be levitated without affecting the main interface space, similar to global navigation, it is best to use DockWidget. First up the source code:

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QDockWidget, QListWidget
from PyQt5.QtCore import Qt


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.items = ['Ha-ha', 'aa', 'bb', 'cc', 'dd', 'ee', 'ff','g', 'h', 'i', 'j', 'k', 'l', 'm'
                 ,'m','n','o','p','q','r','s','t']
        self.init()
        self.addDock()


    def init(self):
        self.text = QTextEdit('main window')
        self.text.setAlignment(Qt.AlignCenter)
        self.setCentralWidget(self.text)

        self.setGeometry(200, 200, 800, 400)
        self.setWindowTitle('QDockWidget Example')
        self.show()
        pass

    def onDockListIndexChanged(self, index):
        item = self.items[index]
        self.text.setText(item)
        pass

    def addDock(self):
        dock1 = QDockWidget('DockWidget')
        dock1.setFeatures(QDockWidget.DockWidgetFloatable)
        dock1.setAllowedAreas(Qt.LeftDockWidgetArea)
        listwidget = QListWidget()

        listwidget.addItems(self.items)
        listwidget.currentRowChanged.connect(self.onDockListIndexChanged)
        dock1.setWidget(listwidget)
        self.addDockWidget(Qt.LeftDockWidgetArea, dock1)


def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())


# Entrance
if __name__ == '__main__':
    main()

Key Source Code Points

  1. QDockWidget does not occupy the Layout of the main layout, it just needs to attach to QMainWindow. The addition can be completed by calling addDockWidget() directly in QMainWindow.
  2. Create a QDockWidget first, and pass in the title of the DockWidget. Then call the setFeatures() function to set the properties of the DockWidget.
    When set to QDockWidget. DockWidget Floatable, double-click will float out and can move anywhere, as shown below:

    When you double-click the border of the DockWidget, it will float out, as shown in the following figure:

    If you set up DockWidget Closable, the following is true (there is an x number in the upper left corner to close):

    Other attributes are not introduced, you can run to see the effect, all the attributes are as follows:
    AllDockWidgetFeatures = 7
    DockWidgetClosable = 1
    DockWidgetFloatable = 4
    DockWidgetMovable = 2
    DockWidgetVerticalTitleBar = 8
    NoDockWidgetFeatures = 0

Then call setAllowedAreas() to set where the DockWidget can be located on the main page, from top to bottom.
3. Then call setWidget() to set the content that you want to display in the DockWidget. Finally, it is added through QMainWindow's addDockWidget(), where the first parameter is the initial location of the DockWidget.
4. We have a new one here. QListWidget Then call listwidget.currentRowChanged.connect(self.onDockListIndexChanged) to pass the click signal of the list to the custom function, which by default passes the index of the List. Of course, you can also call the current TextChanged signal, which sends out a String.

Posted by biffjo on Fri, 07 Jun 2019 12:35:11 -0700