Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the basic window control QWidget

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to understand the basic window control QWidget, the content is very detailed, interested friends can refer to, hope to be helpful to you.

   originally wanted to write about how to use Qt Designer to design an interface, but now I usually write the interface directly in code and rarely use a designer. Because Qt Designer is not written for python, it is not very convenient to use. Many beginners may prefer to use Qt Designer, because WYSIWYG, I used to do MFC development has been using interface designer, mainly VS is very powerful, and interface design and coding are using this IDE, the match is perfect. But Qt Designer is the same. Another point is that you are not familiar with the use of the PyQt5 layout manager, if you learn to use the layout manager, then the layout of these controls is actually very simple. It is not what you think to use code to adjust the window by pixel, PyQt5 layout manager is based on the left and right layout, up and down layout or grid layout to automatically adjust you to the corresponding position, you just need to make some fine-tuning. A lot of the code we found on the Internet is to set the absolute location of the control directly, which misleads a lot of people. PyQt5 layout manager is very easy to use, it can help us to design the general layout is very simple. So my later tutorials will try to use the layout manager. At the same time, some comments will be added where it is used, so that you will have some understanding of the layout manager after you have seen more of the code.

QWidget

   a program can have multiple windows, and each window will host multiple controls. All windows and controls inherit directly or indirectly from the QWidget class.

Window coordinate system

  , like most GUI systems, the coordinate system used by PyQt5 is based on the upper-left corner (0,0).

Import sys

From PyQt5.QtWidgets import QApplication, QWidget

App = QApplication (sys.argv)

Widget = QWidget ()

Widget.setGeometry (300,300,500,500)

Widget.setWindowTitle ("I am the main window control")

W1 = QWidget (widget)

W1.setGeometry (100,100,200,200)

W1.setWindowTitle ("I am a child control")

W1.setStyleSheet ("background-color:blue")

Widget.show ()

Sys.exit (app.exec_ ())

For a separate top-level window, its X and Y coordinates are for the entire screen, which means that the 300300 set by the widget control is offset on the screen. For child controls, its X and Y coordinates are for its parent control, and 100100 for the W1 control is for the widget control, which is the 400400 position on the entire screen. The X coordinate increases from top to next, the larger the value, the closer to the bottom of the screen, the Y coordinate increases from left to right, and the larger the value, the closer to the right of the screen. There are three ways to get the location of a control in PyQt5:

The member functions provided directly by QWidget: X (), y () get the coordinates of the upper left corner of the window, and width (), height () get the width and height of the window.

The member functions provided by geometry () of QWidget: X (), y () get the coordinates of the upper left corner of the window, and width (), height () get the width and height of the window.

The member functions provided by frameGeometry () of QWidget: X (), y () get the coordinates of the upper left corner of the window, and width (), height () get the width and height of the window.

Commonly used geometric structures

QWidget has two geometric structures.

Does not contain the outer border.

Contains the outer border.

For the main window control, the part that does not contain a border is the client area, in which we can add child controls.

Does not contain outer border

The size of the client area is a QRect class. To resize the window, you can use the following function:

Modify window size

QWidget.resize (width, height)

Qwidget.resize (QSize)

two。 Get window size

QWidget.size ()

3. Get the width and height of the window

QWidget.width ()

QWidget.height ()

4. Set the height and width of the window

QWidget.setFixedWidth (width)

QWidget.setFixedHeight (height)

QWidget.setFixedSize (QSize)

QWidget.setFixedSize (width, height)

Set the fixed width and height, after setting, the size of the window cannot be changed.

QWidget.setGeometry (x, y, width, height)

QWidget.setGeometry (QRect)

Set the location and size of the window simultaneously

Include outer border

The QWidget containing border is the entire area where the window is displayed on the entire screen.

Get the location and size of the window

QWidget.frameGeometry ()

two。 Set the location of the window

QWidget.move (x, y)

QWidget.move (QPoint)

3. Get the coordinates of the upper left corner of the window

QWidget.pos ()

Here is another example to end today's article:

Import sys

From PyQt5.QtWidgets import QApplication, QWidget, QPushButton

If _ name__ = = "_ _ main__":

App = QApplication (sys.argv)

Main_widget = QWidget ()

Main_widget.setWindowTitle ("I am the main window control")

# equivalent to main_widget.setGeometry (300,300,500,500)

Main_widget.resize (500,500)

Main_widget.move (300,300)

# QPushButton (main_widget) means to use btn as a child control of main_widget, so that the parent control will be used as a reference when you move the position

Btn = QPushButton (main_widget)

Btn.setText ("I am the button")

Btn.setFixedSize (100,30)

Btn.move (100,100)

# print the location information

Print ("main_widget")

Print ("x% d, y% d"% (main_widget.x (), main_widget.y ()

Print ("width=%d, height=%d"% (main_widget.width (), main_widget.height ()

Print ("geometry")

Print ("x% d, y% d"% (main_widget.geometry () .x (), main_widget.geometry () .y ()

Print ("width=%d, height=%d"% (main_widget.geometry () .width (), main_widget.geometry () .height ()

Print ("frameGeometry")

Print ("x% d, y% d"% (main_widget.frameGeometry () .x (), main_widget.frameGeometry () .y ()

Print ("width=%d, height=%d"% (main_widget.frameGeometry () .width (), main_widget.frameGeometry () .height ()

Main_widget.show ()

Sys.exit (app.exec_ ())

On how to understand the basic window control QWidget to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report