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

How to set my data into QscrollArea using pyqt4

在我的示例代码中,我想将我的字典数据显示到滚动条中 Area.I 尝试了很多方法,但我没有得到 properly.i 没有得到任何错误 also.Can任何人请告诉我为什么我的数据没有显示在滚动中area.where我在下面的程序中犯了错误请提前指导me.Thank你。

下面是我的示例代码:

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

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

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