Pyside2 - unable to get sender inside the slot

2019-08-30 13:02发布

Can't able to get the sender object which emitted the signal. The sender() always returns None. The below code works in pyqt5,

Trying to get which combobox in qtablewidget is emitted the signal, inside the slot.

Referred the docs and net, looks like pyside has different approach for acquiring sender than pyqt. Tried myself seeing a tut but always getting None.

What was the eaisiest way to get sender inside a slot in 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条回答
我命由我不由天
2楼-- · 2019-08-30 13:29

According to Qt's pyside2 documentation, sender() is a method of PySide2.QtCore.QObject.

You can not call it as a static method, but only from within a QObject.

However, all your widgets are members of class Ui_MainWindow(object) and that class does not inherit from QObject.

So change

class Ui_MainWindow(object):    

to

class Ui_MainWindow(QtCore.QObject):    

in order to make sender() available from within your class.

Now you can call self.sender() in the slot 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))
查看更多
登录 后发表回答