0

I'm creating a plugin in which I use spinboxes. The value set by the user on the spinboxes is meant to define precision in which the result will be printed. Even though my spinbox has different values each time I'm debugging it with print(QSpinBox().value) it always returns 0. I suppose that 0 is a default value to be printed, but I can't get to find any solution to my problem.

I'm a beginner in Python.

This is my code:

class ALCalculatorDialog(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(ALCalculatorDialog, self).__init__(parent)

        self.setupUi(self)
        
        self.stackedWidget.setCurrentWidget(self.home)

        self.pbAreaPage.clicked.connect(self.gotoArea)
        self.pbLengthPage.clicked.connect(self.gotoLength)
        self.pbJumpArea.clicked.connect(self.gotoArea)
        self.pbJumpLength.clicked.connect(self.gotoLength)


        self.cbLengthLayerList.setFilters(QgsMapLayerProxyModel.LineLayer)
        self.cbAreaLayerList.setFilters(QgsMapLayerProxyModel.PolygonLayer)


        self.pbLengthCalc.clicked.connect(self.onPbLengthCalcClicked)
        self.pbAreaCalc.clicked.connect(self.onPbAreaCalcClicked)



    def onPbLengthCalcClicked(self):
        self.sb = QSpinBox()
        precision_value = self.sb.value()
        self.cb = QgsMapLayerComboBox(self)
        layer = self.cb.currentLayer()
        miles_conv = 0.621371192
        yard_conv = 1093.6132983377
        meters_conv = 1000
        if self.rbLengthMeters.isChecked():
            total_length = 0
            for feat in layer.getFeatures():
                geometry = feat.geometry()
                total_length += geometry.length()
            total_length = total_length * meters_conv
            self.lblLengthResult.setText('%.*f' % (precision_value, total_length))
        elif self.rbLengthKilometers.isChecked():
            total_length = 0
            for feat in layer.getFeatures():
                geometry = feat.geometry()
                total_length += geometry.length()
            self.lblLengthResult.setText('%.*f' % (precision_value, total_length))
        elif self.rbLengthMiles.isChecked():
            total_length = 0
            for feat in layer.getFeatures():
                geometry = feat.geometry()
                total_length += geometry.length()
            total_length = total_length * miles_conv
            self.lblLengthResult.setText('%.*f' % (precision_value, total_length))
        elif self.rbLengthYards.isChecked():
            total_length = 0
            for feat in layer.getFeatures():
                geometry = feat.geometry()
                total_length += geometry.length()
            total_length = total_length * yard_conv
            self.lblLengthResult.setText('%.*f' % (precision_value, total_length))


    def onPbAreaCalcClicked(self):
        self.sb = QSpinBox()
        precision_value = self.sb.value()
        self.cb = QgsMapLayerComboBox(self)
        layer = self.cb.currentLayer()
        sqmiles_conv = 0.386102159
        sqyards_conv = 1195990.05
        sqmeters_conv = 1000000
        hectares_conv = 100
        acres_conv = 10000
        if self.rbAreaMeters.isChecked():
            total_area = 0
            for feat in layer.getFeatures():
                geometry = feat.geometry()
                total_area += geometry.area()
            total_area = total_area * sqmeters_conv
            self.lblAreaResult.setText('%.*f' % (precision_value, total_area))
        elif self.rbAreaKilometers.isChecked():
            total_area = 0
            for feat in layer.getFeatures():
                geometry = feat.geometry()
                total_area += geometry.area()
            self.lblAreaResult.setText('%.*f' % (precision_value, total_area))
        elif self.rbAreaAres.isChecked():
            total_area = 0
            for feat in layer.getFeatures():
                geometry = feat.geometry()
                total_area += geometry.area()
                total_area = total_area * acres_conv
            self.lblAreaResult.setText('%.*f' % (precision_value, total_area))
        elif self.rbAreaHectares.isChecked():
            total_area = 0
            for feat in layer.getFeatures():
                geometry = feat.geometry()
                total_area += geometry.area()
                total_area = total_area * hectares_conv
            self.lblAreaResult.setText('%.*f' % (precision_value, total_area))
        elif self.rbAreaMiles.isChecked():
            total_area = 0
            for feat in layer.getFeatures():
                geometry = feat.geometry()
                total_area += geometry.area()
                total_area = total_area * sqmiles_conv
            self.lblAreaResult.setText('%.*f' % (precision_value, total_area))
        elif self.rbAreaYards.isChecked():
            total_area = 0
            for feat in layer.getFeatures():
                geometry = feat.geometry()
                total_area += geometry.area()
                total_area = total_area * sqyards_conv
            self.lblAreaResult.setText('%.*f' % (precision_value, total_area))

        #debug
        print(precision_value)


    def gotoArea(self):
        self.stackedWidget.setCurrentWidget(self.area)
        
    def gotoLength(self):
        self.stackedWidget.setCurrentWidget(self.length)
2
  • 2
    Your problem is that you are creating new instances of QSpinBox (and QgsMapLayerComboBox) classes every time your slot methods are called, when area/ length calc buttons are clicked. Even when you call print(QSpinBox().value()) you are simply printing the default value of a new QSpinBox instance. You should have a spin box and map layer combo boxes in your dialog which you should retrieve and reference via their object names. E.g. I think your combo boxes are self.cbLengthLayerList and self.cbAreaLayerList so reference those objects- don't create new ones! Same with spin box.
    – Ben W
    Feb 21, 2022 at 0:02
  • Oh alright looks like i have lacks of some fundamental knowledge in Python coding. Still can't really make it work (getting can't connect QSpinBox errors), but at least i know what I did wrong. Thanks! Feb 21, 2022 at 18:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.