如何使用pyqt4将数据设置到QscrollArea

时间:2019-02-18 12:02:20

标签: python-2.7 pyqt4

在我的示例代码中,我想将我的字典数据显示在滚动区域中。我尝试了很多方法,但是我没有得到正确的显示。我也没有出现任何错误。有人可以告诉我为什么我的数据不会显示在滚动区域中。我在以下程序中犯了错误的地方,请指导我。谢谢。

下面是我的示例代码:

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        self.products_list = {
            "Givers":{
                "price":        140,
                "Quantity":     100,
                "Discount":       0,
                "Total_price":14000,
                "Paid_amount":14000},
            "New_Member":{
                "price":       1200,
                "Quantity":      10,
                "Discount":       0,
                "Total_price":12000,
                "Paid_amount":12000},
            "YearMembers_Ribbons":{
                "price":       110,
                "Quantity":      0,
                "Discount":      0,
                "Total_price":1100,
                "Paid_amount":1100}
        }

        self.grid = QtGui.QGridLayout(self)


        hbox = QtGui.QHBoxLayout()
        self.clbtn = QtGui.QPushButton('close')
        self.clbtn.clicked.connect(self.close)
        self.lab = QtGui.QLabel("orders")
        hbox.addWidget(self.lab,alignment = QtCore.Qt.AlignCenter)
        hbox.addStretch()
        hbox.addWidget(self.clbtn,alignment = QtCore.Qt.AlignRight)
        self.grid.addLayout(hbox,0,0)
        self.line = QtGui.QFrame(frameShape=QtGui.QFrame.HLine)
        self.grid.addWidget(self.line,1,0)


        self.main_hbox = QtGui.QHBoxLayout()

        self.scroll = QtGui.QScrollArea()

        self.show()

        self.VBox = QtGui.QVBoxLayout()
        self.Date = QtGui.QLabel("25-2-2018",objectName="small",alignment = QtCore.Qt.AlignRight)
        self.v_city = QtGui.QLabel("bangalore",alignment = QtCore.Qt.AlignCenter)
        self.v_state = QtGui.QLabel("karnataka",alignment = QtCore.Qt.AlignCenter)
        self.v_pincode = QtGui.QLabel("560003",alignment = QtCore.Qt.AlignCenter)
        self.v_country = QtGui.QLabel("India",alignment = QtCore.Qt.AlignCenter)

        self.VBox.addWidget(self.Date)
        self.VBox.addWidget(self.v_city)
        self.VBox.addWidget(self.v_state)
        self.VBox.addWidget(self.v_pincode)
        self.VBox.addWidget(self.v_country)

        self.main_hbox.addLayout(self.VBox)
        self.main_hbox.addWidget(self.scroll)
        self.grid.addLayout(self.main_hbox,2,0)

        w2 = QtGui.QWidget()
        self.scroll.setWidget(w2)
        grid1 = QtGui.QGridLayout(w2)
        self.items_Vboxlay = QtGui.QVBoxLayout()
        self.vbox2         = QtGui.QVBoxLayout()
        for key, value in self.products_list.items():
            keys   = [ vk for vk in value.keys()]
            values = [ vv for vv in value.values()]
            self.item_label = QtGui.QLabel(str(key), objectName="small")
            self.vbox2.addWidget(self.item_label)
            self.hbox1 = QtGui.QHBoxLayout()
            self.hbox2 = QtGui.QHBoxLayout()
            self.price      = QtGui.QLabel(keys[1] + ": " +  u'\u20B9 ' +str(values[1]))
            self.qnty       = QtGui.QLabel(keys[3] + ": " + str(values[3]))
            self.discount   = QtGui.QLabel(keys[0] + ": " + u'\u20B9 ' + str(values[0]))
            self.totalprice = QtGui.QLabel(keys[2] + ": " + u'\u20B9 ' + str(values[2]))
            self.status = QtGui.QLabel("Status" + ": "+ "Created")
            self.ref_amount = QtGui.QLabel("Refund Amount"+": "+ "0")
            self.ref_status = QtGui.QLabel("Refund Status"+": "+ "False")

            self.hbox1.addWidget(self.price)
            self.hbox1.addWidget(self.qnty)
            self.hbox1.addWidget(self.discount)
            self.hbox1.addWidget(self.totalprice)

            self.hbox2.addWidget(self.status)
            self.hbox2.addWidget(self.ref_amount)
            self.hbox2.addWidget(self.ref_status)

            self.line4 = QtGui.QFrame()
            self.line4.setFrameShape(QtGui.QFrame.HLine)
            self.vbox2.addLayout(self.hbox1)
            self.vbox2.addLayout(self.hbox2)
            self.vbox2.addWidget(self.line4)

        self.items_Vboxlay.addLayout(self.vbox2)
        grid1.addLayout(self.items_Vboxlay,0,0)


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    ex.resize(500,300)
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

在ScrollArea中设置窗口小部件时,它使用的大小是当时的窗口小部件大小,在您的情况下,它没有任何大小,因此窗口小部件的大小将为0、0,尽管您通过QScrollArea布局不会考虑在内,因此要使QScrollArea监视大小,必须启用widgetResizable属性:

self.scroll = QtGui.QScrollArea(widgetResizable=True)