使用QtDesigner在Pyqt中使用ScrollArea的问题

时间:2016-11-01 09:17:03

标签: python scroll pyqt qt-designer

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

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))

我想添加标签,这就是我的工作方式

from main_ui import *
import sys
global height
height=10
def send():
        global height
        label=QtGui.QLabel(myapp.ui.horizontalLayoutWidget)
        label.setGeometry(QtCore.QRect(10, height, 46, 13))
        height=height+label.height()
        label.setText(myapp.ui.lineEdit.text())
        label.show()


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()

myapp.show()
QtCore.QObject.connect(myapp.ui.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), send)
myapp.setCentralWidget(myapp.ui.centralwidget)
QtCore.QMetaObject.connectSlotsByName(myapp)
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

先生,您的代码有点混乱。

这是一个小例子,看它是否有效。没有秘密。的: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: 您只需更改QPushButtons,QLabels,...而不是小QWidgets以及您需要的任何内容。