Search code examples
qtstylesheetsignals-slots

Resetting Qt Style Sheet


I've managed to style my QLineEdit to something like this:

alt text http://www.kimag.es/share/54278758.png

void Utilities::setFormErrorStyle(QLineEdit *lineEdit)
{
    lineEdit->setStyleSheet(
            "background-color: #FF8A8A;"
            "background-image: url(:/resources/warning.png);"
            "background-position: right center;"
            "background-repeat: no-repeat;"
            "");
}

I called the function using

Utilities *util = new Utilities;
util->setFormErrorStyle(lineNoStaf);

The flow should be something like this:

  1. User open form
  2. User fill data
  3. User submit data
  4. Got error
  5. Use setFormErrorStyle()
  6. User edit the text in the QLineEdit and the style disappear

This function should be reusable over and over again, but how can I connect QLineEdit signal such as textChanged() to a function in other class that will reset the Style Sheet and then disconnect the signal so that it won't be running continuously every time the text changed ?


Solution

  • Allright, this is not compile but should work in principle, you should be able to change the look by calling editWidget->setProperty('isError',true) or editWidget->setError(false)

    class ErrorTextEdit : QLineEdit
    {
      Q_OBJECT
      QPROPERTY(bool isError, READ isError, WRITE setError);
    
    public:
      ErrorTextEdit(QWidget* parent) : QLineEdit(parent), m_isError(false)
      {
        m_styleSheet = "" // see below
        setStyleSheet(m_styleSheet);
      }
    
      void setError(bool val)
      {
         if (val != m_isError)
         {
           m_isError = val;
           setStyleSheet(m_styleSheet);
         }
      }
    
      bool isError() {return m_isError;}
    
    private:
      QString m_styleSheet;
      bool m_isError;
    }
    

    for the stylesheet

    ErrorTextEdit[isError="false"]
    {
       optional ...
       Style for textedit that is NOT an error
    }
    
    ErrorTextEdit[isError="true"]
    {
       background-color: #FF8A8A;
       background-image: url(:/resources/warning.png);
       background-position: right center;
       background-repeat: no-repeat;
    }
    

    the term

    [<property>="<value>"]
    

    restricts the application of the stylesheet to instances of the class whose <property> has the appropriate <value> the only caveat is that the style is not changed when the property changes its' value, so the stylesheet has to be reapplied for the look of the widget to actually change, see Stylesheet Documentation -> Property Selector

    This construction moves the stylesheet into the widget that uses it and makes switch internal to the widget, the widget changes in accordance to its state.

    In general you have a couple of ways to handle invalid inputs in your form

    a) observe every change and update the style appropriately, you should be able to use QValidator for that too, but that is a separate topic, using QValidator you will probably be able to completely internalize the state of a single QTextEdit and not have to deal with its validity from the outside

    b) Do it in the submit loop that you have described above, whenever the user clicks on submit change the state of the correct and incorrect fields

    it all depends the structure of your app and the view