Custom widget and stylesheet



  • Well, I'm developing a custom widget. And I want some custom painting, so I reimplement paintEvent() in the class derived from QWidget.

    Now to the point: I want to make my widget somewhat styleable through stylesheets. And the simple question is "Are there means of getting stylesheet properties for my widget in a convenient fashion?" Something like making a QPalette out of stylesheet, or something like that. I have used google and still by now it seems that the only possible way for me is to parse stylesheet by hand, which is nasty.

    An example:
    @
    void KGraph::paintEvent(QPaintEvent )
    {
    QPainter p(this);
    p.setPen(/
    here the pen from KGraph-relevant stylesheet */);
    p.drawLine(0, 0, 10, 10);
    }
    @



  • Well, the actual solution is: there is nothing special to do. Prior to paintEvent the widgets palette gets set according to stylesheet and that is it. Just do it like that:

    @
    void KGraph::paintEvent(QPaintEvent *)
    {
    QPainter p(this);
    p.setPen(palette().color(QPalette::WindowText)); // replace QPalette::WindowText with whatever role is needed
    p.drawLine(0, 0, 100, 100);
    }
    @

    The actual reason for this post was that setting border in stylesheet didn't add one to my custom widget. Unfortunatly qwidgets do not have borders by themselvs, so that stylesheet did nothing.



  • Hi,

    I think it can be also usefull for someone:

    • add new property to widget and then simply use it in stylesheets. Then you can simply use these properties when i.e. painting.

    @
    #ifndef SWIDGET_H
    #define SWIDGET_H

    #include <QWidget>

    class SWidget : public QWidget
    {
    Q_OBJECT
    Q_PROPERTY(QColor customColor READ getCustomColor WRITE setCustomColor DESIGNABLE true)

    public:
    explicit SWidget(QWidget *parent = 0);
    ~SWidget();

    QColor getCustomColor();
    void setCustomColor( QColor pen );
    

    protected:
    void paintEvent( QPaintEvent *e );

    private:
    QColor customColor;
    };

    #endif // SWIDGET_H
    @

    @
    #include "swidget.h"

    #include <QPainter>
    #include <QDebug>

    SWidget::SWidget(QWidget *parent) :
    QWidget(parent)
    {
    }

    SWidget::~SWidget()
    {
    delete ui;
    }

    void SWidget::paintEvent( QPaintEvent *e )
    {
    QPainter p(this);
    p.setPen( getCustomColor() );
    p.drawLine( 0,0, size().width(), size().height() );
    }

    QColor SWidget::getCustomColor()
    {
    return customColor;
    }

    void SWidget::setCustomColor( QColor pen )
    {
    qDebug() << PRETTY_FUNCTION;
    customColor = pen;
    }
    @

    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include "swidget.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    SWidget *w = new SWidget();
    w->setStyleSheet( "qproperty-customColor: red;" );
    
    w->show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    Of course this property can be set via .qss as welll, but this is just a quick example.



  • Thanks a lot, it is a really brilliant idea!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.