简体   繁体   中英

How to get the row index in QtableWidget

I have a qtablewidget with mysql table, and in the last column one button that i want to click to get the row index. Can i get the row index and id of the table from the qtablewidget?

    def datails(self):

    try:
        con=mysql.connector.connect(host='192.168.56.1', user='root', password='', database='valparaiso')

        consulta_sql = f"""SELECT a.id_apto,m.nome_morador,m.celular_morador,m.email_morador,m.rg_morador,m.tipo,a.apartamento,a.torre FROM Moradores AS m JOIN aptos AS a ON m.apto_morador=a.id_apto WHERE stats=1 AND m.id_morador = {_id}"""
        cur = con.cursor()
        cur.execute(consulta_sql)
        rows=cur.fetchall()
        print(rows)
    
        self.ui.ui_pages.Tabela.setRowCount(cur.rowcount)
        tablerow=0
        for row in rows:
            self.ui.ui_pages.table.setItem(tablerow,0,QTableWidgetItem(row[1]))
            self.ui.ui_pages.table.setItem(tablerow,1,QTableWidgetItem(row[2]))
            self.ui.ui_pages.table.setItem(tablerow,2,QTableWidgetItem(row[3]))
            self.ui.ui_pages.table.setItem(tablerow,3,QTableWidgetItem(row[4]))
            self.ui.ui_pages.table.setItem(tablerow,4,QTableWidgetItem(row[5]))
            self.ui.ui_pages.table.setItem(tablerow,5,QTableWidgetItem(row[6]))
            self.ui.ui_pages.table.setItem(tablerow,6,QTableWidgetItem(row[7]))
            self.ui.ui_pages.table.setCellWidget(tablerow,7,self.build_btn())

            tablerow +=1

    except:
        print("erro", error)
    finally:
        if(con.is_connected()):
            con.close()
            cur.close()

With the function

self.ui.ui_pages.table.currentRow()

you should get the row of the selected item. More details in the documentation .

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