2014-09-05 80 views
1

我將我的PyQt代碼與PostgreSQL連接起來,並且在PostgreSQL中我已經把某些列是NUMERIC(11,0)類型,但是現在,因爲NUMERIC(11,0)意味着最多11位數字,所以我想將它限制爲11位數字。PyQt,如何檢查數組的數組是否具有特定長度

class kirurg_GUI(QtGui.QWidget, Ui_kirurg_Widget): 
     def __init__(self): 
       QtGui.QWidget.__init__(self) 
       self.setupUi(self) 
       self.kirurg_Add_button.clicked.connect(self.insert_kirurg) 

     def insert_kirurg(self): 
       oib_kir = self.kirurg_oib_kir_lineEdit.text() 
       if not oib_kir: 
        QtGui.QMessageBox.critical(self, 'Query error', 'oib_kir must not be empty') 

       else: 
        query = QSqlQuery() 
        status = query.exec("INSERT INTO kirurg (oib_kir, prezime, ime, adresa, broj_telefona)" 
             "VALUES ('%s', '%s', '%s', '%s', '%s')" % (''.join(self.kirurg_oib_kir_lineEdit.text()), 
                        ''.join(self.kirurg_prezime_lineEdit.text()), 
                        ''.join(self.kirurg_ime_lineEdit.text()), 
                        ''.join(self.kirurg_adresa_lineEdit.text()), 
                        ''.join(self.kirurg_broj_telefona_lineEdit.text()))) 
        if status is not True: 
          errorText = query.lastError().text() 
          QtGui.QMessageBox.critical(self, 'Query error', errorText) 

        else: 
          msgBox = QtGui.QMessageBox() 
          msgBox.setWindowTitle("Kirurg") 
          msgBox.setText("Data entered successfully") 
          msgBox.exec_() 
          kirurg.close() 

這是我的代碼的一部分,我在我的數據庫中添加值。

這條線:

''.join(self.kirurg_oib_kir_lineEdit.text() 

應該是完全11位長。我已經涵蓋了最大尺寸和數字類型,但現在我想將其限制爲精確的位數。

謝謝!

回答

2

您只能使用QLineEdit.setInputMask (self, QString inputMask)設置數字。閱讀here來配置它們。

class kirurg_GUI (QtGui.QWidget, Ui_kirurg_Widget): 
    def __init__ (self): 
     QtGui.QWidget.__init__(self) 
     self.setupUi(self) 
     self.kirurg_Add_button.clicked.connect(self.insert_kirurg) 
     maxLength = 11              # <- Add length 
     self.kirurg_oib_kir_lineEdit.setInputMask(('9' * maxLength) + ';_') # <- Set input mask 
     self.kirurg_oib_kir_lineEdit.setMaxLength(maxLength)    # <- Set max length 

    def insert_kirurg (self): 
     oib_kir = self.kirurg_oib_kir_lineEdit.text() 
     if not oib_kir: 
      QtGui.QMessageBox.critical(self, 'Query error', 'oib_kir must not be empty') 
     if len(str(oib_kir)) != self.kirurg_oib_kir_lineEdit.maxLength():     # Check digit length 
      QtGui.QMessageBox.critical(self, 'Query error', 'oib_kir must not be 11 diget') # Not 11 digit ? Error! 
     else: 
      query = QSqlQuery() 
      status = query.exec("INSERT INTO kirurg (oib_kir, prezime, ime, adresa, broj_telefona)" 
           "VALUES ('%s', '%s', '%s', '%s', '%s')" % (''.join(self.kirurg_oib_kir_lineEdit.text()), 
                      ''.join(self.kirurg_prezime_lineEdit.text()), 
                      ''.join(self.kirurg_ime_lineEdit.text()), 
                      ''.join(self.kirurg_adresa_lineEdit.text()), 
                      ''.join(self.kirurg_broj_telefona_lineEdit.text()))) 
      if status is not True: 
       errorText = query.lastError().text() 
       QtGui.QMessageBox.critical(self, 'Query error', errorText) 
      else: 
       msgBox = QtGui.QMessageBox() 
       msgBox.setWindowTitle("Kirurg") 
       msgBox.setText("Data entered successfully") 
       msgBox.exec_() 
       kirurg.close() 
+1

謝謝,它的工作! – Luka 2014-09-06 09:10:51

1

不能使用maxLength屬性?

self.kirurg_oib_kir_lineEdit.setMaxLength(11); 

或者您可以嘗試添加一個像這樣的事件過濾器。也許我錯了

class Filter(QtCore.QObject): 
    def eventFilter(self, widget, event): 
     if event.type() == QtCore.QEvent.FocusOut: 
      getTxt = widget.text() 
      if len(getTxt) < 11 or len(getTxt) > 11: 
       reply = QtGui.QMessageBox.critical(None, "Critical", 
        'Value must be 11 numbers', QtGui.QMessageBox.Ok) 
      return False 
     else: 
      return False 

class LimitTest(QtGui.QWidget): 
    def __init__(self, parent=None, total=20): 
     super(LimitTest, self).__init__(parent) 
     self._filter = Filter() 
     self.newLimitTextEdit = QtGui.QLineEdit() 
     self.normal = QtGui.QLineEdit() 
     main_layout = QtGui.QGridLayout() 
     main_layout.addWidget(self.newLimitTextEdit, 0, 0) 
     main_layout.addWidget(self.normal, 0, 1) 
     self.setLayout(main_layout) 
     self.setWindowTitle('Test') 
     self._active = False 
     self.newLimitTextEdit.installEventFilter(self._filter) 


app = QtGui.QApplication(sys.argv) 
bar = LimitTest(total=101) 
bar.show() 
sys.exit(app.exec_()) 
+0

難道這不會限制它的最大長度嗎?我已經有這個工作。我需要它完全是11,而不是9,而不是10,但是11. – Luka 2014-09-05 19:45:17

+0

更新了一個事件過濾器,可能會幫助你。 – Achayan 2014-09-05 20:11:47

相關問題