from PySide6.QtWidgets import QMessageBox
# Question dialog
reply = QMessageBox.question(
self,
"Confirm",
"Are you sure?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No
)
if reply == QMessageBox.StandardButton.Yes:
print("User confirmed")
# Information
QMessageBox.information(self, "Info", "Operation completed successfully")
# Warning
QMessageBox.warning(self, "Warning", "This action cannot be undone")
# Critical error
QMessageBox.critical(self, "Error", "Failed to connect to server")
# About
QMessageBox.about(self, "About", "My App v1.0\n\nCopyright 2024")
# About Qt
QMessageBox.aboutQt(self)
# Custom buttons
msg = QMessageBox(self)
msg.setWindowTitle("Custom Dialog")
msg.setText("Continue?")
msg.setIcon(QMessageBox.Icon.Question)
msg.addButton("Yes", QMessageBox.ButtonRole.YesRole)
msg.addButton("No", QMessageBox.ButtonRole.NoRole)
msg.addButton("Cancel", QMessageBox.ButtonRole.RejectRole)
result = msg.exec()
print(f"Button role: {msg.buttonRole(msg.clickedButton())}")
QInputDialog
from PySide6.QtWidgets import QInputDialog, QLineEdit
# Get text
text, ok = QInputDialog.getText(
self,
"Input",
"Enter name:",
QLineEdit.EchoMode.Normal,
"Default value"
)
if ok and text:
print(f"Name: {text}")
# Get integer
value, ok = QInputDialog.getInt(
self,
"Input",
"Enter age:",
25, # Default0, # Min120, # Max1# Step
)
if ok:
print(f"Age: {value}")
# Get double
price, ok = QInputDialog.getDouble(
self,
"Input",
"Enter price:",
0.0,
0.0,
1000.0,
2# Decimals
)
if ok:
print(f"Price: ${price:.2f}")
# Get item from list
items = ["Option 1", "Option 2", "Option 3"]
item, ok = QInputDialog.getItem(
self,
"Select",
"Choose an option:",
items,
0, # Current indexFalse# Editable
)
if ok:
print(f"Selected: {item}")
# Get multiline text
text, ok = QInputDialog.getMultiLineText(
self,
"Input",
"Enter description:",
"Default\ntext"
)
QColorDialog
from PySide6.QtWidgets import QColorDialog
from PySide6.QtGui import QColor
# Get color
color = QColorDialog.getColor(
QColor(255, 0, 0), # Default colorself,
"Select Color"
)
if color.isValid():
print(f"Color: {color.name()}") # "#ff0000"
widget.setStyleSheet(f"background-color: {color.name()};")
# With alpha
color = QColorDialog.getColor(
QColor(255, 0, 0, 128),
self,
"Select Color with Alpha",
QColorDialog.ColorDialogOption.ShowAlphaChannel
)
# Get color with options
options = (
QColorDialog.ColorDialogOption.ShowAlphaChannel |
QColorDialog.ColorDialogOption.NoButtons
)
color = QColorDialog.getColor(Qt.white, self, "Color", options)
QFontDialog
from PySide6.QtWidgets import QFontDialog
from PySide6.QtGui import QFont
# Get font
font, ok = QFontDialog.getFont(
QFont("Arial", 12), # Default fontself,
"Select Font"
)
if ok:
print(f"Font: {font.family()}, Size: {font.pointSize()}")
widget.setFont(font)
# With options
font, ok = QFontDialog.getFont(
QFont(),
self,
"Select Font",
QFontDialog.FontDialogOption.MonospacedFonts
)