如何使用pyqt获取表中的选定项?

时间:2014-04-09 15:30:53

标签: python mysql database selecteditem

我在mysql数据库的表中显示数据, 之后,我想在单击表中的行时,它将打印行中的值,即打印id数据库中的现有数据。我如何让它成为那样的?

这是源代码:

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_())

1 个答案:

答案 0 :(得分:2)

这行代码将选择current row

r = self.tbl_anggota.currentRow()

如果您知道列(即它不是动态的),则可以通过执行以下操作来提取每个单元格的值:

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

在这种情况下,item调用中的01是您的列,位于0索引数组中。请记住,隐藏的列仍然有效。

要在点击一行时自动执行此操作,您将要使用信号itemClicked