Найти - Пользователи
Полная версия: Поиск по QTableWidget
Начало » GUI » Поиск по QTableWidget
1
snakeand1
Привет всем!
Как можно организовать поиск по таблице QTableWidget?
KriO
#!/usr/bin/env python2
# -*- coding: iso-8859-5 -*-
import sys
from PyQt4 import QtGui,QtCore
class TableDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(TableDialog, self).__init__(parent)
        self.setWindowModality(QtCore.Qt.NonModal)
        self.setWindowTitle(u"Таблица")
        self.vboxlayout = QtGui.QVBoxLayout()
        self.vboxlayout.setContentsMargins(0, 0, 0, 0)
        self.vboxlayout.setSpacing(0)
        self.table = QtGui.QTableWidget(10, 10)
        self.table.horizontalHeader().setStretchLastSection(True)
        self.searchParamLab = QtGui.QLabel(u"&Найти")
        self.searchParam = QtGui.QLineEdit()
        self.searchParam.setMinimumWidth(100)
        self.searchParam.editingFinished.connect(self.findParam)
        self.searchParamLab.setBuddy(self.searchParam)
        self.hb = QtGui.QHBoxLayout()
        self.hb.setContentsMargins(0, 0, 0, 0)
        self.hb.setSpacing(2)
        self.hb.addStretch(1)
        self.hb.addWidget(self.searchParamLab)
        self.hb.addWidget(self.searchParam)
        self.hboxlayout = QtGui.QHBoxLayout()
        self.hboxlayout.setContentsMargins(5, 5, 5, 5)
        self.hboxlayout.setSpacing(10)
        self.buttonBox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Cancel)
        self.buttonBox.button(QtGui.QDialogButtonBox.Cancel).setText(u"Закрыть")
        self.buttonBox.button(QtGui.QDialogButtonBox.Cancel).setAutoDefault(False)
        self.buttonBox.rejected.connect(self.reject)
        self.hboxlayout.addLayout(self.hb)
        self.hboxlayout.addStretch(1)
        self.hboxlayout.addWidget(self.buttonBox, 0)
        self.vboxlayout.addWidget(self.table)
        self.vboxlayout.addLayout(self.hboxlayout)
        self.setLayout(self.vboxlayout)
        self.errorMessageDialog = QtGui.QMessageBox(QtGui.QMessageBox.Warning, u"Информационное сообщение",
                    "", QtGui.QMessageBox.Ok, self)
        self.errorMessageDialog.button(QtGui.QMessageBox.Ok).setText(u"Закрыть")
        self.loadDataToTable()
        self.resize(400, 400)
    def viewErrorMessage(self, message):    
        self.errorMessageDialog.setText(message)
        self.errorMessageDialog.setVisible(True)
    def loadDataToTable(self):
        self.table.clearContents()
        for row in range(self.table.rowCount()):
            for col in range(self.table.columnCount()):
                pitem = QtGui.QTableWidgetItem(str(row) + ":" + str(col))
                self.table.setItem(row, col, pitem)
                self.table.resizeColumnToContents(col)
        pitem = QtGui.QTableWidgetItem("5:6")       #for show multiple selection
        self.table.setItem(6, 5, pitem)
    def clearSelectionInTable(self):
        for selItem in self.table.selectedItems():
            self.table.setItemSelected(selItem, False)
    def findParam(self):
        paramName = str(self.searchParam.text())
        self.clearSelectionInTable()
        for row in range(self.table.rowCount()):
            for col in range(self.table.columnCount()):
                pitem = self.table.item(row, col)
                if pitem:
                    pName = pitem.text()
                    if pName == paramName:
                        self.table.scrollToItem(pitem)
                        self.table.setItemSelected(pitem, True)
                        self.setFocusProxy(self.table)
        if not self.table.selectedItems():
            self.viewErrorMessage("Item not found")
    def accept(self):
        self.done(QtGui.QDialog.Accepted)
        
    def reject(self):
        self.done(QtGui.QDialog.Rejected)
if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    tableD = TableDialog()
    tableD.show()
    sys.exit(app.exec_())
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB