pyside6基础学习(一)

发布时间 2023-11-10 22:45:40作者: d3solate

pyside6基础学习(一)

参考连接
https://doc.qt.io/qtforpython-6/search.html
https://www.pythonguis.com/tutorials/pyside6-creating-your-first-window/
个人学习记录,参考很多前辈的文章。如果有错误,请指出,我将不胜感激。

运行一个Aplication

from PySide6.QtWidgets import QApplication, QWidget  
  
# Only needed for access to command line arguments  
import sys  
  
# You need one (and only one) QApplication instance per application.  
# Pass in sys.argv to allow command line arguments for your app.  
# If you know you won't use command line arguments QApplication([]) works too.  
app = QApplication(sys.argv)  
  
# Create a Qt widget, which will be our window.  
window = QWidget()  
window.show()  # IMPORTANT!!!!! Windows are hidden by default.  
  
# Start the event loop.  
app.exec()  
  
# Your application won't reach here until you exit and the event  
# loop has stopped.
  1. QApplication可以接收参数,不需要添加的时候QApplication([])也可以
  2. 没有parent的Widgets(控件)默认是不可见的,需要.show()

    Widgets without a parent are invisible by default. So, after creating the window object, we must always call .show() to make it visible. You can remove the .show() and run the app, but you'll have no way to quit it!

  3. app.exec()调用之后会开始事件循环

事件循环

  • 每个 Qt 应用程序的核心都是 QApplication 类。
  • 每个应用程序都需要一个也只有一个QApplication 对象来运行。
  • 该对象包含应用程序的事件循环--管理所有用户与图形用户界面交互的核心循环。

QMainWindow控件

在 Qt 中,任何控件都可以是窗口。
例如,如果用 QPushButton 代替 QtWidget。在之前的示例中,你将得到一个窗口,其中有一个可按下的按钮。

from PySide6.QtWidgets import QApplication, QWidget, QPushButton  
import sys  
 
app = QApplication(sys.argv)  
window = QPushButton()  
window.show()
app.exec()  

使用布局在其他部件中嵌套部件的功能意味着你可以在一个空的 QWidget 中构建复杂的用户界面。
不过,Qt 已经为你准备好了解决方案--QMainWindow。这是一个预制窗口部件,它提供了许多您在应用程序中会用到的标准窗口功能,包括工具栏菜单状态栏可停靠窗口部件等。我们稍后将了解这些高级功能,但现在,我们将在应用程序中添加一个简单的空 QMainWindow。只需要将上方代码中的QPushButton修改为QMainWindow即可,在import中添加上对应的控件
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QMainWindow
但是一般需要自定义窗口,所以最好是用一个子类来实现。
例如:

import sys  
from PySide6.QtCore import QSize, Qt  
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton  
  
  
class MainWindow(QMainWindow):  
    def __init__(self):  
        super().__init__()  
        self.setWindowTitle("My App")  
        button = QPushButton("Press Me!")  
        self.setCentralWidget(button)  
  
  
if __name__ == '__main__':  
    app = QApplication(sys.argv)  
    window = MainWindow()  
    window.show()  
    app.exec_()

  1. 创建一个类,继承于QMainWindow,然后在内部调用父类的初始化方法。

    子类化 Qt 类时,必须始终调用父类的 __init__ 函数,以便 Qt 设置对象。super().__init__()

  2. Qt 核心wdigets总是从 QtWidgets 名称空间导入(from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton),QMainWindow 和 QApplication 类也是如此。
  3. 在使用 QMainWindow 时,我们使用 .setCentralWidget 将窗口部件(此处为 QPushButton)放置在 QMainWindow 中,默认情况下它占据整个窗口(后续可以通过布局来自己设定控件的大小位置)。
    这些控件有哪些方法、属性、信号都可以去官方文档查询。
    在QMainWindow中,官方文档:https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QMainWindow.html#qmainwindow

基本框架

  1. 首先创建一个Form类,继承于QDialog(可以换想要的控件)
  2. 定义__init__方法,调用父类的init方法 super().__init__()
  3. 设定窗口类的一些信息
  4. 主函数中初始化QApplication()
  5. 使用From类生成一个from对象
  6. form.show 展示该窗口类
  7. app.exec() 启动
import sys
from PySide6.QtWidgets import QApplication, QDialog, QLineEdit, QPushButton

class Form(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("My Form")


if __name__ == '__main__':
    # Create the Qt Application
    app = QApplication(sys.argv)
    # Create and show the form
    form = Form()
    form.show()
    # Run the main Qt loop
    sys.exit(app.exec())