YunchaoYang / Blogs

blogs and notes, https://yunchaoyang.github.io/blogs/
0 stars 0 forks source link

PyQt5 #40

Open YunchaoYang opened 11 months ago

YunchaoYang commented 11 months ago

PyQt5

1. PyQt Concepts and Programs

import sys
from PyQt5.QtWidgets import QApplication, QWidget
if __name__ == "__main__":
    app = QApplication(sys.argv) # every UI app must create an instance of QApplication, as a sort of entry point into the app
    window = QWidget() # QWidget is the base class of all UI objects in Qt, and virtually everything you see in an app is a widget. That includes dialogs, texts, buttons, bars, and so on.
# The feature that allows you to design complex user interfaces is that the widgets can be nested,
# i.e., you can have a widget inside a widget, which is inside yet another widget. 
    window.resize(300,300)
    window.setWindowTitle("Guru99") # the outermost widget (i.e., the widget with no parent) is called a Window
    window.show() # show() simply displays the widget on the monitor screen.
    sys.exit(app.exec_()) #  app.exec_() method starts the Qt/C++ event loop. 

2. Beyond empty windows

Next, we will look at how to make a widget inside another widget to make a bit complex interface design.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QMessageBox

def dialog():
    mbox = QMessageBox()

    mbox.setText("Your allegiance has been noted")
    mbox.setDetailedText("You are now a disciple and subject of the all-knowing Guru")
    mbox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)

    mbox.exec_()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = QWidget()
    w.resize(300,300)
    w.setWindowTitle("Guru99")

    label = QLabel(w)
    label.setText("Behold the Guru, Guru99")
    label.move(100,130)
    label.show()

    btn = QPushButton(w)
    btn.setText('Beheld')
    btn.move(110,150)
    btn.show()
    btn.clicked.connect(dialog)

    w.show()
    sys.exit(app.exec_())
  1. Once you click on the button in the first window, a new message box will pop open with the text
  2. You can click hide details or show details to toggle the visibility of additional text.
  3. label = QLabel(w) btn = QPushButton(w) QLabel is used to print non-editable text or placeholders inside a widget, whereas QPushButton is used to create a clickable button. The critical thing to notice here is that when you create the label and btn objects, you are passing the window object (w) to the constructors of QLabel and QPushButton. This is how nesting works in PyQt5. To create a widget inside another widget, you pass the reference of the parent widget to the child’s constructor.
  4. move() is used to set the position of a widget with respect to its parent widget. In the first case, the label will be moved 100px from the left and 130px from the top of the window. 5.. The dialog method is called from the main block of the program when a button is pressed in a specific widget (in this case, the btn.PushButton). The click event triggered on that button causes this function to execute. Such a function is called a slot in Qt, and you will learn more about signals and slots in the upcoming paragraphs.
  5. btn.clicked.connect(dialog) Finally, this is an example of signals and slots in Qt. In GUI based applications, functions are executed based on the actions performed by the user, like hovering over an element or clicking a button. These actions are called events. Recall that the app.exec_() method transfers control to the Qt event-loop.
  6. Whenever an event occurs, like a user clicking a button, the corresponding Qt widget raises a signal. These signals can be connected to [python functions] (like the dialog function in this example) so that the function is executed when a signal is triggered. These functions are called slots in Qt lingo.
  7. Subsequently, the basic syntax to trigger a slot function in response to the signal from an event is as follows widget.signal.connect(slot)

Components and Widgets

GUI apps has plenty of widgets in a PyQt app. To grasp a clear picture of a PyQt5 code, it is goo dto have a organized architectural view of the widget, modules, libraries, APIs. image The most frequently used widgets:

  1. QLinEdit
  2. QRadioButton
  3. QComboBox: dropdown menu
  4. QCheckbox: selectable square box
  5. QMenuBar:
  6. QToolBar:
  7. QTab
  8. QScrollBar
  9. QSplitter
  10. QDock

Layouts and Themes

Layouts

grid = QGridLayout(w)
    for i in range(3):
        for j in range(3):
            grid.addWidget(QPushButton("Button"),i,j)
 grid = QGridLayout(w)
grid.addWidget(QPushButton("Button five"),2,0,1,0)

Themes

references:

pyQt5 templates

YunchaoYang commented 11 months ago

Qt Designer

Go to https://www.qt.io/download and download the Qt package. You can opt to install only Creator during the installation.

Open up Qt Creator and you will be presented with the main window. The designer is available via the tab on the left hand side. However, to activate this you first need to start creating a .ui file.

A tutorial of Qt Designer

YunchaoYang commented 11 months ago

How to view stl file or any geometry file in a window?

  1. PyQtGraph: Scientific Graphics and GUI Library for Python:

    • image analysis
    • 3D graphics
  2. VisPy[https://vispy.org/]

YunchaoYang commented 8 months ago

The Event System

PyQt5 Signals, Slots & Events

Mouse Events

Event handler | Event type moved -- | -- mouseMoveEvent | Mouse moved mousePressEvent | Mouse button pressed mouseReleaseEvent | Mouse button released mouseDoubleClickEvent | Double click detected Method | Returns -- | -- .button() | Specific button that triggered this event .buttons() | State of all mouse buttons (OR'ed flags) .globalPos() | Application-global position as a QPoint .globalX() | Application-global horizontal X position .globalY() | Application-global vertical Y position .pos() | Widget-relative position as a QPoint integer .posF() | Widget-relative position as a QPointF float
Method | Returns -- | -- .button() | Specific button that triggered this event .buttons() | State of all mouse buttons (OR'ed flags) .globalPos() | Application-global position as a QPoint .globalX() | Application-global horizontal X position .globalY() | Application-global vertical Y position .pos() | Widget-relative position as a QPoint integer .posF() | Widget-relative position as a QPointF float

Context menus

Context menus are small context-sensitive menus which typically appear when right clicking on a window.

refs