Beispiel #1
0
    def __set_next_id(self, record: QSqlRecord):
        if self.__added_items:
            new_id = self.__added_items[-1] + 1
        else:  # list is empty
            new_id = self.__last_id + 1

        record.setValue(0, QVariant(new_id))
        self.__added_items.append(new_id)
        self.__last_id = new_id
        Tools.write_verbose_class_method_name(self, EventManager.__set_next_id,
                                              "new_id", str(new_id))
Beispiel #2
0
    def saveDataOld(self):
        if self.db.isOpen() != True:
            if self.opendb() != True:
                # todo 打开数据库失败错误处理退出
                return
        rowNum = 0
        model = QSqlTableModel()
        model.setEditStrategy(QSqlTableModel.OnManualSubmit)
        while rowNum < self.tableWidget.rowCount():
            year = self.tableWidget.item(rowNum,0)
            profilo = self.tableWidget.item(rowNum,1)
            model.setTable("history")
            strFilter = "unit = '%s' and year = '%s'" %(self.unit,year.text())
            model.setFilter(strFilter)
            model.select()
            if model.rowCount() != 0:
                curRecord = model.record(0)
                curRecord.setValue(self.tableWidget.item(rowNum,1))
                model.setRecord(0,curRecord)
                model.submitAll()
            else:
                curRecord = QSqlRecord()
                curRecord.setValue("year",year.text())
                curRecord.setValue("profilo",profilo.text())
                curRecord.setValue("unit",self.unit)

                ret = model.insertRecord(-1,curRecord)
                if ret == True:
                    print("数据库插入成功")
                else:
                    print("数据插入失败ret = %d" %ret)

                ret = model.submitAll()
                if ret == True:
                    print("提交成功")
                else:
                    error = self.db.lastError()
                    print("提交失败,ret = %d" %ret)
                    # print(error.text())
                    qDebug(error.text())
            rowNum += 1
        self.db.close()
        return True
Beispiel #3
0
class Dialog(QDialog):
    def __init__(self):
        super().__init__()

        self.initdiagUI()

    def initdiagUI(self):

        exitShortcut = QShortcut(QKeySequence('Ctrl+W'), self, qApp.quit)

        #=====#	#=====#	#=====#	#=====#	#=====#	#=====#
        # Create local seperate connection to databse
        #=====#	#=====#	#=====#	#=====#	#=====#	#=====#

        self.db = QSqlDatabase.database("dialog")

        #=====#	#=====#	#=====#	#=====#	#=====#	#=====#
        # Create SqlRecord template with predefined fields
        #=====#	#=====#	#=====#	#=====#	#=====#	#=====#

        self.record_template = QSqlRecord()
        self.record_template.insert(0, QSqlField('model'))
        self.record_template.insert(1, QSqlField('order_quantity'))
        self.record_template.insert(2, QSqlField('order_time'))
        self.record_template.insert(3, QSqlField('expected_production'))

        #=====#	#=====#	#=====#	#=====#	#=====#	#=====#
        # Main widgets
        #=====#	#=====#	#=====#	#=====#	#=====#	#=====#

        label_model = QLabel('Model', self)
        label_model.move(40, 50)

        self.combo = QComboBox(self)
        self.fillcombo()
        self.combo.move(180, 50)

        label_orderquantity = QLabel('Order Quantity', self)
        label_orderquantity.move(40, 100)

        self.oq_input = QLineEdit('', self)
        self.oq_input.move(180, 100)

        self.error_message = QLabel('', self)
        self.error_message.move(180, 130)

        btn_submit = QPushButton('Submit Order', self)
        btn_submit.move(180, 150)
        btn_submit.clicked.connect(self.submittodb)

        self.setFixedSize(350, 200)
        self.setWindowTitle('Input Dialog')
        self.show()
        self.new_record = -1

    #=====#	#=====#	#=====#	#=====#	#=====#	#=====#
    #=====#	#=====#	#=====#	#=====#	#=====#	#=====#

    #							METHODS													#
    #=====#	#=====#	#=====#	#=====#	#=====#	#=====#
    #=====#	#=====#	#=====#	#=====#	#=====#	#=====#

    #=====#	#=====#	#=====#	#=====#	#=====#	#=====#
    #Populate combo box with model names using QSqlQuery and QSqlRecord objects
    #=====#	#=====#	#=====#	#=====#	#=====#	#=====#
    def fillcombo(self):
        q = QSqlQuery("select name from models", self.db)
        self.rec = q.record()
        nameCol = self.rec.indexOf("name")
        print(nameCol)
        while q.next():
            self.combo.addItem(q.value(nameCol))

    #=====#	#=====#	#=====#	#=====#	#=====#	#=====#
    #Submit input data to the model in database
    #=====#	#=====#	#=====#	#=====#	#=====#	#=====#
    def submittodb(self):
        #print ('Inside label: ', self.oq_input.text())
        if self.oq_input.text().isdigit():
            self.new_record = QSqlRecord(self.record_template)
            self.new_record.setValue(0, self.combo.currentText())
            self.new_record.setValue(1, self.oq_input.text())
            self.new_record.setValue(2, str(datetime.datetime.now()))
            self.close()
        else:
            self.error_message.setText('Please enter an integer only')
            self.error_message.adjustSize()
            self.error_message.setStyleSheet("color:red")
Beispiel #4
0
 def _prev_challenge_insert(self, row, record: QSqlRecord):
     record.setValue(record.indexOf('competition_id'), self.idn)
Beispiel #5
0
 def _beforeInsert(self, record: QtSql.QSqlRecord):
     '''增加一行之前给主表键值列赋值'''
     if self.filter():
         fn, fv = self.filter().split('=')
         record.setValue(fn, int(fv))
         record.setGenerated(fn, True)
Beispiel #6
0
class QmyDialogData(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_QWDialogData()
        self.ui.setupUi(self)

        self.__record = QSqlRecord()

    def setInsertRecord(self, recData):
        self.__record = recData
        self.ui.spinEmpNo.setEnabled(True)
        self.setWindowTitle("插入新记录")
        self.ui.spinEmpNo.setValue(recData.value("empNo"))

    def setUpdateRecord(self, recData):
        self.__record = recData
        self.ui.spinEmpNo.setEnabled(False)
        self.setWindowTitle("更新记录")

        self.ui.spinEmpNo.setValue(recData.value("empNo"))
        self.ui.editName.setText(recData.value("Name"))
        self.ui.comboSex.setCurrentText(recData.value("Gender"))

        birth = recData.value("Birthday")
        birth_date = QDate.fromString(birth, "yyyy-MM-dd")
        self.ui.editBirth.setDate(birth_date)

        self.ui.comboProvince.setCurrentText(recData.value("Province"))
        self.ui.comboDep.setCurrentText(recData.value("Department"))
        self.ui.spinSalary.setValue(recData.value("Salary"))
        self.ui.editMemo.setPlainText(recData.value("Memo"))

        picData = recData.value("Photo")
        if (picData == ''):
            self.ui.LabPhoto.clear()
        else:
            pic = QPixmap()
            pic.loadFromData(picData)
            W = self.ui.LabPhoto.size().width()
            self.ui.LabPhoto.setPixmap(pic.scaledToWidth(W))

    def getRecordData(self):
        self.__record.setValue("empNo", self.ui.spinEmpNo.value())
        self.__record.setValue("Name", self.ui.editName.text())
        self.__record.setValue("Gender", self.ui.comboSex.currentText())
        self.__record.setValue("Birthday", self.ui.editBirth.date())
        self.__record.setValue("Province", self.ui.comboProvince.currentText())
        self.__record.setValue("Department", self.ui.comboDep.currentText())
        self.__record.setValue("Salary", self.ui.spinSalary.value())
        self.__record.setValue("Memo", self.ui.editMemo.toPlainText())
        return self.__record

    @pyqtSlot()
    def on_btnSetPhoto_clicked(self):
        fileName, filt = QFileDialog.getOpenFileName(self, "选择图片", "",
                                                     "照片(*.jpg)")
        if (fileName == ''):
            return
        file = QFile(fileName)
        file.open(QIODevice.ReadOnly)
        data = file.readAll()
        file.close()

        self.__record.setValue("Photo", data)
        pic = QPixmap()
        pic.loadFromData(data)
        W = self.ui.LabPhoto.width()
        self.ui.LabPhoto.setPixmap(pic.scaledToWidth(W))

    @pyqtSlot()
    def on_btnClearPhoto_clicked(self):
        self.ui.LabPhoto.clear()
        self.__record.setNull("Photo")
Beispiel #7
0
class QmyDialogData(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)  #调用父类构造函数,创建窗体
        self.ui = Ui_QWDialogData()  #创建UI对象
        self.ui.setupUi(self)  #构造UI界面

        self.__record = QSqlRecord()  #用于存储一条记录的数据

##  ==============自定义功能函数============

    def setInsertRecord(self, recData):  ##设置插入记录的数据
        self.__record = recData
        self.ui.spinEmpNo.setEnabled(True)  #员工编号允许编辑
        self.setWindowTitle("插入新记录")
        self.ui.spinEmpNo.setValue(recData.value("empNo"))

    def setUpdateRecord(self, recData):  ##设置更新记录的数据
        self.__record = recData
        self.ui.spinEmpNo.setEnabled(False)  #员工编号不允许编辑
        self.setWindowTitle("更新记录")

        ##根据recData的数据更新界面显示
        self.ui.spinEmpNo.setValue(recData.value("empNo"))
        self.ui.editName.setText(recData.value("Name"))
        self.ui.comboSex.setCurrentText(recData.value("Gender"))

        birth = recData.value("Birthday")  #str类型, 注意!!!
        birth_date = QDate.fromString(birth, "yyyy-MM-dd")
        self.ui.editBirth.setDate(birth_date)

        self.ui.comboProvince.setCurrentText(recData.value("Province"))
        self.ui.comboDep.setCurrentText(recData.value("Department"))
        self.ui.spinSalary.setValue(recData.value("Salary"))
        self.ui.editMemo.setPlainText(recData.value("Memo"))

        picData = recData.value("Photo")  #bytearray类型
        if (picData == ''):  #图片字段内容为空
            self.ui.LabPhoto.clear()
        else:
            pic = QPixmap()
            pic.loadFromData(picData)
            W = self.ui.LabPhoto.size().width()
            self.ui.LabPhoto.setPixmap(pic.scaledToWidth(W))

    def getRecordData(self):  ##返回界面编辑的数据
        self.__record.setValue("empNo", self.ui.spinEmpNo.value())
        self.__record.setValue("Name", self.ui.editName.text())
        self.__record.setValue("Gender", self.ui.comboSex.currentText())
        self.__record.setValue("Birthday", self.ui.editBirth.date())

        self.__record.setValue("Province", self.ui.comboProvince.currentText())
        self.__record.setValue("Department", self.ui.comboDep.currentText())

        self.__record.setValue("Salary", self.ui.spinSalary.value())
        self.__record.setValue("Memo", self.ui.editMemo.toPlainText())
        ##照片编辑时已经修改了self.__record的photo字段的值

        return self.__record  #以记录作为返回值

##  ==========由connectSlotsByName() 自动连接的槽函数==================

    @pyqtSlot()  ##“导入照片”按钮
    def on_btnSetPhoto_clicked(self):
        fileName, filt = QFileDialog.getOpenFileName(self, "选择图片", "",
                                                     "照片(*.jpg)")
        if (fileName == ''):
            return

        file = QFile(fileName)
        file.open(QIODevice.ReadOnly)
        data = file.readAll()
        file.close()

        self.__record.setValue("Photo", data)  #图片保存到Photo字段

        pic = QPixmap()
        pic.loadFromData(data)  #在界面上显示
        W = self.ui.LabPhoto.width()
        self.ui.LabPhoto.setPixmap(pic.scaledToWidth(W))

    @pyqtSlot()  ##“清除照片按钮”
    def on_btnClearPhoto_clicked(self):
        self.ui.LabPhoto.clear()
        self.__record.setNull("Photo")  #Photo字段清空