我有一个分组框,其中有按垂直格式排列的按钮。参见下图。红色背景表示我堆叠的窗口小部件的哪个索引处于活动状态,只需忽略它即可。
按钮排列
当我使用.hide()方法时,按钮隐藏得很好。
但是,当我在按钮上使用.show()方法时,它不会返回。我假设它被放置在另一个按钮下,而我无法访问该按钮。参见下图。
破损的按钮排列
我尝试在组框上使用.adjustSize()、. repaint()和.update()方法,但是它不能解决问题。
我应该在组框中提到,我确实有一个垂直间隔,其大小类型为“ Expanding”,正在向上推动按钮。
问题: 使用.show()方法时,如何使按钮显示并再次散布为第一张图像?
每个请求,添加示例代码。请记住,这是我所能做到的最低要求。
appui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_app(object):
def setupUi(self, app):
app.setObjectName("App")
app.setWindowModality(QtCore.Qt.NonModal)
app.resize(400, 852)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(app.sizePolicy().hasHeightForWidth())
app.setSizePolicy(sizePolicy)
app.setMinimumSize(QtCore.QSize(400, 0))
app.setMaximumSize(QtCore.QSize(400, 16777215))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
app.setFont(font)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("theme/app.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
app.setWindowIcon(icon)
app.setStyleSheet("")
self.groupbox_buttongroup = QtWidgets.QGroupBox(self.tabWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.groupbox_buttongroup.sizePolicy().hasHeightForWidth())
self.groupbox_buttongroup.setSizePolicy(sizePolicy)
self.groupbox_buttongroup.setMinimumSize(QtCore.QSize(50, 0))
self.groupbox_buttongroup.setStyleSheet("")
self.groupbox_buttongroup.setTitle("")
self.groupbox_buttongroup.setFlat(True)
self.groupbox_buttongroup.setObjectName("groupbox_buttongroup")
self.verticalLayout = QtWidgets.QVBoxLayout(self.groupbox_buttongroup)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName("verticalLayout")
self.btn_notefield = QtWidgets.QPushButton(self.groupbox_buttongroup)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.btn_notefield.sizePolicy().hasHeightForWidth())
self.btn_notefield.setSizePolicy(sizePolicy)
self.btn_notefield.setMinimumSize(QtCore.QSize(0, 0))
self.btn_notefield.setMaximumSize(QtCore.QSize(16777215, 16777215))
self.btn_notefield.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("theme/g_notes.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.btn_notefield.setIcon(icon2)
self.btn_notefield.setIconSize(QtCore.QSize(40, 30))
self.btn_notefield.setObjectName("btn_notefield")
self.verticalLayout.addWidget(self.btn_notefield)
self.btn_nuc = QtWidgets.QPushButton(self.groupbox_buttongroup)
self.btn_nuc.setText("")
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("theme/g_nuc.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.btn_nuc.setIcon(icon3)
self.btn_nuc.setIconSize(QtCore.QSize(40, 30))
self.btn_nuc.setObjectName("btn_nuc")
self.verticalLayout.addWidget(self.btn_nuc)
self.btn_com = QtWidgets.QPushButton(self.groupbox_buttongroup)
self.btn_com.setText("")
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("theme/g_com.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.btn_com.setIcon(icon4)
self.btn_com.setIconSize(QtCore.QSize(40, 30))
self.btn_com.setObjectName("btn_com")
self.verticalLayout.addWidget(self.btn_com)
self.btn_comcontacts = QtWidgets.QPushButton(self.groupbox_buttongroup)
self.btn_comcontacts.setText("")
icon5 = QtGui.QIcon()
icon5.addPixmap(QtGui.QPixmap("theme/g_contacts.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.btn_comcontacts.setIcon(icon5)
self.btn_comcontacts.setIconSize(QtCore.QSize(40, 30))
self.btn_comcontacts.setObjectName("btn_comcontacts")
self.verticalLayout.addWidget(self.btn_comcontacts)
self.btn_nucsearch = QtWidgets.QPushButton(self.groupbox_buttongroup)
self.btn_nucsearch.setText("")
icon6 = QtGui.QIcon()
icon6.addPixmap(QtGui.QPixmap("theme/g_search.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.btn_nucsearch.setIcon(icon6)
self.btn_nucsearch.setIconSize(QtCore.QSize(40, 30))
self.btn_nucsearch.setObjectName("btn_nucsearch")
self.verticalLayout.addWidget(self.btn_nucsearch)
self.btn_about = QtWidgets.QPushButton(self.groupbox_buttongroup)
self.btn_about.setText("")
icon7 = QtGui.QIcon()
icon7.addPixmap(QtGui.QPixmap("theme/g_about.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.btn_about.setIcon(icon7)
self.btn_about.setIconSize(QtCore.QSize(40, 30))
self.btn_about.setObjectName("btn_about")
self.verticalLayout.addWidget(self.btn_about)
spacerItem38 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem38)
appmain.py
from PyQt5 import QtCore, QtGui, QtWidgets
from appui import Ui_app
from settingsui import Ui_Settings
class appMain(QtWidgets.QMainWindow, Ui_app):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.btn_notefield.released.connect(lambda: self.stackedWidget.setCurrentIndex(0))
self.btn_notefield.released.connect(self.updateButton)
self.btn_com.released.connect(lambda: self.stackedWidget.setCurrentIndex(2))
self.btn_com.released.connect(self.updateButton)
self.btn_comcontacts.released.connect(lambda: self.stackedWidget.setCurrentIndex(3))
self.btn_comcontacts.released.connect(self.updateButton)
self.btn_nuc.released.connect(lambda: self.stackedWidget.setCurrentIndex(1))
self.btn_nuc.released.connect(self.updateButton)
self.btn_nucsearch.released.connect(lambda: self.stackedWidget.setCurrentIndex(4))
self.btn_nucsearch.released.connect(self.updateButton)
self.btn_about.released.connect(lambda: self.stackedWidget.setCurrentIndex(5))
self.btn_about.released.connect(self.updateButton)
def settings(self):
app = QtWidgets.QDialog()
self.settingui = settingsui()
self.settingui.btn_profileapply.clicked.connect(self.applyprofile)
def applyprofile(self):
if self.settingui.radiobtn_custom.isChecked():
user = os.getenv('username')
custom_opts = []
tabopts = [
self.settingui.checkbox_nuctab,
self.settingui.checkbox_comtab,
self.settingui.checkbox_nucomcontact,
self.settingui.checkbox_nucsearch,
]
for option in tabopts:
if option.isChecked():
selected_opt = option.text()
custom_opts.append(selected_opt)
with open(f'C:\\Users\\{user}\\AppData\\Local\\app\\settings', 'r') as rsettings:
sett = json.load(rsettings)
sett['default_profile']['profile'] = 'custom'
sett['custom_opts']['selected_opts'] = custom_opts
with open(f'C:\\Users\\{user}\\AppData\\Local\\app\\settings', 'w') as wsettings:
json.dump(sett, wsettings, indent=2)
for item in custom_opts:
if item == 'COM Tab':
self.btn_com.show()
if item != 'COM Tab':
self.btn_com.hide()
if item == 'nuc Tab':
self.btn_nuc.show()
if item != 'nuc Tab':
self.btn_nuc.hide()
if item == 'NUC/COM Contacts':
self.btn_comcontacts.show()
if item != 'NUC/COM Contacts':
self.btn_comcontacts.hide()
if item == 'NUC Search':
self.btn_nucsearch.show()
if item != 'NUC Search':
self.btn_nucsearch.hide()
class settingsui(QtWidgets.QDialog, Ui_Settings):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
self.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
win = appMain()
win.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
我发现了问题。显示btn的if语句被忽略,因为它是在json将数据转储回文件之后的。