SoFunction
Updated on 2024-10-29

PyQt5 QDockWidget control application details

preamble

QDockWidget is a window control that can be docked inside a QMainWindow. It can be kept floating or attached to the main window as a child window at a specified location. The main window object of the QMainWindow class retains an area for docking the window around the center of the control.

Common methods in the QDoCKWidget class

methodologies descriptive
setWidget() Setting up QWidgets in the Dock window area
setFloating() Sets whether the Dock window can be floated, if set to True, it means it can be floated.
setAlllowedAreas() Setting the area where windows can be docked
LeftDockWidgetArea:Left Docking Area
RightDockWidgetArea:Right docking area
TopDockWidgetArea:Top dock area
BottomDockWidgetArea:Bottom dock area
NoDockWidgetArea: do not show widgets
setFearures() Setting Docking Window Functional Properties
DockWidgetClosable:Closable
DockWidgetMovable: Movable
DockWidgetFloatable: floatable
DockWidgetVerticalTitleBar: show vertical title bar on the left side
AllDockWidgetFeatures:has all the features of the first three properties
NoDockWidgetFeatures:can't close, can't hover, can't move

Example: Using QDockWidgets

import sys
from  import *
from  import *
from  import *

class DockDemo(QMainWindow):
  def __init__(self,parent=None):
    super(DockDemo, self).__init__(parent)
    # Setting up a horizontal layout
    layout=QHBoxLayout()
    # Instantiate the menu bar
    bar=()
    # Create a main menu file, add submenus to it
    file=('File')
    ('New')
    ('Save')
    ('quit')

    # Create QDockWidget window (title, own window)
    =QDockWidget('Dockable',self)

    # Instantiate the list window, add a few entries
    =QListWidget()
    ('Item1')
    ('Item2')
    ('Item3')
    ('Item4')

    # Set up a QWidget in the window area to add a list control
    ()

    #Set whether the dock window can be floated, True, run to float outside, automatically detached from the main interface, False, default float inside the main window, you can manually detach it.
    (False)

    # Set QTextEdit as the central mini-control
    (QTextEdit())
    # Place the window to the right of the central mini-control
    (,)

    (layout)
    ('Dock Example')
if __name__ == '__main__':
  app=QApplication()
  demo=DockDemo()
  ()
  (app.exec_())

Not overriding the QDockWidget class

import sys
from  import *
from  import QMainWindow, QTextEdit, QDockWidget, QPushButton, QApplication


class DockDemo(QMainWindow):
  def __init__(self):
    super().__init__()
    self.docker1=QDockWidget('Docker1', self)
    self.docker2=QDockWidget('Docker2', self)
    =QTextEdit()
    ()
    self.btn1=QPushButton('btn1')
    self.btn2=QPushButton('btn2')
    self.(self.btn1fun)
    self.(self.btn2fun)
    self.(self.btn1)
    self.(self.btn2)
    (,self.docker1)
    (, self.docker2)
    ('Dock Example')
    (400, 300)
  def btn1fun(self):
    ('btn1')
    self.(True)# Turn on hovering
  def btn2fun(self):
    ('btn2')
    self.(True)
    
if __name__ == '__main__':
  app=QApplication()
  demo=DockDemo()
  ()
  (app.exec_())

Effect.

Rewrite the QDockWidget class

import sys
from  import *
from  import QMainWindow, QTextEdit, QDockWidget, QPushButton, QApplication, QWidget, QVBoxLayout


class docker(QDockWidget):
  def __init__(self, parent):
    super().__init__(parent)
    self.btn1 = QPushButton('btn1')
    self.btn2 = QPushButton('btn2')
     = QVBoxLayout()
    (self.btn1)
    (self.btn2)
     = QWidget()
    ()
    ()

class DockDemo(QMainWindow):
  def __init__(self):
    super().__init__()
     = docker(self)
     = QTextEdit()
    ()
    .(self.btn1fun)
    .(self.btn2fun)
    (, )
    ('Dock Example')
    (400, 300)

  def btn1fun(self):
    ('btn1')

  def btn2fun(self):
    ('btn2')


if __name__ == '__main__':
  app = QApplication()
  demo = DockDemo()
  ()
  (app.exec_())

Effect.

This article on the application of PyQt5 QDockWidget control is introduced to this article, more related to PyQt5 QDockWidget content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future !