PyQt:将值从一个类传递给另一个类

时间:2017-08-27 16:12:21

标签: python python-2.7 pyqt pyqt4

如果有人可以帮助我,我想从这个变量“self.item_clicked”中获得从Class First到Class Second的值

提前致谢

conn = sqlite3.connect('DBM.db')
conn.text_factory = str

c = conn.cursor()
#for row in c.execute('SELECT * FROM typem'):
#    print(row)


class Second2(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Second2, self).__init__(parent)

        #self.pushButton = QtGui.QPushButton("Close")
        #self.setCentralWidget(self.pushButton)
        self.setWindowTitle('Number')
        self.setGeometry(500, 400, 100, 100)
        self.label = QtGui.QLabel(self)
        self.setCentralWidget(self.label)
        self.label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.label.setAlignment(Qt.AlignCenter)    

    def setValue(self, item):
        select_item = c.execute('SELECT Num FROM typem WHERE Type=?', (item, ))
        print item
        self.label.setText(item)



class First(QListWidget):
    def __init__(self, parent=None):
        super(First, self).__init__(parent)
        self.add_items()
        self.itemDoubleClicked.connect(self.item_click)
        self.dialog = Second2(self)


    def item_click(self, item):
        self.item_clicked = item.text()
        self.dialog.setValue(self.item_clicked)
        self.dialog.show()

    def add_items(self):
        for row in c.execute('SELECT Type FROM typem'):
            item = QListWidgetItem("{0}".format(row[0]))
            self.addItem(item)


def main():
    app = QtGui.QApplication(sys.argv)
    main = First()
    main.show()
    sys.exit(app.exec_())



if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

您需要做的是创建一个接收该值的方法,如果您想在该方法中处理该数据,则如下所示:

class Second2(QtGui.QMainWindow):
    [...]
    def setValue(self, item):
        print(item)
        select_item = c.execute('SELECT Num FROM typem WHERE Type=(?)', item)

class First(QListWidget):
    [...]
    def item_click(self, item):
        self.item_clicked = item.text()
        print self.item_clicked
        self.dialog.setValue(self.item_clicked)
        self.dialog.show()