Question

I display data in a table from mysql database, after that I want to when the rows in the table is clicked it will print the value in the line,that in print id existing data in the database. How do I make it to be like that?

Here's the source code:

from PyQt4 import QtCore, QtGui
import sys
import MySQLdb
from form.DBConnection import Connection
import MySQLdb as mdb
db = Connection()
myCursor = db.name().cursor()

"................................................ ....................................."

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(655, 356)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.tbl_anggota = QtGui.QTableWidget(self.centralwidget)
        self.tbl_anggota.setGeometry(QtCore.QRect(15, 40, 511, 192))
        self.tbl_anggota.setObjectName(_fromUtf8("tbl_anggota"))
        self.tbl_anggota.setColumnCount(3)
        self.tbl_anggota.setRowCount(0)
        item = QtGui.QTableWidgetItem()
        self.tbl_anggota.setHorizontalHeaderItem(0, item)
        item = QtGui.QTableWidgetItem()
        self.tbl_anggota.setHorizontalHeaderItem(1, item)
        item = QtGui.QTableWidgetItem()
        self.tbl_anggota.setHorizontalHeaderItem(2, item)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 655, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        self.table(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        item = self.tbl_anggota.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "NIM", None))
        item = self.tbl_anggota.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "NAMA", None))
        item = self.tbl_anggota.horizontalHeaderItem(2)

    def table(self, MainWindow):
        myCursor.execute("SELECT * FROM anggota")
        jum_baris= myCursor.fetchall()
        self.tbl_anggota.setRowCount(len(jum_baris)) 
        self.tbl_anggota.setColumnCount(3) 
        for i in range (len(jum_baris)):
            for j in range (3):
                item = Qt.QTableWidgetItem('%s' % (jum_baris[i][j + 1]))
                self.tbl_anggota.setItem(i, j, item)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Was it helpful?

Solution

This line of code will select the current row

r = self.tbl_anggota.currentRow()

If you know your columns (ie. it's not dynamic) you can then pull the value of each of your cells by doing this:

field1 = self.tbl_anggota.item(r,0).text()
field2 = self.tbl_anggota.item(r,1).text()

In this case, the 0 and the 1 in the item call are your columns, in a 0 indexed array. Remember that hidden columns still count.

To do this automatically when a row is clicked, you'll want to use the signal itemClicked

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top