[GIS] Using the QgsColorButton Widgets

pyqgis

Platform:
Windows 10
QGis 2.18.14
QT Desiner 4.8.5
PyQT4

I'm creating a custom form using QT Designer to be used in a layer along with a routine in Python.

Just for testing I added a QgsColorButton element on the form.

The idea would be to select the color in the color options box of this button and the selected color would be saved in the attribute table of the layer's feature.

I tried to do a little routine in Python only to check if I could access the properties of the QgsColorButton when the form was opened, but can not even get through this phase.

Any tips to solve this problem?


In the console the code seems to be simple, but I have difficulty understanding with accessing the QgsColorButton widget of the form created in QT Designer.

This would be the form template with multiple QgsColorButton.

enter image description here

I need to access the QgsColorButton with something like the code below:

from qgis.core import *
from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *

def form_open (Dialog, layer_id, feature_id):
     global form
     form = Dialog
     btn = form.findChild (QgsColorButton, "mColorButton")
     btn.setColor (QColor (255, 0, 0, 127))
     layer = iface.activeLayer ()
     provider = layer.dataProvider ()
     feature = layer.selectedFeatures ()
     feature ['Color_01'] = btn.color ()
     layer.startEditing ()
     layer.updateFeature (feature)
     layer.commitChanges ()

The code above seems to be simple, but does not work as you like.

A second part code would be the QgsColorButton load the color saved in the attribute table when the form was opened.

I have no idea how to make this work!


@Francisco Raga, perfect! The only change I need to make is to change the LoadColor method.

def LoadColor ():
    color_value = ToolFeature ["Color"]    
    if not isinstance (color_value, QPyNullVariant):
    mColorButton.setColor(color_value)
    return

But this returned the following error.

Traceback (most recent call last):
   File "", line 1, in
   File "", line 22, in UpdateColorForm
   File "", line 29, in LoadColor
AttributeError: 'QPushButton' object has no attribute 'setColor'

When I enter the command below in the Python console of the QGIS
dir (QgsColorButton) returns all methods of QgsColorButton and the setColor method appears in the list.

I compiled the ui template to check which methods and arguments I want to return to the method

self.mColorButton.setColor (QtGui.QColor (0, 0, 255))

I do not know what can be done, because in the QGIS console the setColor method is recognized and it runs the code perfectly.

I tried to use the code below, but nothing happens. No error message is displayed and the background of the mColorButton button does not change color with the value loaded from the "Color" attribute of the feature.

def LoadColor():
    color_value = ToolFeature["Color"]    
    if not isinstance(color_value, QPyNullVariant):
        style = "QWidget { background-color: %s; }" % color_value
        mColorButton.setAutoFillBackground(True)
        mColorButton.setStyleSheet(style)
        mColorButton.update()
    return

Best Answer

I've done a full documentation for all Python QGIS GUI classes

You can found out illustrated samples for both:

Related Question