Example #1
0
class Main(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self)
        self.ui = Ui_winMain()
        self.ui.setupUi(self)
        self.itemProto = QTableWidgetItem()
        self.itemProto.setFlags(Qt.NoItemFlags)
        self.ui.tblMain.setItemPrototype(self.itemProto)
        self.ui.tblMain.insertRow(self.ui.tblMain.rowCount() - 1)
        QObject.connect(self.ui.tblMain, SIGNAL("currentCellChanged(int,int,int,int)"), self.cellChanged)

    def is_int(self, s):
        try:
            int(s)
            return True
        except ValueError:
            return False

    def keyPressEvent(self, keyEvent):
        if keyEvent.key() == Qt.Key_Return:
            self.advance()

    def cellChanged(self, row, column, prev_row, prev_column):
        item = self.ui.tblMain.item(row, column)
        if column == 0:
            dlg = SelectParts(self)
            dlg.exec_()
            if not dlg.done:
                self.ui.tblMain.setCurrentCell(row, 0)
                return
            self.ui.tblMain.setItem(row, 0, QTableWidgetItem(dlg.item))
            self.ui.tblMain.setItem(row, 2, QTableWidgetItem(dlg.price))
            self.ui.tblMain.setItem(row, 3, QTableWidgetItem(1))
            self.ui.tblMain.setCurrentCell(row, 3)
        elif column == 3:
            if self.ui.tblMain.item(row, 0) == None:
                self.ui.tblMain.setCurrentCell(row, 0)
                return
            item.setFlags(Qt.ItemIsEditable | Qt.ItemIsEnabled)
            self.ui.tblMain.editItem(item)
        elif column == 4:
            if not self.is_int(self.ui.tblMain.item(row, 3).text()):
                self.ui.tblMain.setItem(row, 3, QTableWidgetItem(1))
                self.ui.tblMain.setCurrentCell(row, 3)
            else:
                total = int(self.ui.tblMain.item(row, 3).text()) * float(self.ui.tblMain.item(row, 2).text()[1:])
                print total
                self.ui.tblMain.setItem(row, 4, QTableWidgetItem("$%1.2f" % total))

    def advance(self):
        current_row = self.ui.tblMain.currentRow()
        current_column = self.ui.tblMain.currentColumn()
        if current_column == 4 and current_row == self.ui.tblMain.rowCount() - 2:
            self.ui.tblMain.insertRow(self.ui.tblMain.rowCount() - 1)
            self.ui.tblMain.setCurrentCell(current_row + 1, 0)
        else:
            self.ui.tblMain.setCurrentCell(current_row, current_column + 1)
Example #2
0
 def __init__(self, parent=None):
     QMainWindow.__init__(self)
     self.ui = Ui_winMain()
     self.ui.setupUi(self)
     self.itemProto = QTableWidgetItem()
     self.itemProto.setFlags(Qt.NoItemFlags)
     self.ui.tblMain.setItemPrototype(self.itemProto)
     self.ui.tblMain.insertRow(self.ui.tblMain.rowCount() - 1)
     QObject.connect(self.ui.tblMain, SIGNAL("currentCellChanged(int,int,int,int)"), self.cellChanged)