roam-qgis / Roam

Simple data collection built using QGIS.
http://roam-docs.readthedocs.org/en/latest/
GNU General Public License v2.0
166 stars 60 forks source link

**An update:** I never managed to use QIntValidator nor QRegExpValidator directly on self.boundwidgets['flatenr'] with setValidator as e.g. self.boundwidgets['flatenr'].setValidator(validator). #475

Open PeymanSmaili opened 3 years ago

PeymanSmaili commented 3 years ago

An update: I never managed to use QIntValidator nor QRegExpValidator directly on self.boundwidgets['flatenr'] with setValidator as e.g. self.boundwidgets['flatenr'].setValidator(validator). My "solution" (workaround): I create a new QLineEdit called flatenr and display it directly above the original field (created in the Config Manager) and then I save the value from this QLineEdit as the attribute value for the self.boundwidgets['flatenr']. In addition I added "save last value". With "my own" QLineEdit QIntValidator and QRegExpValidator works. However, I would prefer to be able to use these directly on self.boundwidgets['...']. Is it possible? (It's time consuming placing the QLineEdit directly on the self.boundwidgets['...']....)

My workaround-code:

from roam.api import FeatureForm, RoamEvents, utils
from PyQt4.QtGui import QRegExpValidator, QLineEdit, QDesktopWidget
from PyQt4.QtCore import QRegExp, QSettings

class Form(FeatureForm):
    def __init__(self, *args, **kwargs):
        super(Form, self).__init__(*args, **kwargs)

    def uisetup(self):
        # Getting the screen size
        screen = QDesktopWidget().availableGeometry()  # returns the usable rectangle of the screen
        wSize = screen.width()                         # returns the usable screen witdh
        # Creating a new lineedit-widget performing regexp to ensure 4 digits flatenr (between 1000 and 9999)
        global flatenr
        flatenr = QLineEdit(self)
        # Display the QLineEdit above self.boundwidgets['flatenr'], depends on PC or tablet (with 150 % text size) screen size
        flatenr.setGeometry(169.5,19,wSize-537,42)      #vertical 26 on PC, sets the widt to available screen width
        # Make the QLineEdit frame transparent (the frame of the text widget self.boundwidgets['flatenr'] will be visible)
        flatenr.setStyleSheet("QLineEdit{border: 1px transparent}")
        # Change the frame color if the linedit is selcted
        flatenr.setStyleSheet("QLineEdit{selection-border: 2px solid #5CADFF; selection-border-radius: 2px}")
        # Only allow 4 digits flatenr not starting with a zero
        rx = QRegExp('^[1-9]\d{3}$')
        validator = QRegExpValidator(rx)
        flatenr.setValidator(validator)
        flatenr.textChanged.connect(self.handler1)
...
    def handler1(self,value):
        # Save the value from QLineEdit as the attribute flatenr in the shapefile
        self.boundwidgets['flatenr'].setvalue(flatenr.text())
        # Save value from QLineEdit (just like "action: save last value" in the config manager)
        settings = QSettings("setting.set")
        text = flatenr.text()
        settings.setValue("text", text)
...
    def load(self, feature, layers, values):
        # Restore value of QLineEdit (show the last saved value)
        settings = QSettings("setting.set")
        text = settings.value("text", "")
        flatenr.setText(str(text))
        # Set the value of the attribute flatenr = the value in the QLineEdit
        self.boundwidgets['flatenr'].setvalue(flatenr.text())
        pass

Originally posted by @9ls1 in https://github.com/roam-qgis/Roam/issues/239#issuecomment-117533595