gpt4 book ai didi

python - 禁用 QWizardPage 的下一个按钮上的默认焦点,并让自定义 QPushButton 由 'enter' 触发

转载 作者:行者123 更新时间:2023-12-01 00:42:30 26 4
gpt4 key购买 nike

我正在开发一个QWizardPage,用户可以在其中搜索要安装的软件包。因此,我提供了一个搜索按钮(QPushButton)和一个QLineEdit(作为搜索按钮的buddy())。

我的问题是,向导的下一个按钮始终具有焦点。但对于这个特定页面,我希望 QLineEdit 默认具有焦点,并且 returnenter 会触发搜索按钮。

我已经在 QLineEdit 上尝试了 setDefault(True)setFocus(),但这不起作用。我通读了文档,但找不到访问 PyQt5 中 QWizardPage 的下一个按钮的方法。我想也许有类似 autofocus(False) 的东西可以在特定的向导页面上禁用它。

如何将焦点集中在 QWizardPage 内的 QLineEdit 上,并让 returnenter 触发自定义QPushButton

<小时/>

重现代码:

# -*- coding: utf-8 -*-
from PyQt5.QtGui import QIcon, QStandardItemModel, QStandardItem
from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot, QObject, QTimer, QThread
from PyQt5.QtWidgets import (QApplication, QProgressBar, QGridLayout, QLabel,
QFileDialog, QHBoxLayout, QVBoxLayout, QDialog,
QWizard, QWizardPage, QToolButton, QComboBox,
QCheckBox, QLineEdit, QGroupBox, QTableView,
QAbstractItemView, QFrame, QPushButton)

# ...

#]===========================================================================[#
#] WIZARD [#=================================================================[#
#]===========================================================================[#

class VenvWizard(QWizard):
"""
Wizard for creating and setting up a virtual environment.
"""
def __init__(self):
super().__init__()

self.setWindowTitle("Venv Wizard")
self.resize(635, 480)
self.move(528, 153)

self.setStyleSheet(
"""
QToolTip {
background-color: rgb(47, 52, 63);
border: rgb(47, 52, 63);
color: rgb(210, 210, 210);
padding: 2px;
opacity: 325
}

QTableView {
gridline-color: rgb(230, 230, 230)
}

QTableView::item {
selection-background-color: rgb(120, 120, 130);
selection-color: rgb(255, 255, 255)
}
"""
)

self.addPage(InstallPackages())
self.addPage(Summary())

# ...


class InstallPackages(QWizardPage):
"""
Install packages via `pip` into the created virtual environment.
"""
def __init__(self):
super().__init__()

self.setTitle("Install Packages")
self.setSubTitle(
"Specify the packages you want to install into the "
"virtual environment. Right-click on the item to "
"mark it for installation and click next when ready."
)

#]===================================================================[#
#] PAGE CONTENT [#===================================================[#
#]===================================================================[#

verticalLayout = QVBoxLayout()
gridLayout = QGridLayout(self)

pkgNameLabel = QLabel("Package name:")
self.pkgNameLineEdit = QLineEdit()
pkgNameLabel.setBuddy(self.pkgNameLineEdit)

self.searchButton = QPushButton(
"&Search",
#clicked=self.list_packages
)

resultsTable = QTableView(
selectionBehavior=QAbstractItemView.SelectRows,
editTriggers=QAbstractItemView.NoEditTriggers,
alternatingRowColors=True
)

# adjust vertical headers
v_Header = resultsTable.verticalHeader()
v_Header.setDefaultSectionSize(27.5)
v_Header.hide()

# adjust (horizontal) headers
h_Header = resultsTable.horizontalHeader()
h_Header.setDefaultAlignment(Qt.AlignLeft)
h_Header.setDefaultSectionSize(150)
h_Header.setStretchLastSection(True)

# set table view model
self.resultsModel = QStandardItemModel(0, 2, self)
self.resultsModel.setHorizontalHeaderLabels(
["Name", "Version", "Description"]
)
resultsTable.setModel(self.resultsModel)

gridLayout.addWidget(pkgNameLabel, 0, 0, 1, 1)
gridLayout.addWidget(self.pkgNameLineEdit, 0, 1, 1, 1)
gridLayout.addWidget(self.searchButton, 0, 2, 1, 1)
gridLayout.addWidget(resultsTable, 1, 0, 1, 3)

verticalLayout.addLayout(gridLayout)

# ...


class Summary(QWizardPage):
def __init__(self):
super().__init__()

self.setTitle("Summary")
self.setSubTitle("..........................."
"...........................")

#]===================================================================[#
# TODO: create the summary page
#]===================================================================[#

def initializePage(self):
pass



if __name__ == "__main__":
import sys

app = QApplication(sys.argv)

wiz = VenvWizard()
wiz.show()

sys.exit(app.exec_())

最佳答案

您必须在显示后立即更改按钮的默认属性,在这种情况下您可以使用 initializePage() QWizardPage的方法:

# ...
class InstallPackages(QWizardPage):
# ...
def initializePage(self):
next_button = self.wizard().button(QWizard.NextButton)
QTimer.singleShot(0, lambda: next_button.setDefault(False))
QTimer.singleShot(0, lambda: self.searchButton.setDefault(True))
# ...

关于python - 禁用 QWizardPage 的下一个按钮上的默认焦点,并让自定义 QPushButton 由 'enter' 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57254088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com