简体   繁体   English

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

[英]How to get selected item in table using pyqt?

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. 我在mysql数据库中的表中显示数据,此后,我想单击表中的行时,它将在该行中打印该值,即在该ID中打印数据库中的现有数据。 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_())

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. 在这种情况下, 项目调用中的01是您在0索引数组中的列。 Remember that hidden columns still count. 请记住,隐藏的列仍然有效。

To do this automatically when a row is clicked, you'll want to use the signal itemClicked 要在单击某行时自动执行此操作,您将需要使用信号itemClicked

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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