1

now i make Some application using PYQT5.

i want minimize my app , when button clicked that you can see under the picture.

how to i make minimize button or how to action minimize.

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
One Bottle
  • 37
  • 3
  • 1
    Would it be possible to include a [mcve]? – user2314737 Apr 13 '20 at 08:20
  • Please provide a minimum example which reproduces your issue: https://stackoverflow.com/help/minimal-reproducible-example https://stackoverflow.com/help/how-to-ask – mahesh Apr 13 '20 at 08:22
  • How did you create that window shown in the picture? Is it a dialogue (because we see a window in the background)? – hc_dev Jan 01 '22 at 16:46

2 Answers2

1

For QDialog there are options under setWindowsFlag where you can set whether the minimize and maximize button to be visible or hidden. By default they are set to be hidden.

dialog.setWindowFlag(Qt.WindowMinimizeButtonHint, True)
dialog.setWindowFlag(Qt.WindowMaximizeButtonHint, True)

for minimizing and maximizing respectively

CyberNoob
  • 37
  • 7
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 01 '22 at 16:04
1

TL;DR: The OS or platform's windowing-system is responsible of drawing the window-icons/-buttons as well as primary event-handling for actions "minimize", "maximize", etc. Some QtWidgets like QDialog have predefined window-flags. The desired window-buttons can be set using widget.setWindowFlag(flags | Qt.WindowMinimizeButtonHint)

Drawing Windows (incl. Dialogues) in Qt

Note: The recommended way of drawing windows is using QtWidget or QQuickWindow.

See QWindow — PyQt v5.15 Reference Guide, description:

The QWindow class represents a window in the underlying windowing system.

A window that is supplied a parent becomes a native child window of their parent window.

An application will typically use QWidget or QQuickView for its UI, and not QWindow directly. Still,

(formatting and emphasize mine)

Search the docs in PyQt for "minimized". Like QWindow the QtWidget, which QDialog inherits from, has several event-handling methods supporting minimized state:

  • minimized property
  • showMinimized() function

But no explicit method or property to draw this window-button for the Window action "minimize".

Windowing-System responsibility

The appearance and rendering, as well as modifier-buttons shown in the window-edges depends on the Platform/OS and windowing-system (like e.g. Linux's X-Window system X11 and deployed window-manager).

Using Qt's WindowFlags

Window Flags Example | Qt Widgets 5.15.7:

A widget's flags are stored in a Qt::WindowFlags type which stores an OR combination of the flags.

In PyQt this is implemented using the class QtCore.Qt.WindowFlags which is a set of the QtCore.Qt.WindowType enum:

The CustomizeWindowHint flag is used to enable customization of the window controls. This flag must be set to allow the WindowTitleHint, WindowSystemMenuHint, WindowMinimizeButtonHint, WindowMaximizeButtonHint and WindowCloseButtonHint flags to be changed.

These methods of QWindow support WindowFlags:

  • flags() → WindowFlags
  • setFlags(Union[WindowFlags, WindowType])

And, these methods of QWidget support WindowFlags:

  • __init__(parent: QWidget = None, flags: Union[WindowFlags, WindowType] = Qt.WindowFlags())
  • windowFlags() → WindowFlags
  • setWindowFlags(Union[WindowFlags, WindowType])
  • overrideWindowFlags(Union[WindowFlags, WindowType])

Example:

from PyQt5.QtCore import Qt # WindowFlags, WindowType

window = QQuickWindow(flags=Qt.WindowFlags(Qt.WindowMinimizeButtonHint))
print('current window-flags, represented as int:', window.flags(), int(window.flags()) )
window.setFlags(Qt.WindowFlags(Qt.WindowMaximizeButtonHint))

Prints:

current window-flags, represented as int: <PyQt5.QtCore.WindowFlags object at 0x7fc471d574a8> 16384

Showing the int representation of WindowFalgs instance.

Draws: QQuickWindow with unset WindowMinimizeButtonHint

QDialog, default window-flags and how to modify

Dialogs are special type of window. They often are not resizable, can not be minimized or maximized. Sometimes they are bound to the parent-window (see also Modal dialogues).

QDialog for example has some default flags.

The widget flags f are passed on to the QWidget constructor. If, for example, you don't want a What's This button in the title bar of the dialog, pass Qt::WindowTitleHint | Qt::WindowSystemMenuHint in f.

How to query and modify window-flags on QWidgets like QDialog

Example for PyQt's QDialog where MinimizeWindowButtonHint is set:

import sys
from PyQt5.QtWidgets import QApplication 
from PyQt5.QtWidgets import QDialog
from PyQt5.QtCore import Qt # WindowFlags, WindowType

app = QApplication(sys.argv)

widget = QDialog()
widget.setWindowTitle('QDialog demo')
print('current dialog-flags, represented as int and hex:', widget.windowFlags(), int(widget.windowFlags()), hex(int(widget.windowFlags())) )

widget.setWindowFlags(widget.windowFlags() | Qt.WindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint)
print('after added min&max then dialog-flags, represented as int and hex:', widget.windowFlags(), int(widget.windowFlags()), hex(int(widget.windowFlags())) )

widget.resize(400,100)
widget.show()

sys.exit(app.exec_())

Renders (on Linux/Ubuntu using XFCE windowing-system): QDialgou with explicitly set WindowFlags()

Prints:

current dialog-flags, represented as int and hex: <PyQt5.QtCore.WindowFlags object at 0x7fc6509d10b8> 134295555 0x8013003

after added min&max then dialog-flags, represented as int and hex: <PyQt5.QtCore.WindowFlags object at 0x7fc6509d1198> 134344707 0x801f003

Working with WindowFlags and their combinations

However, the first are the default window-flags for QDialogue because the class is inherited from QWidget and QWidget.windowFlags are predefined too:

Window flags are a combination of a type (e.g. Qt::Dialog) and zero or more hints to the window system (e.g. Qt::FramelessWindowHint).

If we convert the decimal value of QDialog's default flags 134295554 from decimal to hex we get: 8013002 as hex, in programming languages also written as 0x8013002.

Compare this to Qt's WindowType

enum constant hex value default of QDialog
Qt::Window 0x00000001 No
Qt::Dialog 0x00000002 | Window Yes
Qt::WindowTitleHint 0x00001000 Yes
Qt::WindowSystemMenuHint 0x00002000 Yes
Qt::WindowMinimizeButtonHint 0x00004000 No
Qt::WindowMaximizeButtonHint 0x00008000 No
Qt::WindowMinMaxButtonsHint WindowMinimizeButtonHint | WindowMaximizeButtonHint No
Qt::WindowContextHelpButtonHint 0x00010000 Yes
Qt::WindowCloseButtonHint 0x08000000 Yes

When adding all above marked in bold (with last column "Yes") added into one set of flags using boolean OR |, we get: 0x8013002 as default-flags for QDialog.

After printing the initial flags, we added both WindowMinimizeButtonHint and WindowMaximizeButtonHint using boolean OR: widget.windowFlags() | Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint

So the dialog now has existing QDialog's default-flags (window-buttons) plus additional minimize & maximize buttons.

See also

hc_dev
  • 8,389
  • 1
  • 26
  • 38