Pyside2 - 无法在插槽内获取发件人

时间:2018-03-23 09:55:06

标签: python-3.x pyqt pyqt5 pyside pyside2

无法获取发出信号的发件人对象。 sender()始终返回None。 以下代码适用于pyqt5,

试图让qtablewidget中的哪个组合框在插槽内发出信号。

提到了docs和net,看起来pyside采用不同于获取发件人的方法而不是pyqt。 试过自己看到了一个啧啧但总是得到无。

将发件人放入pyside的插槽中最简单的方法是什么?

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtSql import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtCore import QSize
from PySide2.QtGui import QImage, QPalette, QBrush
import sys
import time
import sqlite3
import requests
import os

wwd=1366
wwh=768

class Ui_MainWindow(object):    
    def loadData(self):        
        conn = sqlite3.connect('uniprotect.db',timeout=60);
        query='select u_key,u_user,u_password,remote,port,rusername,rpassword,ragent,u_status from user_info'
        result=conn.execute(query)
        self.tableWidget.setRowCount(0)

        for row_number, row_data in enumerate(result):
            self.tableWidget.insertRow(row_number)
            for column_number, data in enumerate(row_data):   
                self.tableWidget.setItem(row_number,column_number,QtWidgets.QTableWidgetItem(str(data)))
                if str(column_number)=='8':
                    comboBox = QtWidgets.QComboBox()

                    comboBox.addItems(['Enabled','Disabled'])
                    index = comboBox.findText(data, QtCore.Qt.MatchFixedString)
                    print('combo index ',index)
                    comboBox.setCurrentIndex(index)
                    comboBox.setCurrentIndex(index)
                    comboBox.setProperty('row', row_number)  
                    comboBox.currentIndexChanged.connect(self.Combo_indexchanged)                                        
                    self.tableWidget.setCellWidget(row_number,column_number,comboBox)
        conn.close();        
        self.tableFields = ["Key","UserName","Password","Remote", "Port","Rusername","Rpassword", "Agent","AccountStatus"]
        self.tableWidget.setColumnCount(len(self.tableFields))
        self.tableWidget.setHorizontalHeaderLabels(self.tableFields)

    def Combo_indexchanged(self):
        print(self.MainWindow.sender())
        combo = self.MainWindow.sender()
        row = combo.property('row')
        index = combo.currentIndex()
        print('combo row %d indexChanged to %d' % (row, index))

    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowModality(QtCore.Qt.ApplicationModal)
        MainWindow.resize(1366,768)
        MainWindow.setFocusPolicy(QtCore.Qt.StrongFocus)        
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.tableWidget=QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(wwd*0.15, wwh*0.15, wwd*0.85,wwh*0.85))
        self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)

        self.tableWidget.setRowCount(50)
        self.tableWidget.setColumnCount(10)
        self.tableWidget.setObjectName("TableWIdget")

        MainWindow.setCentralWidget(self.centralwidget)
        self.loadData();
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)    
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

根据Qt的pyside2 documentationsender()PySide2.QtCore.QObject的一种方法。

您不能将其称为静态方法,而只能从QObject内部调用。

但是,您所有的窗口小部件都是class Ui_MainWindow(object)的成员,并且该类并不继承自QObject。

所以改变

class Ui_MainWindow(object):    

class Ui_MainWindow(QtCore.QObject):    

为了使您班上的sender()可用。

现在您可以在插槽self.sender()中呼叫Combo_indexchanged()

def Combo_indexchanged(self):
    combo = self.sender()
    row = combo.property('row')
    index = combo.currentIndex()
    print('combo row %d indexChanged to %d' % (row, index))