如何使用pyqt4固定按钮小部件在vbox布局中的位置

时间:2019-01-27 11:34:12

标签: python-2.7 pyqt4

在我的程序中,我在vbox布局中有多个小部件。当我单击订单中的任何窗口小部件时,该窗口小部件便转移到另一个订单,并且我按当前顺序删除了该窗口小部件,但是问题是当我删除了所有窗口小部件时,我的按钮即将进入拆分器的中心。任何人都可以帮我如何在vbox中固定按钮的位置吗?我尝试设置几何形状和移动方法,但没有正确获得输出。预先谢谢你。

from PyQt4 import QtCore, QtGui
import sys, collections
from functools import partial
class InfoWidget(QtGui.QWidget):
    clicked = QtCore.pyqtSignal()

    def __init__(self, index, info, parent=None):
        super(InfoWidget, self).__init__(parent)
        self._info = info
        self._index = index

        date_label = QtGui.QLabel("{}   {}".format(info["Date"], info["Id"]), alignment= QtCore.Qt.AlignCenter, objectName="small")
        amount_label = QtGui.QLabel("Amount:{}".format(info["Amount"]))
        shopping_label = QtGui.QLabel("Shopping :{}".format(info["Shopping"]))
        ordercity_label = QtGui.QLabel("Order City :{}".format(info["Order_City"]))
        orderstate_label = QtGui.QLabel("Order State :{}".format(info["Order_State"]))
        hline = QtGui.QFrame(frameShape=QtGui.QFrame.HLine)

        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(date_label)
        lay.addWidget(amount_label)
        lay.addWidget(shopping_label)
        lay.addWidget(ordercity_label)
        lay.addWidget(orderstate_label)
        lay.addWidget(hline)
    @property
    def info(self):
        return self._info
    @property
    def index(self):
        return self._index
    def mousePressEvent(self, event):
        self.clicked.emit()
        super(InfoWidget, self).mousePressEvent(event)
        self.setParent(None)

class Orders(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Orders, self).__init__(parent)
        self.Online_order_dict = [
            {"Date" : "jan-24-2019", "Id" : "#175", "Amount" : "191 rs", "Shopping" : "Online", "Order_City" : "Hyderbad", "Order_State" : "TELANGANA"},
            {"Date" : "jan-25-2019", "Id" : "#186", "Amount" : "200 Rs", "Shopping" : "Online", "Order_City" : "Hyderbad", "Order_State" : "TELANGANA"},
            {"Date" : "jan-29-2019", "Id" : "#188", "Amount" : "250 Rs", "Shopping" : "Online", "Order_City" : "Hyderbad", "Order_State" : "TELANGANA"},
            {"Date" : "jan-25-2019", "Id" : "#176", "Amount" : "200 Rs", "Shopping" : "Online", "Order_City" : "Hyderbad", "Order_State" : "TELANGANA"},
            {"Date" : "jan-28-2019", "Id" : "#201", "Amount" : "250 Rs", "Shopping" : "Online", "Order_City" : "Hyderbad", "Order_State" : "TELANGANA"}
        ]


        self.Offline_order_dict = [
            {"Date":"jan-2-2019","Id":"#1","Amount":"100", "Shopping":"offline","Order_City":"Hyderbad","Order_State":"TELANGANA"},
            {"Date":"jan-2-2019","Id":"#15","Amount": " 200", "Shopping":"Offline","Order_City":"Hyderbad","Order_State":"TELANGANA"},
            {"Date":"jan-5-2019","Id":"#17","Amount": "250", "Shopping":"Offline","Order_City":"Hyderbad","Order_State":"TELANGANA"},
            {"Date":"jan-5-2019","Id":"#16","Amount": "200", "Shopping":"Offline","Order_City":"Hyderbad","Order_State":"TELANGANA"},
            {"Date":"jan-10-2019","Id":"#202","Amount": "200", "Shopping":"Offline","Order_City":"Hyderbad","Order_State":"TELANGANA"}
        ]

        self.qvw1 = QtGui.QWidget()
        self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
        self.scrollArea.setWidget(self.qvw1)

        self.online_order_hbox = QtGui.QGridLayout(self.qvw1)
        self.online_label = QtGui.QPushButton("Online Order")
        self.online_label.setStyleSheet("QPushButton{ background-color: #FF8C00; color: white;outline : None;}")
        self.online_order_hbox.addWidget(self.online_label,0,0)
        self.qvw1.setFixedWidth(300)
        self.qvw2 = QtGui.QWidget()
        self.scrollArea1 = QtGui.QScrollArea(widgetResizable=True)
        self.scrollArea1.setWidget(self.qvw2)

        self.offline_order_hbox = QtGui.QGridLayout(self.qvw2)
        self.offline_label = QtGui.QPushButton("offline Order")
        self.offline_label.setStyleSheet("QPushButton{ background-color: #FF8C00; color: white;outline : None;}")
        self.offline_order_hbox.addWidget(self.offline_label,0,0)
        self.qvw2.setFixedWidth(300)


        splitter = QtGui.QSplitter()
        splitter.addWidget(self.scrollArea)
        splitter.addWidget(self.scrollArea1)

        for index, i in enumerate(self.Online_order_dict):
            w = InfoWidget(index, i)
            w.clicked.connect(self.on_info_clicked)
            self.online_order_hbox.addWidget(w,index+1,0)

        for index, i in enumerate(self.Offline_order_dict):
            w = InfoWidget(index, i)
            w.clicked.connect(self.on_info_clicked)
            self.offline_order_hbox.addWidget(w,index+1,0)

        self.setCentralWidget(splitter)
    @QtCore.pyqtSlot()
    def on_info_clicked(self):
        w = self.sender()

        index = w.index
        if 0 <= index < len(self.Online_order_dict):
            poped_item = self.Online_order_dict.pop(index)
            self.Offline_order_dict.insert(0, poped_item)
        w = InfoWidget(0,w.info)
        self.offline_order_hbox.addWidget(w)
        print type(self.Online_order_dict), "typeee"

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    settingobj= Orders()
    settingobj.showFullScreen()
    sys.exit(app.exec_())

0 个答案:

没有答案