0

连接到同一模型的两个独立形式。据我了解模型视图是如何工作的,所有连接到模型的视图都应该在模型中呈现数据。但这种情况并非如此。请参阅随附的代码。

class Model(QStringListModel):

    def __init__(self, parent=None):
        super(Model, self).__init__(parent)
        data = QStringList()
        data << "one" << "two" << "three" << "four" << "five"
        self.setStringList(data)


class Form_2(QDialog):

    def __init__(self, parent=None):
        super(Form_2, self).__init__(parent)
        self.model = Model()
        self.combo = QListView()
        self.combo.setModel(self.model)

        layout = QVBoxLayout()
        layout.addWidget(self.combo)
        self.setLayout(layout)


class Form_1(QDialog):

    def __init__(self, parent=None):
        super(Form_1, self).__init__(parent)
        self.model = Model()
        self.listView = QListView()
        self.listView.setModel(self.model)
        self.combo = QComboBox()
        self.combo.setModel(self.model)
        self.form2_button = QPushButton("Open Form_2")

        layout = QVBoxLayout()
        layout.addWidget(self.listView)
        layout.addWidget(self.combo)
        layout.addWidget(self.form2_button)
        self.setLayout(layout)

        self.connect(self.form2_button, SIGNAL("clicked()"), self.form_2)

    def form_2(self):
        self.ft = Form_2()
        self.ft.show()


app = QApplication(sys.argv)
form = Form_1()
form.show()
app.exec_()
4

1 回答 1

0

感谢 ccc.larc 回答我的问题。您基本上需要将模型的相同实例传递给您希望它使用的任何形式。

class Model(QStringListModel):

    def __init__(self, parent=None):
        super(Model, self).__init__(parent)
        data = QStringList()
        data << "one" << "two" << "three" << "four" << "five"
        self.setStringList(data)


class Form_2(QDialog):

    def __init__(self, model, parent=None):
        super(Form_2, self).__init__(parent)
        self.model = model
        self.combo = QListView()
        self.combo.setModel(self.model)

        layout = QVBoxLayout()
        layout.addWidget(self.combo)
        self.setLayout(layout)


class Form_1(QDialog):

    def __init__(self, parent=None):
        super(Form_1, self).__init__(parent)
        self.model = Model()
        self.listView = QListView()
        self.listView.setModel(self.model)
        self.combo = QComboBox()
        self.combo.setModel(self.model)
        self.form2_button = QPushButton("Open Form_2")

        layout = QVBoxLayout()
        layout.addWidget(self.listView)
        layout.addWidget(self.combo)
        layout.addWidget(self.form2_button)
        self.setLayout(layout)

        self.connect(self.form2_button, SIGNAL("clicked()"), self.form_2)

    def form_2(self):
        self.ft = Form_2(self.model)
        self.ft.show()


app = QApplication(sys.argv)
form = Form_1()
form.show()
app.exec_()
于 2013-04-01T21:39:08.523 回答