Pyside QTableWidget不会等待在单击事件中接受或拒绝对话框

时间:2015-07-26 18:39:05

标签: python pyside

当我创建一个Dialog并在QTableWidget之外测试它时,它似乎工作得很好,就像在模态和阻塞中一样。但是,当我将它嵌入到QTableWidget子类(连接到cellClicked)事件的cell_clicked方法中时,我的dialog.exec_()方法不起作用模式,该方法只是由它运行,在我接受之前打印出结果拒绝。我做错了什么?

import PySide.QtGui as qg
import PySide.QtCore as qc

class TableWidget(qg.QTableWidget):
    def __init__(self):
        super(TableWidget, self).__init__()
        self.setRowCount(1)
        self.setColumnCount(2)
        self.setHorizontalHeaderLabels(['a','b'])
        self.setHorizontalScrollBarPolicy(qc.Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.cellClicked.connect(self.cell_clicked)
        self.setShowGrid(False)
        self.setAlternatingRowColors(True)
#        self.resizeColumnsToContents()

    def cell_clicked(self,row,column):
        print(row,column)
        self.d = Dialog1()
        result = self.d.exec_()
        print(result)
        if result:
            results = self.d.results()
            model = self.model()
            index = model.index(row,column)
            item = self.itemFromIndex(index)
            item.setData(index,results,qc.Qt.ItemDataRole.DisplayRole)

class Dialog1(qg.QDialog):
    def __init__(self):
        super(Dialog1,self).__init__()
        self.text_edit = qg.QLineEdit()
        button_ok = qg.QPushButton('Ok')
        button_cancel = qg.QPushButton('Cancel')

        sub_layout1 = qg.QHBoxLayout()
        sub_layout1.addWidget(button_ok)
        sub_layout1.addWidget(button_cancel)

        layout = qg.QVBoxLayout()
        layout.addWidget(self.text_edit)
        layout.addLayout(sub_layout1)

        self.setLayout(layout)
        button_ok.clicked.connect(self.accept)
        button_cancel.clicked.connect(self.reject)

    def results(self):
        return self.text_edit.text()

if __name__ =='__main__':
    import sys
    app = qg.QApplication(sys.argv)
    dialog = Dialog1()
    result = dialog.exec_()
    print(result)    

    widget = TableWidget()
    widget.show()

    sys.exit(app.exec_)

1 个答案:

答案 0 :(得分:1)

它在最后一行......我用了

sys.exit(app.exec_)

而不是

sys.exit(app.exec_())

卫生署!!!