有没有办法将自定义 QGroupBox 添加到任何小部件/布局?
Posted
技术标签:
【中文标题】有没有办法将自定义 QGroupBox 添加到任何小部件/布局?【英文标题】:Is there a way to add a customized QGroupBox to any widgets/layouts? 【发布时间】:2020-08-30 09:55:02 【问题描述】:我已经通过 Qt Designer 创建了一个 QMainWindow 和一个自定义的 QGroupBox 并编译为 .py 文件。
这是我的小部件层次结构:QMainWindow -> QStackedWidget -> QScrollArea -> QVBoxLayout -> QGroupBox(es).
但是,当我尝试将上面创建的自定义 QGroupBox(Ui_deck
类)添加到 QVBoxLayout(属于Ui_main
类)时,它什么也没显示。
这是我的代码:
#main.py
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from main_ui import Ui_main
from deck_name import Ui_deck
class Main(QtWidgets.QMainWindow, Ui_main):
def __init__(self):
super().__init__()
self.setupUi(self)
groupbox = QtWidgets.QGroupBox(self.scroll_widget)
deck = Ui_deck()
deck.setupUi(groupbox)
self.scroll_layout.addWidget(groupbox)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main = QtWidgets.QMainWindow()
ui = Main()
ui.setupUi(main)
main.show()
sys.exit(app.exec_())
#main_ui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'deck_name.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_main(object):
def setupUi(self, main):
main.setObjectName("main")
main.resize(621, 522)
main.setMinimumSize(QtCore.QSize(0, 100))
main.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(main)
self.centralwidget.setStyleSheet("")
self.centralwidget.setObjectName("centralwidget")
self.stacked = QtWidgets.QStackedWidget(self.centralwidget)
self.stacked.setGeometry(QtCore.QRect(0, 0, 621, 501))
self.stacked.setObjectName("stacked")
self.home = QtWidgets.QWidget()
self.home.setObjectName("home")
self.scroll = QtWidgets.QScrollArea(self.home)
self.scroll.setGeometry(QtCore.QRect(0, 0, 621, 511))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scroll.sizePolicy().hasHeightForWidth())
self.scroll.setSizePolicy(sizePolicy)
self.scroll.setStyleSheet("QScrollBar:vertical\n"
" width: 5px;\n"
" border: none;\n"
"\n"
"\n"
"QScrollBar::handle:vertical\n"
" border:0;\n"
" background-color: rgb(176, 176, 176);\n"
" border-radius: 2px;\n"
"\n"
"\n"
"QScrollBar:up-arrow:vertical, QScrollBar:down-arrow:vertical\n"
" border: none;\n"
" background-color: none;\n"
" color: none;\n"
"\n"
"\n"
"QScrollBar::add-line:vertical \n"
" border: none;\n"
" background: none;\n"
"\n"
"\n"
"QScrollBar::sub-line:vertical \n"
" border: none;\n"
" background: none;\n"
"")
self.scroll.setFrameShadow(QtWidgets.QFrame.Plain)
self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scroll.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
self.scroll.setWidgetResizable(True)
self.scroll.setObjectName("scroll")
self.scroll_widget = QtWidgets.QWidget()
self.scroll_widget.setGeometry(QtCore.QRect(0, 0, 619, 509))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scroll_widget.sizePolicy().hasHeightForWidth())
self.scroll_widget.setSizePolicy(sizePolicy)
self.scroll_widget.setStyleSheet("QPushButton\n"
" height: 100px;\n"
"\n"
"QWidget#scroll_widget\n"
" background-color: white;\n"
"\n"
"")
self.scroll_widget.setObjectName("scroll_widget")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.scroll_widget)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.scroll_layout = QtWidgets.QVBoxLayout()
self.scroll_layout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize)
self.scroll_layout.setObjectName("scroll_layout")
self.buttons = QtWidgets.QGroupBox(self.scroll_widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.buttons.sizePolicy().hasHeightForWidth())
self.buttons.setSizePolicy(sizePolicy)
self.buttons.setMinimumSize(QtCore.QSize(0, 100))
font = QtGui.QFont()
font.setFamily("Arial")
self.buttons.setFont(font)
self.buttons.setStyleSheet("QGroupBox#buttons\n"
" border: 0;\n"
"")
self.buttons.setTitle("")
self.buttons.setObjectName("buttons")
self.label = QtWidgets.QLabel(self.buttons)
self.label.setGeometry(QtCore.QRect(8, 0, 591, 50))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setMinimumSize(QtCore.QSize(0, 50))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.label.setObjectName("label")
self.add_deck = QtWidgets.QPushButton(self.buttons)
self.add_deck.setGeometry(QtCore.QRect(10, 50, 129, 40))
self.add_deck.setMaximumSize(QtCore.QSize(16777215, 50))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.add_deck.setFont(font)
self.add_deck.setStyleSheet("QPushButton#add_deck\n"
" border: 0;\n"
" border-radius: 20px;\n"
" background-color: rgb(132, 255, 16)\n"
"\n"
"\n"
"QPushButton#add_deck:hover\n"
" background-color: rgb(125, 234, 0)\n"
"\n"
"\n"
"")
self.add_deck.setObjectName("add_deck")
self.scroll_layout.addWidget(self.buttons)
self.verticalLayout_3.addLayout(self.scroll_layout)
self.scroll.setWidget(self.scroll_widget)
self.stacked.addWidget(self.home)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.stacked.addWidget(self.page_2)
main.setCentralWidget(self.centralwidget)
self.retranslateUi(main)
self.stacked.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(main)
def retranslateUi(self, main):
_translate = QtCore.QCoreApplication.translate
main.setWindowTitle(_translate("main", "Flashcards"))
self.label.setText(_translate("main", "All decks"))
self.add_deck.setText(_translate("main", "Add deck"))
#deck_name.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'deck_name.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_deck(object):
def setupUi(self, deck):
deck.setObjectName("deck")
deck.resize(565, 200)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(deck.sizePolicy().hasHeightForWidth())
deck.setSizePolicy(sizePolicy)
deck.setMinimumSize(QtCore.QSize(0, 200))
deck.setWindowTitle("")
self.gridLayoutWidget = QtWidgets.QWidget(deck)
self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 0, 561, 201))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.deck_name = QtWidgets.QLabel(self.gridLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.deck_name.sizePolicy().hasHeightForWidth())
self.deck_name.setSizePolicy(sizePolicy)
self.deck_name.setMinimumSize(QtCore.QSize(0, 50))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(13)
self.deck_name.setFont(font)
self.deck_name.setObjectName("deck_name")
self.gridLayout.addWidget(self.deck_name, 0, 0, 1, 1)
self.button = QtWidgets.QFrame(self.gridLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.button.sizePolicy().hasHeightForWidth())
self.button.setSizePolicy(sizePolicy)
self.button.setMinimumSize(QtCore.QSize(0, 30))
self.button.setMaximumSize(QtCore.QSize(16777215, 50))
font = QtGui.QFont()
font.setFamily("Arial")
self.button.setFont(font)
self.button.setStyleSheet("QPushButton\n"
" border-radius: 12px;\n"
"")
self.button.setObjectName("button")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.button)
self.horizontalLayout.setObjectName("horizontalLayout")
self.review = QtWidgets.QPushButton(self.button)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.review.sizePolicy().hasHeightForWidth())
self.review.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
self.review.setFont(font)
self.review.setStyleSheet("QPushButton#review\n"
" background-color: rgb(132, 255, 16);\n"
"\n"
"\n"
"QPushButton#review:hover\n"
" background-color: rgb(125, 234, 0)\n"
"\n"
"")
self.review.setObjectName("review")
self.horizontalLayout.addWidget(self.review)
self.flash = QtWidgets.QPushButton(self.button)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.flash.sizePolicy().hasHeightForWidth())
self.flash.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
self.flash.setFont(font)
self.flash.setStyleSheet("QPushButton#flash\n"
" background-color:rgb(132, 255, 16);\n"
"\n"
"\n"
"QPushButton#flash:hover\n"
" background-color: rgb(125, 234, 0)\n"
"\n"
"")
self.flash.setObjectName("flash")
self.horizontalLayout.addWidget(self.flash)
self.word_list = QtWidgets.QPushButton(self.button)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.word_list.sizePolicy().hasHeightForWidth())
self.word_list.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
self.word_list.setFont(font)
self.word_list.setStyleSheet("QPushButton#word_list\n"
" background-color:rgb(132, 255, 16);\n"
"\n"
"\n"
"QPushButton#word_list:hover\n"
" background-color: rgb(125, 234, 0)\n"
"\n"
"")
self.word_list.setObjectName("word_list")
self.horizontalLayout.addWidget(self.word_list)
self.gridLayout.addWidget(self.button, 1, 0, 1, 1)
self.dropdown = QtWidgets.QToolButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.dropdown.setFont(font)
self.dropdown.setStyleSheet("border: none")
self.dropdown.setObjectName("dropdown")
self.gridLayout.addWidget(self.dropdown, 0, 1, 1, 1)
self.retranslateUi(deck)
QtCore.QMetaObject.connectSlotsByName(deck)
def retranslateUi(self, deck):
_translate = QtCore.QCoreApplication.translate
self.deck_name.setText(_translate("deck", "Deck_name"))
self.review.setText(_translate("deck", "Review"))
self.flash.setText(_translate("deck", "Flashcards mode"))
self.word_list.setText(_translate("deck", "Word list mode"))
self.dropdown.setText(_translate("deck", "..."))
这是输出:https://drive.google.com/file/d/1uAtbH72oS9NdBzeEOMhW77ZXYZTBMycg/view?usp=sharing 它应该在窗口中有两个分组框
【问题讨论】:
【参考方案1】:您显示的是QtWidgets.QMainWindow
的实例,而不是Main
。尝试将main.py
的最后一部分更改为
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
【讨论】:
以上是关于有没有办法将自定义 QGroupBox 添加到任何小部件/布局?的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法将自定义代码添加到公共静态函数中? (woocommerce update_order_review)