使用QtDesign的Pyqt中的ScrollArea问题

2024-04-25 14:18:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我用QtDesigner设计了一个窗口,并在其中添加了一个滚动区域和一个布局,但滚动区域不滚动,它显示滚动条,但它根本无法滚动。在

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

class Ui_MainWindow(object):

def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(767, 600)
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.pushButton = QtGui.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(230, 300, 75, 23))
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    self.lineEdit = QtGui.QLineEdit(self.centralwidget)
    self.lineEdit.setGeometry(QtCore.QRect(80, 330, 371, 71))
    self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtGui.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 767, 21))
    self.menubar.setObjectName(_fromUtf8("menubar"))
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtGui.QStatusBar(MainWindow)
    self.statusbar.setObjectName(_fromUtf8("statusbar"))
    self.scrollArea = QtGui.QScrollArea(self.centralwidget)
    self.scrollArea.setGeometry(QtCore.QRect(130, 70, 301, 191))
    self.scrollArea.setWidgetResizable(True)
    self.scrollArea.setMinimumSize(QtCore.QSize(100, 100))
    self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
    self.scrollAreaWidgetContents = QtGui.QWidget()
    self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 299, 189))
    self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
    self.scrollAreaWidgetContents.setMinimumSize(QtCore.QSize(100, 100))
    self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
    self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    self.horizontalLayoutWidget = QtGui.QWidget(self.scrollAreaWidgetContents)
    self.horizontalLayoutWidget.setGeometry(QtCore.QRect(-1, -1, 301, 191))
    self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
    self.horizontalLayout = QtGui.QVBoxLayout(self.horizontalLayoutWidget)
    self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))



    p= QtGui.QPalette()
    p.setColor(QtGui.QPalette.Background, QtCore.Qt.white)
    self.scrollArea.setAutoFillBackground(True)
    self.scrollArea.setPalette(p)
    self.retranslateUi(MainWindow)

def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
    self.pushButton.setText(_translate("MainWindow", "send", None))

我想添加标签,我就是这样做的

^{pr2}$

Tags: selfdeftranslateqtguiqtcorepushbuttonsetobjectnamesetgeometry
1条回答
网友
1楼 · 发布于 2024-04-25 14:18:53

先生,你的密码有点乱。在

这里有一个小例子,看看它是否有效。没有什么秘密。:D

在PyQt5。你只要把你的进口改成PyQt4就行了。

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QScrollArea
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget


class WSlideBar(QWidget):
    """WSlideBar is a personalized slide bar."""

    w_container = None
    v_layout_container = None
    v_scroll_area = None
    v_layout_preview = None


    def __init__(self):
        """Init UI."""

        super(WSlideBar, self).__init__()
        self.init_ui()

    def init_ui(self):
        """Init all ui object requirements."""


        self.setFixedSize(100,500)

        self.setStyleSheet("""
            background: gray;
        """)

        # Container Widget
        self.w_container = QWidget()
        self.w_container.setFixedWidth(100)

        # Layout of Container Widget
        self.v_layout_container = QVBoxLayout()
        self.v_layout_container.setSpacing(100)

        aux = QWidget()
        aux.setFixedSize(10,10)
        aux.setStyleSheet("""background: red;""")

        aux2 = QWidget()
        aux2.setFixedSize(20, 20)
        aux2.setStyleSheet("""background: blue;""")

        aux3 = QWidget()
        aux3.setFixedSize(15, 15)
        aux3.setStyleSheet("""background: yellow;""")

        aux4 = QWidget()
        aux4.setFixedSize(50,50)
        aux4.setStyleSheet("""background: rgb(0,255,0,30%);""")

        aux5 = QWidget()
        aux5.setFixedSize(40, 40)
        aux5.setStyleSheet("""background: green;""")

        aux6 = QWidget()
        aux6.setFixedSize(40, 40)
        aux6.setStyleSheet("""background: green;""")

        self.v_layout_container.addWidget(aux)
        self.v_layout_container.addWidget(aux2)
        self.v_layout_container.addWidget(aux3)
        self.v_layout_container.addWidget(aux4)
        self.v_layout_container.addWidget(aux5)
        self.v_layout_container.addWidget(aux6)

        self.w_container.setLayout(self.v_layout_container)

        self.v_scroll_area = QScrollArea(self)
        self.v_scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.v_scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.v_scroll_area.setWidget(self.w_container)

        # Scroll Area Layer add
        self.v_layout_preview = QVBoxLayout()
        self.setLayout(self.v_layout_preview)

        self.v_layout_preview.addWidget(self.v_scroll_area)

def run():
    app = QApplication(sys.argv)
    GUI = WSlideBar()
    GUI.show()
    sys.exit(app.exec_())


run()

OBS:您只需更改qpushbutton、qlabel、,。。。而不是小的QWidgets和你需要的任何东西。

相关问题 更多 >