Table of Contents#
- Understanding QCommandLinkButton
- What is the Auto Default Property?
- Auto Default vs. Default Property
- Why and When to Use Auto Default
- Setting Auto Default: Practical Guide
- Complete Example Implementation
- Common Mistakes and Best Practices
- Conclusion
- References
Understanding QCommandLinkButton#
QCommandLinkButton (introduced in Qt 4.5) extends QPushButton with Windows Vista-style command links. It features:
- A large arrow icon
- A title (main text)
- Descriptive supplementary text
- Modern flat appearance
Commonly used in:
- Installer wizards
- Configuration dialogs
- Multi-step forms
Declaration syntax:
from PyQt5.QtWidgets import QCommandLinkButton
cmd_button = QCommandLinkButton("Title", "Description", parent)What is the Auto Default Property?#
The autoDefault property (bool) controls automatic default button assignment in dialogs. Its default value depends on the parent widget type: True when the parent is a dialog, False otherwise.
- When enabled (
True):
The button becomes the default button when its parent dialog gains focus. - When disabled (
False):
No automatic default assignment occurs.
Key behaviors:
- Auto-default buttons respond to the
Enterkey press - Only one button can be default at a time
- Visual cue: Default buttons have emphasized styling (OS-dependent)
Auto Default vs. Default Property#
| Property | Default Value | Behavior | Use Case |
|---|---|---|---|
autoDefault | True (dialog parent) / False (non-dialog parent) | Auto-assigns default status when parent gains focus | Typical for command link buttons |
default | False | Forces permanent default status (overrides autoDefault) | Critical actions (e.g., "Delete") |
setDefault() | Method | Explicitly sets a button as default (bypasses autoDefault mechanism) | Manual control scenarios |
Golden Rule:
Set autoDefault=False when manually assigning defaults via setDefault() to prevent conflicts.
Why and When to Use Auto Default#
⚠️ Disable Auto Default When:#
- Implementing custom keyboard navigation logic
- Building dialogs with multiple action buttons
- Creating non-traditional workflows (e.g., fullscreen apps)
- Designating a high-risk action (e.g., "Format Disk") as non-default
✅ Keep Enabled For:#
- Standard modal dialogs
- Primary action buttons ("Next", "Accept", "Save")
- Wizards with sequential steps
Setting Auto Default: Practical Guide#
Basic Usage#
# Create button with autoDefault enabled (default)
button_ok = QCommandLinkButton("OK", "Confirm action", self)
button_ok.setAutoDefault(True) # Explicitly enable (redundant but clear)
# Disable auto-default behavior
button_cancel = QCommandLinkButton("Cancel", "Abort operation", self)
button_cancel.setAutoDefault(False)Dynamic Toggle#
Change behavior at runtime:
def toggle_auto_default(state):
button.setAutoDefault(state) # state: bool (0/1 from checkbox/trigger)Complete Example Implementation#
import sys
from PyQt5.QtWidgets import (QApplication, QDialog, QVBoxLayout,
QCommandLinkButton)
class CommandLinkDemo(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("AutoDefault Demo")
layout = QVBoxLayout(self)
# Primary action (auto-default enabled)
self.btn_install = QCommandLinkButton(
"Install", "Start software installation", self
)
self.btn_install.clicked.connect(self.start_install)
layout.addWidget(self.btn_install)
# Secondary action (auto-default disabled)
self.btn_cancel = QCommandLinkButton(
"Cancel", "Exit setup wizard", self
)
self.btn_cancel.setAutoDefault(False) # Disable auto-default
self.btn_cancel.clicked.connect(self.reject)
layout.addWidget(self.btn_cancel)
# Set explicit default (overrides auto-default)
self.btn_install.setDefault(True)
def start_install(self):
print("Installation started!")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = CommandLinkDemo()
window.show()
sys.exit(app.exec_())Behavior:
Installbutton:- Auto-default enabled (but irrelevant due to explicit
setDefault) - Responds to
Enterkey
- Auto-default enabled (but irrelevant due to explicit
Cancelbutton:- Ignores
Enterkey (requires mouse/tab+Space activation)
- Ignores
Common Mistakes and Best Practices#
🚫 Common Errors#
-
Conflict with Explicit Defaults:
Setting bothsetDefault(True)andsetAutoDefault(True)makes the button always be in default state, but usually it's recommended to use only one method to avoid confusion. -
Overusing Auto Default:
Enabling for all buttons leads to last-focused button stealing default status. -
Ignoring Dialog Context:
Not disablingautoDefaultin complex dialogs risks accidental activation.
✅ Best Practices#
-
Hierarchy Principle:
DisableautoDefaultfor secondary/destructive actions:delete_btn.setAutoDefault(False) # Prevent accidental deletion via Enter -
Keyboard Navigation Test:
Always verify tab order with:self.setTabOrder(btn1, btn2) # Explicit tab sequence control -
Visual Feedback:
UseQStyleto highlight default buttons:if is_default: button.setStyleSheet("font-weight: bold;") # Supplemental styling -
Consistency Rules:
- Modal dialogs: Enable for primary action
- Modeless windows: Disable globally
- Destroyable actions: Always disable
autoDefault
Conclusion#
Mastering autoDefault is essential for creating intuitive PyQt5 interfaces. By strategically enabling this property for primary actions while disabling it for secondary/destructive buttons, you ensure keyboard interactions align with user expectations. Remember to:
- Disable
autoDefaultwhen usingsetDefault() - Test keyboard navigation thoroughly
- Prioritize safety for irreversible actions
Effective use of this property significantly enhances dialog usability while minimizing accidental activations.