How to find out the clickable widget index using pyqt4

后端 未结 1 1921
不思量自难忘°
不思量自难忘° 2021-01-26 05:07

Here in my program i need to create a multiple widget orders so i am using the list of dictionary by using that i create a list of widget.I used mouse release event for clicka

相关标签:
1条回答
  • 2021-01-26 05:54

    While an application increases in complexity it is better to use the specialization strategy, that is, create a class that only has a responsibility, in this case create a class that emits a signal when it is pressed with the mouse, and that class must have the information that sample to access it easily.

    from PyQt4 import QtCore, QtGui
    
    class InfoWidget(QtGui.QWidget):
        clicked = QtCore.pyqtSignal()
    
        def __init__(self, info, parent=None):
            super(InfoWidget, self).__init__(parent)
            self._info = info
    
            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    
    
        def mousePressEvent(self, event):
            self.clicked.emit()
            super(InfoWidget, self).mousePressEvent(event)
    
    class Orders(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(Orders, self).__init__(parent)
            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.qvw1 = QtGui.QWidget()
            self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
            self.scrollArea.setWidget(self.qvw1)
    
            online_order_hbox = QtGui.QVBoxLayout(self.qvw1)
            self.online_label = QtGui.QPushButton("Online Order")
            self.online_label.setStyleSheet("QPushButton{ background-color: #FF8C00; color: white;outline : None;}")
            online_order_hbox.addWidget(self.online_label,QtCore.Qt.AlignTop)
            self.qvw1.setFixedWidth(550)
    
            for i in Online_order_dict:
                w = InfoWidget(i)
                w.clicked.connect(self.on_info_clicked)
                online_order_hbox.addWidget(w)
            self.setCentralWidget(self.scrollArea)
    
        @QtCore.pyqtSlot()
        def on_info_clicked(self):
            w = self.sender()
            print(w.info)
    
    if __name__ == '__main__':
        import sys
        app = QtGui.QApplication(sys.argv)
        settingobj= Orders()
        settingobj.showFullScreen()
        sys.exit(app.exec_())
    
    0 讨论(0)
提交回复
热议问题