Qt Style Sheets and Custom Painting Example: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Qt Style Sheets and Custom Painting Example=
[[Category:snippets]] [toc align_right="yes" depth="2"]


==Introduction==
= Qt Style Sheets and Custom Painting Example =


This example demonstrates:
== Introduction ==


# use of qt style sheets
This example demonstrates:<br /># use of qt style sheets<br /># use of Q_PROPERTY<br /># how to combine 1 and 2 with custom painting
# use of Q_PROPERTY
# how to combine 1 and 2 with custom painting


In this example we have some custom widget that has reimplemented paintEvent function and inside, some custom painting is performed. We’ll see how to set custom colors for for shapes and lines via Qt Style Sheet mechanism. Thanks to it, we can easily change the colors inside the .qss file without recompiling the program.
In this example we have some custom widget that has reimplemented paintEvent function and inside, some custom painting is performed. We'll see how to set custom colors for for shapes and lines via Qt Style Sheet mechanism. Thanks to it, we can easily change the colors inside the .qss file without recompiling the program.


==Example Source code==
== Example Source code ==


'''swidget.h'''<br />'''swidget.cpp'''<br />'''main.cpp'''<br />'''sheet.qss'''<br />
'''swidget.h'''<br /><code><br />#ifndef SWIDGET_H<br />#define SWIDGET_H


==Code Walkthrough==
#include &lt;QWidget&amp;gt;


We need to define our custom widget and assign new properties for further. In this case we need two custom color properties. One for line color and one for rectangle color.<br />
class SWidget : public QWidget<br />{<br /> Q_OBJECT<br /> Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor DESIGNABLE true)<br /> Q_PROPERTY(QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true)


Methods required by new properties:<br />
public:<br /> explicit SWidget(QWidget *parent = 0);<br /> ~SWidget();


We’ll reimplement the paintEvent<br />
QColor getLineColor() const;<br /> void setLineColor( QColor c );


Private members will hold our Q_PROPERTY values<br />
QColor getRectColor() const;<br /> void setRectColor( QColor c );


swidget.cpp<br /> Paint event reimplementation. Drawing of line that goes across the widget and rectangle is performed in paintEvent for simplicty.<br />
protected:<br /> void paintEvent( QPaintEvent *e );


Define the setters and getters for lineColor and rectColor properties.<br /><span class="caps">NOTE</span>: we’re using qDebug() to make sure that our setters are being called in further widget restyling.<br />
private:<br /> QColor lineColor;<br /> QColor rectColor;<br />};


Let’s go into main.cpp now.<br /> We’ll create very simple gui app that will show our custom widget:<br />
#endif // SWIDGET_H<br /></code><br />'''swidget.cpp'''<br /><code><br />#include &quot;swidget.h&amp;quot;


Open the sheet.qss file that holds qproperties for this application. If open is succeedded get data in QString '''ssheet''' and apply stylle sheet to QApplication object.<br />
#include &lt;QPainter&amp;gt;<br />#include &lt;QDebug&amp;gt;


Show widget and enter main event loop.<br />
SWidget::SWidget(QWidget *parent) :<br /> QWidget(parent)<br />{<br />}


Inside sheet.qss we’re defining line and rectangle color for SWidget<br />
SWidget::~SWidget()<br />{<br />}


Please note that all SWidget widget we’ll be styled in the same way.
void SWidget::paintEvent( QPaintEvent *e )<br />{<br /> QPainter p(this);<br /> p.setPen( getLineColor() );<br /> p.drawLine( 0,0, size().width(), size().height() );<br /> p.setPen( getRectColor() );<br /> p.drawRect( 5,5, size().width()–10, size().height()<s>10 );<br />}
<br />QColor SWidget::getLineColor() const<br />{<br /> return lineColor;<br />}
<br />void SWidget::setLineColor( QColor c )<br />{<br /> qDebug() &lt;&lt; ''FUNCTION'';<br /> lineColor = c;<br />}
<br />QColor SWidget::getRectColor() const<br />{<br /> return rectColor;<br />}
<br />void SWidget::setRectColor( QColor c )<br />{<br /> qDebug() &lt;&lt; ''FUNCTION'';<br /> rectColor = c;<br />}<br /></code><br />'''main.cpp'''<br /><code><br />#include &lt;QtGui/QApplication&amp;gt;<br />#include &quot;mainwindow.h&amp;quot;<br />#include &quot;swidget.h&amp;quot;
<br />int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);
<br /> //create widget<br /> SWidget *w = new SWidget();
<br /> //open and set style sheet<br /> QFile f( &quot;sheet.qss&amp;quot; );<br /> if( f.open( QFile::ReadOnly ) )<br /> {<br /> QString ssheet = QLatin1String( f.readAll() );<br /> a.setStyleSheet( ssheet );<br /> }
<br /> w</s>&gt;show();


In Case that we have:<br /> and want custom look of each of them, we need to add names for them:<br />
return a.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code><br />'''sheet.qss'''<br /><code><br />SWidget<br />{<br /> qproperty-lineColor: yellow;<br /> qproperty-rectColor: red;<br />}<br /></code>


Noe, sheet.qss may look like this:<br />
== Code Walkthrough ==


Now we can just modify the qss file to change appearance of our custom widget.
We need to define our custom widget and assign new properties for further. In this case we need two custom color properties. One for line color and one for rectangle color.<br /><code><br />class SWidget : public QWidget<br />{<br /> Q_OBJECT<br /> Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor DESIGNABLE true)<br /> Q_PROPERTY(QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true)<br /></code>


===Categories:===
Methods required by new properties:<br /><code><br /> QColor getLineColor() const;<br /> void setLineColor( QColor c );


* [[:Category:snippets|snippets]]
QColor getRectColor() const;<br /> void setRectColor( QColor c );<br /></code>
 
We'll reimplement the paintEvent<br /><code><br />protected:<br /> void paintEvent( QPaintEvent *e );<br /></code>
 
Private members will hold our Q_PROPERTY values<br /><code><br />private:<br /> QColor lineColor;<br /> QColor rectColor;<br /></code>
 
swidget.cpp<br />Paint event reimplementation. Drawing of line that goes across the widget and rectangle is performed in paintEvent for simplicty.<br /><code><br />void SWidget::paintEvent( QPaintEvent *e )<br />{<br /> QPainter p(this);<br /> p.setPen( getLineColor() );<br /> p.drawLine( 0,0, size().width(), size().height() );<br /> p.setPen( getRectColor() );<br /> p.drawRect( 5,5, size().width()–10, size().height()<s>10 );<br />}<br /></code>
<br />Define the setters and getters for lineColor and rectColor properties.<br />NOTE: we're using qDebug() to make sure that our setters are being called in further widget restyling.<br /><code><br />QColor SWidget::getLineColor() const<br />{<br /> return lineColor;<br />}
<br />void SWidget::setLineColor( QColor c )<br />{<br /> qDebug() &lt;&lt; ''FUNCTION'';<br /> lineColor = c;<br />}
<br />QColor SWidget::getRectColor() const<br />{<br /> return rectColor;<br />}
<br />void SWidget::setRectColor( QColor c )<br />{<br /> qDebug() &lt;&lt; ''FUNCTION'';<br /> rectColor = c;<br />}<br /></code>
<br />Let's go into main.cpp now.<br />We'll create very simple gui app that will show our custom widget:<br /><code><br />int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);
<br /> //create widget<br /> SWidget *w = new SWidget();<br /></code>
<br />Open the sheet.qss file that holds qproperties for this application. If open is succeedded get data in QString '''ssheet''' and apply stylle sheet to QApplication object.<br /><code><br /> QFile f( &quot;sheet.qss&amp;quot; );<br /> if( f.open( QFile::ReadOnly ) )<br /> {<br /> QString ssheet = QLatin1String( f.readAll() );<br /> a.setStyleSheet( ssheet );<br /> }<br /></code>
<br />Show widget and enter main event loop.<br /><code><br /> w</s>&gt;show();
 
return a.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
 
Inside sheet.qss we're defining line and rectangle color for SWidget<br /><code><br />SWidget<br />{<br /> qproperty-lineColor: yellow;<br /> qproperty-rectColor: red;<br />}<br /></code>
 
Please note that all SWidget widget we'll be styled in the same way.
 
In Case that we have:<br /><code><br />SWidget *w = new SWidget();<br />SWidget *w2 = new SWidget();<br /></code><br />and want custom look of each of them, we need to add names for them:<br /><code><br />w-&gt;setObjectName(&quot;w1&amp;quot;);<br />w2-&gt;setObjectName(&quot;w2);<br /></code>
 
Noe, sheet.qss may look like this:<br /><code><br />SWidget#w1<br />{<br /> qproperty-lineColor: yellow;<br /> qproperty-rectColor: red;<br />}<br />SWidget#w2<br />{<br /> qproperty-lineColor: blue;<br /> qproperty-rectColor: white;<br />}<br /></code>

Revision as of 09:32, 24 February 2015

[toc align_right="yes&quot; depth="2&quot;]

Qt Style Sheets and Custom Painting Example

Introduction

This example demonstrates:
# use of qt style sheets
# use of Q_PROPERTY
# how to combine 1 and 2 with custom painting

In this example we have some custom widget that has reimplemented paintEvent function and inside, some custom painting is performed. We'll see how to set custom colors for for shapes and lines via Qt Style Sheet mechanism. Thanks to it, we can easily change the colors inside the .qss file without recompiling the program.

Example Source code

swidget.h

<br />#ifndef SWIDGET_H<br />#define SWIDGET_H

#include &lt;QWidget&amp;gt;

class SWidget : public QWidget<br />{<br /> Q_OBJECT<br /> Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor DESIGNABLE true)<br /> Q_PROPERTY(QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true)

public:<br /> explicit SWidget(QWidget *parent = 0);<br /> ~SWidget();

QColor getLineColor() const;<br /> void setLineColor( QColor c );

QColor getRectColor() const;<br /> void setRectColor( QColor c );

protected:<br /> void paintEvent( QPaintEvent *e );

private:<br /> QColor lineColor;<br /> QColor rectColor;<br />};

#endif // SWIDGET_H<br />


swidget.cpp

<br />#include &quot;swidget.h&amp;quot;

#include &lt;QPainter&amp;gt;<br />#include &lt;QDebug&amp;gt;

SWidget::SWidget(QWidget *parent) :<br /> QWidget(parent)<br />{<br />}

SWidget::~SWidget()<br />{<br />}

void SWidget::paintEvent( QPaintEvent *e )<br />{<br /> QPainter p(this);<br /> p.setPen( getLineColor() );<br /> p.drawLine( 0,0, size().width(), size().height() );<br /> p.setPen( getRectColor() );<br /> p.drawRect( 5,5, size().width()10, size().height()<s>10 );<br />}
<br />QColor SWidget::getLineColor() const<br />{<br /> return lineColor;<br />}
<br />void SWidget::setLineColor( QColor c )<br />{<br /> qDebug() &lt;&lt; ''FUNCTION'';<br /> lineColor = c;<br />}
<br />QColor SWidget::getRectColor() const<br />{<br /> return rectColor;<br />}
<br />void SWidget::setRectColor( QColor c )<br />{<br /> qDebug() &lt;&lt; ''FUNCTION'';<br /> rectColor = c;<br />}<br />


main.cpp

<br />#include &lt;QtGui/QApplication&amp;gt;<br />#include &quot;mainwindow.h&amp;quot;<br />#include &quot;swidget.h&amp;quot;
<br />int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);
<br /> //create widget<br /> SWidget *w = new SWidget();
<br /> //open and set style sheet<br /> QFile f( &quot;sheet.qss&amp;quot; );<br /> if( f.open( QFile::ReadOnly ) )<br /> {<br /> QString ssheet = QLatin1String( f.readAll() );<br /> a.setStyleSheet( ssheet );<br /> }
<br /> w</s>&gt;show();

return a.exec&amp;amp;#40;&amp;#41;;<br />}<br />


sheet.qss

<br />SWidget<br />{<br /> qproperty-lineColor: yellow;<br /> qproperty-rectColor: red;<br />}<br />

Code Walkthrough

We need to define our custom widget and assign new properties for further. In this case we need two custom color properties. One for line color and one for rectangle color.

<br />class SWidget : public QWidget<br />{<br /> Q_OBJECT<br /> Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor DESIGNABLE true)<br /> Q_PROPERTY(QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true)<br />

Methods required by new properties:

<br /> QColor getLineColor() const;<br /> void setLineColor( QColor c );

QColor getRectColor() const;<br /> void setRectColor( QColor c );<br />

We'll reimplement the paintEvent

<br />protected:<br /> void paintEvent( QPaintEvent *e );<br />

Private members will hold our Q_PROPERTY values

<br />private:<br /> QColor lineColor;<br /> QColor rectColor;<br />

swidget.cpp
Paint event reimplementation. Drawing of line that goes across the widget and rectangle is performed in paintEvent for simplicty.

<br />void SWidget::paintEvent( QPaintEvent *e )<br />{<br /> QPainter p(this);<br /> p.setPen( getLineColor() );<br /> p.drawLine( 0,0, size().width(), size().height() );<br /> p.setPen( getRectColor() );<br /> p.drawRect( 5,5, size().width()10, size().height()<s>10 );<br />}<br />


Define the setters and getters for lineColor and rectColor properties.
NOTE: we're using qDebug() to make sure that our setters are being called in further widget restyling.

<br />QColor SWidget::getLineColor() const<br />{<br /> return lineColor;<br />}
<br />void SWidget::setLineColor( QColor c )<br />{<br /> qDebug() &lt;&lt; ''FUNCTION'';<br /> lineColor = c;<br />}
<br />QColor SWidget::getRectColor() const<br />{<br /> return rectColor;<br />}
<br />void SWidget::setRectColor( QColor c )<br />{<br /> qDebug() &lt;&lt; ''FUNCTION'';<br /> rectColor = c;<br />}<br />


Let's go into main.cpp now.
We'll create very simple gui app that will show our custom widget:

<br />int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);
<br /> //create widget<br /> SWidget *w = new SWidget();<br />


Open the sheet.qss file that holds qproperties for this application. If open is succeedded get data in QString ssheet and apply stylle sheet to QApplication object.

<br /> QFile f( &quot;sheet.qss&amp;quot; );<br /> if( f.open( QFile::ReadOnly ) )<br /> {<br /> QString ssheet = QLatin1String( f.readAll() );<br /> a.setStyleSheet( ssheet );<br /> }<br />


Show widget and enter main event loop.

<br /> w</s>&gt;show();

return a.exec&amp;amp;#40;&amp;#41;;<br />}<br />

Inside sheet.qss we're defining line and rectangle color for SWidget

<br />SWidget<br />{<br /> qproperty-lineColor: yellow;<br /> qproperty-rectColor: red;<br />}<br />

Please note that all SWidget widget we'll be styled in the same way.

In Case that we have:

<br />SWidget *w = new SWidget();<br />SWidget *w2 = new SWidget();<br />


and want custom look of each of them, we need to add names for them:

<br />w-&gt;setObjectName(&quot;w1&amp;quot;);<br />w2-&gt;setObjectName(&quot;w2);<br />

Noe, sheet.qss may look like this:

<br />SWidget#w1<br />{<br /> qproperty-lineColor: yellow;<br /> qproperty-rectColor: red;<br />}<br />SWidget#w2<br />{<br /> qproperty-lineColor: blue;<br /> qproperty-rectColor: white;<br />}<br />