PyQt:搜索项目QTableWidget并获取它的坐标?

时间:2012-08-10 08:57:24

标签: python search pyqt qtablewidget

所以,我正在使用 Python PyQt ,我创建了一个QTableWidget,我填充了数据。

有没有办法搜索特定值,如果程序找到了某些内容,是否可以返回该项的coordinated(index)

更新
这是代码:

program2.py:

# -*- coding: utf-8 -*-
#imports:
import os
import platform
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import newchilddlg
from newchilddlg import *
from PyKDE4.kdeui import KDateComboBox
import xlrd
import xlwt


#version:
__version__ = "1.0.0"


#Main Window Class:
class MainWindow(QMainWindow):


    #__init__ function:
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)


        #Window size:
        self.resize(800,600)
        self.centralwidget = QtGui.QWidget(self)
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)


        #Table Widget:
        self.tableWidget = QtGui.QTableWidget(self.centralwidget)
        self.vBar = self.tableWidget.verticalScrollBar()
        self._vBar_lastVal = self.vBar.value()
        self.horizontalLayout.addWidget(self.tableWidget)
        self.vBar.valueChanged.connect(self.scrollbarChanged)
        self.tableWidget.setGridStyle(QtCore.Qt.SolidLine)
        self.tableWidget.setRowCount(100)
        self.tableWidget.setColumnCount(28)
        item = QtGui.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        self.tableWidget.horizontalHeader().setDefaultSectionSize(160)
        self.tableWidget.horizontalHeader().setHighlightSections(True)
        self.tableWidget.horizontalHeader().setStretchLastSection(False)
        self.tableWidget.verticalHeader().setDefaultSectionSize(30)
        self.tableWidget.verticalHeader().setStretchLastSection(False)
        self.horizontalLayout.addWidget(self.tableWidget)
        self.setCentralWidget(self.centralwidget)


        #Window Title:
        self.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget.setSortingEnabled(False)


        #Collumns'names:
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(QtGui.QApplication.translate("MainWindow", "Α.Μ.", None, QtGui.QApplication.UnicodeUTF8))


        #menubar:
        self.menubar = QtGui.QMenuBar(self)
        self.menubar.setEnabled(True)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menuFile = self.menubar.addMenu("&File")
        self.menuFile.setSizeIncrement(QtCore.QSize(0, 0))
        self.menuEdit = self.menubar.addMenu("&Edit")
        self.setMenuBar(self.menubar)
        self.menuFile.setTitle(QtGui.QApplication.translate("MainWindow", "File", None, QtGui.QApplication.UnicodeUTF8))
        self.menuEdit.setTitle(QtGui.QApplication.translate("MainWindow", "Edit", None, QtGui.QApplication.UnicodeUTF8)) 


        #Creating Action filenewchild:
        self.filenewchild = self.createAction("&Εισαγωγή Ανηλίκου", self.doupdate, "Ctrl+C", "" ,"Εισαγωγή Δεδομέων Ανηλίκου")
        self.menuFile.addAction(self.filenewchild)
        self.filenewchild.setText(QtGui.QApplication.translate("MainWindow", "Εισαγωγή Ανηλίκου", None, QtGui.QApplication.UnicodeUTF8))


        #Creating Action Open File:
        self.fileopen = self.createAction("& Άνοιγμα Αρχείου", self.openfile, "Ctrl+O", "", "Εισαγωγή Δεδομένων από Αρχείο")
        self.menuFile.addAction(self.fileopen)
        self.fileopen.setText(QtGui.QApplication.translate("MainWindow", "Άνοιγμα Αρχείου", None, QtGui.QApplication.UnicodeUTF8))


        #Creating Action Save File
        self.filesave = self.createAction("& Άνοιγμα Αρχείου", self.savefile, "Ctrl+S", "", "Σώσιμο Δεδομένων σε Αρχείο")
        self.menuFile.addAction(self.filesave)
        self.filesave.setText(QtGui.QApplication.translate("MainWindow", "Σώσιμο Αρχείου", None, QtGui.QApplication.UnicodeUTF8)) 


    #Creating a function that opens an .xls file:
    def openfile(self):
            try:
                filename = unicode(QtGui.QFileDialog.getOpenFileName(self, 'Open File', '', ".xls(*.xls)"))
                wb = xlrd.open_workbook(filename)
                wb.sheet_names()
                self.sh = wb.sheet_by_index(0)
                if self.sh.nrows > self.tableWidget.rowCount():
                    self.tableWidget.setRowCount(self.sh.nrows)
                self.r = 0
                self.a = 0
                for i in range(self.sh.ncols):
                    self.new = self.sh.col_values(self.a)
                    self.add(self.a)
                    self.a += 1
                    self.r = 0
            except IOError:
                pass


    #Creating a function that converts float to integers and then to strings when necessary:                                 
    def add(self, c):
        for i in self.new:
             try:
                newItem = QtGui.QTableWidgetItem(str(int(i)))
             except ValueError:
                newItem = QtGui.QTableWidgetItem(unicode(i))    
             self.tableWidget.setItem(self.r, c, newItem)
             self.r += 1


    #Creating a function that saves to an .xls file:
    def savefile(self):
        try:
            filename = unicode(QtGui.QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)"))    
            wbk = xlwt.Workbook()
            self.sheet = wbk.add_sheet("sheet", cell_overwrite_ok=True)
            self.add2()
            wbk.save(filename)    
        except IOError:
            pass

    #Creating a function that puts data into place:
    def add2(self):
        row = 0
        col = 0         
        for i in range(self.tableWidget.columnCount()):
            for x in range(self.tableWidget.rowCount()):
                try:             
                    teext = unicode(self.tableWidget.item(row, col).text())
                    self.sheet.write(row, col, teext)
                    row += 1
                except AttributeError:
                    row += 1
            row = 0
            col += 1                                


    # Growing rows while scrolling down....
    def scrollbarChanged(self, val):
        bar = self.vBar
        minVal, maxVal = bar.minimum(), bar.maximum()
        avg = (minVal+maxVal)/2
        rowCount = self.tableWidget.rowCount()
        if val > self._vBar_lastVal and val >= avg:
            self.tableWidget.insertRow(rowCount)
        elif val < self._vBar_lastVal:
            lastRow = rowCount-30
            empty = True
            for col in xrange(self.tableWidget.columnCount()):
                item = self.tableWidget.item(lastRow, col)
                if item and item.text():
                    empty=False
                    break
            if empty:
                self.tableWidget.removeRow(lastRow)
        self._vBar_lastVal = val    


    #A function to help us create Actions faster:
    def createAction(self,text, slot=None, shortcut=None, icon=None,
                     tip=None, checkable=False, signal="triggered()"):
        action = QAction(text, self)
        if icon is not None:
            action.setIcon(QIcon(":/%s.png" % icon))
        if shortcut is not None:
            action.setShortcut(shortcut)
        if tip is not None:
            action.setToolTip(tip)
        if slot is not None:
            self.connect(action, SIGNAL(signal), slot)
        if checkable:
            action.setCheckable(True)
        return action    


    #A function that updates the table:
    def updatetable(self):
        r = self.tableWidget.currentRow()
        c=0
        for i in self.textlist:
            newItem = QtGui.QTableWidgetItem(i)
            self.tableWidget.setItem(r, c, newItem)
            c += 1
        c = 0        


    #Handles NewChildDlg actions:
    def doupdate(self):
        d = NewChildDlg(self)
        if d.exec_():
            text = d.lineEdit.text()
            text2 = d.lineEdit2.text()
            text3 = d.lineEdit3.text()
            text4 = d.lineEdit4.text()
            text5 = d.kdatecombobox.currentText()
            text6 = d.lineEdit5.text()
            text7 = d.combobox.currentText()
            text8 = d.lineEdit6.text()
            text9 = d.lineEdit7.text()
            text10 = d.lineEdit8.text()
            text11 = d.lineEdit9.text()
            text12 = d.lineEdit10.text()
            text13 = d.kdatecombobox2.currentText()
            text14 = d.lineEdit11.text()
            text15 = d.lineEdit12.text()
            text16 = d.kdatecombobox3.currentText()
            text17 = d.lineEdit13.text()
            text18 = d.combobox2.currentText()
            text19 = d.combobox3.currentText()
            text20 = d.textEdit.toPlainText()
            text21 = d.combobox4.currentText()
            text22 = d.combobox5.currentText()
            text23 = d.lineEdit14.text()
            text24 = d.lineEdit15.text()
            text25 = d.kdatecombobox4.currentText()
            text26 = d.combobox6.currentText()
            text27 = d.textEdit2.toPlainText()
            text28 = d.lineEdit16.text()
            self.textlist = [text, text2, text3, text4, text5, text6, text7, text8, text9, text10, text11,
                             text12, text13, text14, text15, text16, text17, text18, text19, text20, text21, text22,
                             text23, text24, text25, text26, text27, text28]
            self.updatetable()


#Main loop:
if __name__ == '__main__':
    app = QApplication(sys.argv)
    myapp = MainWindow()
    myapp.show()
    sys.exit(app.exec_())

newchilddlg.py:

# -*- coding: utf-8 -*-
#imports:
from PyQt4 import QtCore, QtGui
import sys
from PyKDE4.kdeui import KDateComboBox
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from program2 import *
from finddlg import *


#NewChildDialog Class:
class NewChildDlg(QDialog):


    #__init__ function:
    def __init__(self, parent=None):
       super(NewChildDlg, self).__init__(parent)
       self.resize(800, 600)


       #Add to am:
       self.lineEdit = QtGui.QLineEdit(self)
       self.lineEdit.setGeometry(QtCore.QRect(160, 20, 211, 23))

       #Add to namesurname:
       self.lineEdit2 = QtGui.QLineEdit(self)
       self.lineEdit2.setGeometry(QtCore.QRect(160, 50, 211, 23))
       self.lineEdit2.editingFinished.connect(self.dialog)

       #Add to fathersname:
       self.lineEdit3 = QtGui.QLineEdit(self)
       self.lineEdit3.setGeometry(QtCore.QRect(160, 80, 211, 23))

       #Add to mothersname:
       self.lineEdit4 = QtGui.QLineEdit(self)
       self.lineEdit4.setGeometry(QtCore.QRect(160, 110, 211, 23))

       #Add to birthday:
       self.kdatecombobox = KDateComboBox(self)
       self.kdatecombobox.setGeometry(QtCore.QRect(160, 140, 211, 23))

       #Add to placeofbirth:
       self.lineEdit5 = QtGui.QLineEdit(self)
       self.lineEdit5.setGeometry(QtCore.QRect(160, 170, 211, 23))

       #Add to sex:
       self.combobox = QtGui.QComboBox(self)
       self.combobox.setGeometry(QtCore.QRect(160,200, 211, 23))
       self.combobox.addItem("")
       self.combobox.addItem("")

       #Add to nationality:
       self.lineEdit6 = QtGui.QLineEdit(self)
       self.lineEdit6.setGeometry(QtCore.QRect(160, 230, 211, 23))

       #Add to Address:
       self.lineEdit7 = QtGui.QLineEdit(self)
       self.lineEdit7.setGeometry(QtCore.QRect(160, 260, 211, 23))

       #Add to policedepartment:
       self.lineEdit8 = QtGui.QLineEdit(self)
       self.lineEdit8.setGeometry(QtCore.QRect(160, 290, 211, 23))

       #Add to tel:
       self.lineEdit9 = QtGui.QLineEdit(self)
       self.lineEdit9.setGeometry(QtCore.QRect(160, 320, 211, 23))

       #Add to job:
       self.lineEdit10 = QtGui.QLineEdit(self)
       self.lineEdit10.setGeometry(QtCore.QRect(160, 350, 211, 23))

       #Add to courtdate:       
       self.kdatecombobox2 = KDateComboBox(self)
       self.kdatecombobox2.setGeometry(QtCore.QRect(160, 380, 211, 23))

       #Add to board_number:
       self.lineEdit11 = QtGui.QLineEdit(self)
       self.lineEdit11.setGeometry(QtCore.QRect(160, 410, 211, 23))

       #Add to praxis:
       self.lineEdit12 = QtGui.QLineEdit(self)
       self.lineEdit12.setGeometry(QtCore.QRect(160, 440, 211, 23))

       #Add to dayofdoing:
       self.kdatecombobox3 = KDateComboBox(self)
       self.kdatecombobox3.setGeometry(QtCore.QRect(160, 470, 211, 23))

       #Add to placeofdoing:
       self.lineEdit13 = QtGui.QLineEdit(self)
       self.lineEdit13.setGeometry(QtCore.QRect(160, 500, 211, 23))

      #Add to else_existence:
       self.combobox2 = QtGui.QComboBox(self)
       self.combobox2.setGeometry(QtCore.QRect(570, 20, 211, 23))
       self.combobox2.addItem("")
       self.combobox2.addItem("")

       #Add to admission:
       self.combobox3 = QtGui.QComboBox(self)
       self.combobox3.setGeometry(QtCore.QRect(570 ,50, 211, 23))
       self.combobox3.addItem("")
       self.combobox3.addItem("")

       #Add to praxis_more:
       self.textEdit = QtGui.QTextEdit(self)
       self.textEdit.setGeometry(QtCore.QRect(570, 80, 211, 115))

       #Add to appearance:
       self.combobox4 = QtGui.QComboBox(self)
       self.combobox4.setGeometry(QtCore.QRect(570 ,205, 211, 23))
       self.combobox4.addItem("")
       self.combobox4.addItem("")
       self.combobox4.addItem("")

       #Add to postponement:
       self.combobox5 = QtGui.QComboBox(self)
       self.combobox5.setGeometry(QtCore.QRect(570 ,235, 211, 23))
       self.combobox5.addItem("")
       self.combobox5.addItem("")
       self.combobox5.addItem("")

       #Add to decision:
       self.lineEdit14 = QtGui.QLineEdit(self)
       self.lineEdit14.setGeometry(QtCore.QRect(570, 265, 211, 23))

       #Add to decision_number:
       self.lineEdit15 = QtGui.QLineEdit(self)
       self.lineEdit15.setGeometry(QtCore.QRect(570, 295, 211, 23))

       #Add to decision_date:
       self.kdatecombobox4 = KDateComboBox(self)
       self.kdatecombobox4.setGeometry(QtCore.QRect(570, 325, 211, 23))

       #Add to appear:
       self.combobox6 = QtGui.QComboBox(self)
       self.combobox6.setGeometry(QtCore.QRect(570 ,355, 211, 23))
       self.combobox6.addItem("")
       self.combobox6.addItem("")

       #Add to decisions_past:
       self.textEdit2 = QtGui.QTextEdit(self)
       self.textEdit2.setGeometry(QtCore.QRect(570, 385, 211, 115))

       #Add to trustee:
       self.lineEdit16 = QtGui.QLineEdit(self)
       self.lineEdit16.setGeometry(QtCore.QRect(570, 510, 211, 23))

       #Button accept:
       self.pushButton = QtGui.QPushButton(self)
       self.pushButton.setGeometry(QtCore.QRect(160, 540, 211, 40))
       self.pushButton.clicked.connect(self.accept)

       #Button reject:
       self.pushButton2 = QtGui.QPushButton(self)
       self.pushButton2.setGeometry(QtCore.QRect(570, 540, 211, 40))
       self.pushButton2.clicked.connect(self.reject)

       #self.lineEdit:
       self.label = QtGui.QLabel(self)
       self.label.setGeometry(QtCore.QRect(20, 20, 211, 23))

       #self.lineEdit2:
       self.label2 = QtGui.QLabel(self)
       self.label2.setGeometry(QtCore.QRect(20, 50, 211, 23))

       #self.lineEdit3:
       self.label3 = QtGui.QLabel(self)
       self.label3.setGeometry(QtCore.QRect(20, 80, 211, 23))

       #self.lineEdit4:
       self.label4 = QtGui.QLabel(self)
       self.label4.setGeometry(QtCore.QRect(20, 110, 211, 23))

       #self.kdatecombobox:
       self.label5 = QtGui.QLabel(self)
       self.label5.setGeometry(QtCore.QRect(20, 140, 211, 23))

       #self.lineEdit5:
       self.label6 = QtGui.QLabel(self)
       self.label6.setGeometry(QtCore.QRect(20, 170, 211, 23))

       #self.combobox:
       self.label7 = QtGui.QLabel(self)
       self.label7.setGeometry(QtCore.QRect(20, 200, 211, 23))

       #self.lineEdit6:
       self.label8 = QtGui.QLabel(self)
       self.label8.setGeometry(QtCore.QRect(20, 230, 211, 23))

       #self.lineEdit7:
       self.label9 = QtGui.QLabel(self)
       self.label9.setGeometry(QtCore.QRect(20, 260, 211, 23))

       #self.lineEdit8:
       self.label10 = QtGui.QLabel(self)
       self.label10.setGeometry(QtCore.QRect(20, 290, 211, 23))

       #self.lineEdit9:
       self.label11 = QtGui.QLabel(self)
       self.label11.setGeometry(QtCore.QRect(20, 320, 211, 23))

       #self.lineEdit10:
       self.label12 = QtGui.QLabel(self)
       self.label12.setGeometry(QtCore.QRect(20, 350, 211, 23))

       #self.kdatecombobox2:
       self.label13 = QtGui.QLabel(self)
       self.label13.setGeometry(QtCore.QRect(20, 380, 211, 23))

       #self.lineEdit11:
       self.label14 = QtGui.QLabel(self)
       self.label14.setGeometry(QtCore.QRect(20, 410, 211, 23))

       #self.lineEdit12:
       self.label15 = QtGui.QLabel(self)
       self.label15.setGeometry(QtCore.QRect(20, 440, 211, 23))

       #self.kdatekombobox3:
       self.label16 = QtGui.QLabel(self)
       self.label16.setGeometry(QtCore.QRect(20, 470, 211, 23))

       #self.lineEdit13:
       self.label17 = QtGui.QLabel(self)
       self.label17.setGeometry(QtCore.QRect(20, 500, 211, 23))

       #self.combobox2:
       self.label18 = QtGui.QLabel(self)
       self.label18.setGeometry(QtCore.QRect(420, 20, 211, 23))

       #self.combobox3:
       self.label19 = QtGui.QLabel(self)
       self.label19.setGeometry(QtCore.QRect(420, 50, 211, 23))

       #self.textEdit:
       self.label20 = QtGui.QLabel(self)
       self.label20.setGeometry(QtCore.QRect(420, 120, 211, 23))

       #self.combobox4:
       self.label21 = QtGui.QLabel(self)
       self.label21.setGeometry(QtCore.QRect(420, 205, 211, 23))

       #self.combobox5:
       self.label22 = QtGui.QLabel(self)
       self.label22.setGeometry(QtCore.QRect(420, 235, 211, 23))

       #self.lineEdit14:
       self.label23 = QtGui.QLabel(self)
       self.label23.setGeometry(QtCore.QRect(420, 265, 211, 23))

       #self.lineEdit15:
       self.label24 = QtGui.QLabel(self)
       self.label24.setGeometry(QtCore.QRect(420, 295, 211, 23))

       #self.kdatecombobox4:
       self.label25 = QtGui.QLabel(self)
       self.label25.setGeometry(QtCore.QRect(420, 325, 211, 23))

       #self.combobox6:
       self.label26 = QtGui.QLabel(self)
       self.label26.setGeometry(QtCore.QRect(420, 355, 211, 23))

       #self.textEdit2:
       self.label27 = QtGui.QLabel(self)
       self.label27.setGeometry(QtCore.QRect(420, 425, 211, 23))

       #self.lineEdit16:
       self.label28 = QtGui.QLabel(self)
       self.label28.setGeometry(QtCore.QRect(420, 510, 211, 23))


       self.setWindowTitle(QtGui.QApplication.translate("Form", "Εισαγωγή Ανηλίκου", None, QtGui.QApplication.UnicodeUTF8))
       self.label.setText(QtGui.QApplication.translate("Form", "Α.Μ.:", None, QtGui.QApplication.UnicodeUTF8))
       self.label2.setText(QtGui.QApplication.translate("Form", "Ονοματεπώνυμο:", None, QtGui.QApplication.UnicodeUTF8))
       self.label3.setText(QtGui.QApplication.translate("Form", "Όνομα Πατρός:", None, QtGui.QApplication.UnicodeUTF8))
       self.label4.setText(QtGui.QApplication.translate("Form", "Όνομα Μητρός:", None, QtGui.QApplication.UnicodeUTF8))
       self.label5.setText(QtGui.QApplication.translate("Form", "Ημερομηνία Γέννησης:", None, QtGui.QApplication.UnicodeUTF8))
       self.label6.setText(QtGui.QApplication.translate("Form", "Τόπος Γέννησης:", None, QtGui.QApplication.UnicodeUTF8))
       self.label7.setText(QtGui.QApplication.translate("Form", "Φύλο:", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox.setItemText(0, QtGui.QApplication.translate("Form", "Αγόρι", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox.setItemText(1, QtGui.QApplication.translate("Form", "Κορίτσι", None, QtGui.QApplication.UnicodeUTF8))
       self.label8.setText(QtGui.QApplication.translate("Form", "Εθνικότητα:", None, QtGui.QApplication.UnicodeUTF8))
       self.label9.setText(QtGui.QApplication.translate("Form", "Διεύθυνση Κατοικίας:", None, QtGui.QApplication.UnicodeUTF8))
       self.label10.setText(QtGui.QApplication.translate("Form", "Αστυνομικό Τμήμα:", None, QtGui.QApplication.UnicodeUTF8))
       self.label11.setText(QtGui.QApplication.translate("Form", "Τηλέφωνο:", None, QtGui.QApplication.UnicodeUTF8))
       self.label12.setText(QtGui.QApplication.translate("Form", "Επάγγελμα-Ιδιότητα:", None, QtGui.QApplication.UnicodeUTF8))
       self.label13.setText(QtGui.QApplication.translate("Form", "Ημερομηνία Δικασίμου:", None, QtGui.QApplication.UnicodeUTF8))
       self.label14.setText(QtGui.QApplication.translate("Form", "Αριθμός Πινακίου:", None, QtGui.QApplication.UnicodeUTF8))
       self.label15.setText(QtGui.QApplication.translate("Form", "Πράξη:", None, QtGui.QApplication.UnicodeUTF8))
       self.label16.setText(QtGui.QApplication.translate("Form", "Ημερομηνία Τέλεσης:", None, QtGui.QApplication.UnicodeUTF8))
       self.label17.setText(QtGui.QApplication.translate("Form", "Τόπος Τέλεσης:", None, QtGui.QApplication.UnicodeUTF8))
       self.label18.setText(QtGui.QApplication.translate("Form", "Ύπαρξη Συνενόχων:", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox2.setItemText(0, QtGui.QApplication.translate("Form", "Ναι", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox2.setItemText(1, QtGui.QApplication.translate("Form", "Όχι", None, QtGui.QApplication.UnicodeUTF8))
       self.label19.setText(QtGui.QApplication.translate("Form", "Παραδοχή:", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox3.setItemText(0, QtGui.QApplication.translate("Form", "Ναι", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox3.setItemText(1, QtGui.QApplication.translate("Form", "Όχι", None, QtGui.QApplication.UnicodeUTF8))
       self.label20.setText(QtGui.QApplication.translate("Form", "Περιγραφή Πράξης:", None, QtGui.QApplication.UnicodeUTF8))
       self.label21.setText(QtGui.QApplication.translate("Form", "Εμφάνιση:", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox4.setItemText(0, QtGui.QApplication.translate("Form", "Πρωτοεμφανιζόμενος", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox4.setItemText(1, QtGui.QApplication.translate("Form", "Υπότροπος", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox4.setItemText(2, QtGui.QApplication.translate("Form", "Αναβολή", None, QtGui.QApplication.UnicodeUTF8))
       self.label22.setText(QtGui.QApplication.translate("Form", "Αναβολή:", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox5.setItemText(0, QtGui.QApplication.translate("Form", "Φετινή", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox5.setItemText(1, QtGui.QApplication.translate("Form", "Περισυνή", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox5.setItemText(2, QtGui.QApplication.translate("Form", "---", None, QtGui.QApplication.UnicodeUTF8))
       self.label23.setText(QtGui.QApplication.translate("Form", "Απόφαση:", None, QtGui.QApplication.UnicodeUTF8))
       self.label24.setText(QtGui.QApplication.translate("Form", "Αριθμός Απόφασης:", None, QtGui.QApplication.UnicodeUTF8))
       self.label25.setText(QtGui.QApplication.translate("Form", "Ημερομηνία Απόφασης:", None, QtGui.QApplication.UnicodeUTF8))
       self.label26.setText(QtGui.QApplication.translate("Form", "Παρουσία:", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox6.setItemText(0, QtGui.QApplication.translate("Form", "Παρόν", None, QtGui.QApplication.UnicodeUTF8))
       self.combobox6.setItemText(1, QtGui.QApplication.translate("Form", "Απόν", None, QtGui.QApplication.UnicodeUTF8))
       self.label27.setText(QtGui.QApplication.translate("Form", "Προηγούμενες Αποφάσεις:", None, QtGui.QApplication.UnicodeUTF8))
       self.label28.setText(QtGui.QApplication.translate("Form", "Υπεύθυνος Επιμελητής:", None, QtGui.QApplication.UnicodeUTF8))
       self.pushButton.setText(QtGui.QApplication.translate("Form", "Εισαγωγή", None, QtGui.QApplication.UnicodeUTF8))
       self.pushButton2.setText(QtGui.QApplication.translate("Form", "Άκυρωση", None, QtGui.QApplication.UnicodeUTF8))

    def dialog(self):
       self.texta = str(self.lineEdit2.text())
       main = MainWindow(self)
       item = main.tableWidget.findItems(self.texta, QtCore.Qt.MatchExactly)
       if len(item) > 0:
           f = ChildFound(self)
           f.exec_()


#Main loop:        
def main():
    dialog = NewChildDlg()
    dialog.show()
    dialog.exec_()
你能看到任何错误吗?对不起,但我不确定哪个部分有问题...... 我已经削减了一些不重要的部分......

2 个答案:

答案 0 :(得分:2)

如果您在QTableWidget中使用QTableWidgetItems,那么您可以使用它:

for row in xrange(table.rowCount ()):
   for column in xrange(table.columnCount ()):
       item = table.item (row, column )
       if item and item.data (Qt.DisplayRole) == "my_search_value":
           return table.indexFromItem (item)
 return None

答案 1 :(得分:2)

使用QTableWidget.findItems获取匹配QTableWidgetItems的列表,然后可以查询其坐标。

这是一个简单的演示脚本:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self, rows, columns):
        QtGui.QWidget.__init__(self)
        self.table = QtGui.QTableWidget(self)
        self.table.setRowCount(rows)
        self.table.setColumnCount(columns)
        for column in range(columns):
            for row in range(rows):
                item = QtGui.QTableWidgetItem('Text%d' % row)
                self.table.setItem(row, column, item)
        self.edit = QtGui.QLineEdit(self)
        self.button = QtGui.QPushButton('Search', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.table)
        layout.addWidget(self.edit)
        layout.addWidget(self.button)

    def handleButton(self):
        items = self.table.findItems(
            self.edit.text(), QtCore.Qt.MatchExactly)
        if items:
            results = '\n'.join(
                'row %d column %d' % (item.row() + 1, item.column() + 1)
                for item in items)
        else:
            results = 'Found Nothing'
        QtGui.QMessageBox.information(self, 'Search Results', results)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window(6, 3)
    window.resize(350, 300)
    window.show()
    sys.exit(app.exec_())