简体   繁体   中英

Python PyQt5 - Detect Click on vertical header item (QTableWidget)

I was wondering if there is any way to detect a mouse click on a vertical header item (row header). I can detect clicks on the cells of the table no problem, but it doesn't seem there is any way to detect clicks on the header items.

self.w3.WorkerTab.clicked.connect(self.putNameInComboBoxFromTableClick)
self.w3.WorkerTab.doubleClicked.connect(self.putDoubleClickDataInSpaces)

I changed the normal given headers (numbers) to be names of employees (rows) and working days (columns). Right now (with the code below) on a single click on any item in a row, the row header content is detected and is put into a ComboBox. On a double click it detects the employee again and opens up another window to customize the employee.

def putNameInComboBoxFromTableClick(self, item):
    selectedName = self.w3.WorkerTab.verticalHeaderItem(item.row())
    NameMA = selectedName.text()
    indexMA = self.w3.chooseWorkerBox.findText(NameMA)
    self.w3.chooseWorkerBox.setCurrentIndex(indexMA)

def putDoubleClickDataInSpaces(self, item):
    selectedName = self.w3.WorkerTab.verticalHeaderItem(item.row())
    NameMA = selectedName.text()
    AZ = "AZ"
    with open(self.database) as mb_json:
        data = json.load(mb_json)
        # Festlegung der Abteilung
        for key1 in data:
            for key2 in data[key1]:
                if key2 == NameMA:
                    self.w4.NameText.setText(NameMA)
                    self.w4.MoText.setCurrentText(data[key1][key2][AZ]["Mo"])
                    self.w4.DiText.setCurrentText(data[key1][key2][AZ]["Di"])
                    self.w4.MiText.setCurrentText(data[key1][key2][AZ]["Mi"])
                    self.w4.DoText.setCurrentText(data[key1][key2][AZ]["Do"])
                    self.w4.FrText.setCurrentText(data[key1][key2][AZ]["Fr"])
                    self.w4.SaText.setCurrentText(data[key1][key2][AZ]["Sa"])
                    self.w4.OeText.setText(key1)
    self.w4.show()
    self.w3.close()

Found a solution myself:

self.w3.WorkerTab.verticalHeader().sectionClicked.connect(self.putNameInComboBoxFromTableClick)
self.w3.WorkerTab.verticalHeader().sectionDoubleClicked.connect(self.putDoubleClickDataInSpaces)

and edited the other code:

def putNameInComboBoxFromTableClick(self):
    row = self.w3.WorkerTab.currentItem().row()
    selectedName = self.w3.WorkerTab.verticalHeaderItem(row)
    NameMA = selectedName.text()
    indexMA = self.w3.chooseWorkerBox.findText(NameMA)
    self.w3.chooseWorkerBox.setCurrentIndex(indexMA)

def putDoubleClickDataInSpaces(self):
    row = self.w3.WorkerTab.currentItem().row()
    selectedName = self.w3.WorkerTab.verticalHeaderItem(row)
    NameMA = selectedName.text()
    AZ = "AZ"
    with open(self.database) as mb_json:
        data = json.load(mb_json)
        # Festlegung der Abteilung
        for key1 in data:
            for key2 in data[key1]:
                if key2 == NameMA:
                    self.w4.NameText.setText(NameMA)
                    self.w4.MoText.setCurrentText(data[key1][key2][AZ]["Mo"])
                    self.w4.DiText.setCurrentText(data[key1][key2][AZ]["Di"])
                    self.w4.MiText.setCurrentText(data[key1][key2][AZ]["Mi"])
                    self.w4.DoText.setCurrentText(data[key1][key2][AZ]["Do"])
                    self.w4.FrText.setCurrentText(data[key1][key2][AZ]["Fr"])
                    self.w4.SaText.setCurrentText(data[key1][key2][AZ]["Sa"])
                    self.w4.OeText.setText(key1)
    self.w4.show()
    self.w3.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM