Qt Style Sheets and Custom Painting Example: Difference between revisions
No edit summary |
m (formatting) |
||
(8 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:snippets]] | |||
== Introduction == | |||
==Introduction== | |||
This example demonstrates: | This example demonstrates: | ||
# use of qt style sheets | # use of qt style sheets | ||
# use of Q_PROPERTY | # use of Q_PROPERTY | ||
# how to combine 1 and 2 with custom painting | # 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. | 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''' | |||
<syntaxhighlight lang="c++"> | |||
#ifndef SWIDGET_H | |||
#define SWIDGET_H | |||
#include <QWidget> | |||
class SWidget : public QWidget | |||
{ | |||
Q_OBJECT | |||
Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor DESIGNABLE true) | |||
Q_PROPERTY(QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true) | |||
public: | |||
explicit SWidget(QWidget *parent = 0); | |||
~SWidget(); | |||
QColor getLineColor() const; | |||
void setLineColor( QColor c ); | |||
QColor getRectColor() const; | |||
void setRectColor( QColor c ); | |||
protected: | |||
void paintEvent( QPaintEvent *e ); | |||
private: | |||
QColor lineColor; | |||
QColor rectColor; | |||
}; | |||
#endif // SWIDGET_H | |||
</syntaxhighlight> | |||
'''swidget.cpp''' | |||
<syntaxhighlight lang="c++"> | |||
#include "swidget.h" | |||
#include <QPainter> | |||
#include <QDebug> | |||
SWidget::SWidget(QWidget *parent) : | |||
QWidget(parent) | |||
{ | |||
} | |||
SWidget::~SWidget() | |||
{ | |||
} | |||
void SWidget::paintEvent( QPaintEvent *e ) | |||
{ | |||
QPainter p(this); | |||
p.setPen( getLineColor() ); | |||
p.drawLine( 0,0, size().width(), size().height() ); | |||
p.setPen( getRectColor() ); | |||
p.drawRect( 5,5, size().width()–10, size().height()-10 ); | |||
} | |||
QColor SWidget::getLineColor() const | |||
{ | |||
return lineColor; | |||
} | |||
void SWidget::setLineColor( QColor c ) | |||
{ | |||
qDebug() << "FUNCTION"; | |||
lineColor = c; | |||
} | |||
QColor SWidget::getRectColor() const | |||
{ | |||
return rectColor; | |||
} | |||
void SWidget::setRectColor( QColor c ) | |||
{ | |||
qDebug() << "FUNCTION"; | |||
rectColor = c; | |||
} | |||
</syntaxhighlight> | |||
'''main.cpp''' | |||
<syntaxhighlight lang="c++"> | |||
#include <QApplication> | |||
#include "mainwindow.h" | |||
#include "swidget.h" | |||
int main(int argc, char *argv[]) | |||
{ | |||
QApplication a(argc, argv); | |||
//create widget | |||
SWidget *w = new SWidget(); | |||
//open and set style sheet | |||
QFile f( "sheet.qss" ); | |||
if( f.open( QFile::ReadOnly ) ) | |||
{ | |||
QString ssheet = QLatin1String( f.readAll() ); | |||
a.setStyleSheet( ssheet ); | |||
} | |||
w->show(); | |||
return a.exec(); | |||
} | |||
</syntaxhighlight> | |||
'''sheet.qss''' | |||
<syntaxhighlight lang="css"> | |||
SWidget | |||
{ | |||
qproperty-lineColor: yellow; | |||
qproperty-rectColor: red; | |||
} | |||
</syntaxhighlight> | |||
== 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. | |||
<code> | |||
class SWidget : public QWidget | |||
{ | |||
Q_OBJECT | |||
Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor DESIGNABLE true) | |||
Q_PROPERTY(QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true) | |||
</code> | |||
Methods required by new properties: | |||
<code> | |||
QColor getLineColor() const; | |||
void setLineColor( QColor c ); | |||
QColor getRectColor() const; | |||
void setRectColor( QColor c ); | |||
</code> | |||
We | We'll reimplement the paintEvent | ||
<code> | |||
protected: | |||
void paintEvent( QPaintEvent *e ); | |||
</code> | |||
Private members will hold our Q_PROPERTY values | |||
<code> | |||
private: | |||
QColor lineColor; | |||
QColor rectColor; | |||
</code> | |||
swidget.cpp | |||
Paint event reimplementation. Drawing of line that goes across the widget and rectangle is performed in paintEvent for simplicty. | |||
<code> | |||
void SWidget::paintEvent( QPaintEvent *e ) | |||
{ | |||
QPainter p(this); | |||
p.setPen( getLineColor() ); | |||
p.drawLine( 0,0, size().width(), size().height() ); | |||
p.setPen( getRectColor() ); | |||
p.drawRect( 5,5, size().width()–10, size().height()-10 ); | |||
} | |||
</code> | |||
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. | |||
<code> | |||
QColor SWidget::getLineColor() const | |||
{ | |||
return lineColor; | |||
} | |||
void SWidget::setLineColor( QColor c ) | |||
{ | |||
qDebug() << "FUNCTION"; | |||
lineColor = c; | |||
} | |||
QColor SWidget::getRectColor() const | |||
{ | |||
return rectColor; | |||
} | |||
void SWidget::setRectColor( QColor c ) | |||
{ | |||
qDebug() << "FUNCTION"; | |||
rectColor = c; | |||
} | |||
</code> | |||
Let's go into main.cpp now. | |||
We'll create very simple gui app that will show our custom widget: | |||
<code> | |||
int main(int argc, char *argv[]) | |||
{ | |||
QApplication a(argc, argv); | |||
//create widget | |||
SWidget *w = new SWidget(); | |||
</code> | |||
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. | |||
<code> | |||
QFile f( "sheet.qss" ); | |||
if( f.open( QFile::ReadOnly ) ) | |||
{ | |||
QString ssheet = QLatin1String( f.readAll() ); | |||
a.setStyleSheet( ssheet ); | |||
} | |||
</code> | |||
Show widget and enter main event loop. | |||
<code> | |||
w->show(); | |||
return a.exec(); | |||
} | |||
</code> | |||
Inside sheet.qss we're defining line and rectangle color for SWidget | |||
<code> | |||
SWidget | |||
{ | |||
qproperty-lineColor: yellow; | |||
qproperty-rectColor: red; | |||
} | |||
</code> | |||
Please note that all SWidget widget we'll be styled in the same way. | |||
== | In Case that we have: | ||
<code> | |||
SWidget *w = new SWidget(); | |||
SWidget *w2 = new SWidget(); | |||
</code> | |||
and want custom look of each of them, we need to add names for them: | |||
<code> | |||
w->setObjectName("w1"); | |||
w2->setObjectName("w2); | |||
</code> | |||
Noe, sheet.qss may look like this: | |||
<code> | |||
SWidget#w1 | |||
{ | |||
qproperty-lineColor: yellow; | |||
qproperty-rectColor: red; | |||
} | |||
SWidget#w2 | |||
{ | |||
qproperty-lineColor: blue; | |||
qproperty-rectColor: white; | |||
} | |||
</code> |
Latest revision as of 14:13, 30 March 2023
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
#ifndef SWIDGET_H
#define SWIDGET_H
#include <QWidget>
class SWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor DESIGNABLE true)
Q_PROPERTY(QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true)
public:
explicit SWidget(QWidget *parent = 0);
~SWidget();
QColor getLineColor() const;
void setLineColor( QColor c );
QColor getRectColor() const;
void setRectColor( QColor c );
protected:
void paintEvent( QPaintEvent *e );
private:
QColor lineColor;
QColor rectColor;
};
#endif // SWIDGET_H
swidget.cpp
#include "swidget.h"
#include <QPainter>
#include <QDebug>
SWidget::SWidget(QWidget *parent) :
QWidget(parent)
{
}
SWidget::~SWidget()
{
}
void SWidget::paintEvent( QPaintEvent *e )
{
QPainter p(this);
p.setPen( getLineColor() );
p.drawLine( 0,0, size().width(), size().height() );
p.setPen( getRectColor() );
p.drawRect( 5,5, size().width()–10, size().height()-10 );
}
QColor SWidget::getLineColor() const
{
return lineColor;
}
void SWidget::setLineColor( QColor c )
{
qDebug() << "FUNCTION";
lineColor = c;
}
QColor SWidget::getRectColor() const
{
return rectColor;
}
void SWidget::setRectColor( QColor c )
{
qDebug() << "FUNCTION";
rectColor = c;
}
main.cpp
#include <QApplication>
#include "mainwindow.h"
#include "swidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//create widget
SWidget *w = new SWidget();
//open and set style sheet
QFile f( "sheet.qss" );
if( f.open( QFile::ReadOnly ) )
{
QString ssheet = QLatin1String( f.readAll() );
a.setStyleSheet( ssheet );
}
w->show();
return a.exec();
}
sheet.qss
SWidget
{
qproperty-lineColor: yellow;
qproperty-rectColor: red;
}
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.
class SWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor DESIGNABLE true)
Q_PROPERTY(QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true)
Methods required by new properties:
QColor getLineColor() const;
void setLineColor( QColor c );
QColor getRectColor() const;
void setRectColor( QColor c );
We'll reimplement the paintEvent
protected:
void paintEvent( QPaintEvent *e );
Private members will hold our Q_PROPERTY values
private:
QColor lineColor;
QColor rectColor;
swidget.cpp Paint event reimplementation. Drawing of line that goes across the widget and rectangle is performed in paintEvent for simplicty.
void SWidget::paintEvent( QPaintEvent *e )
{
QPainter p(this);
p.setPen( getLineColor() );
p.drawLine( 0,0, size().width(), size().height() );
p.setPen( getRectColor() );
p.drawRect( 5,5, size().width()–10, size().height()-10 );
}
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.
QColor SWidget::getLineColor() const
{
return lineColor;
}
void SWidget::setLineColor( QColor c )
{
qDebug() << "FUNCTION";
lineColor = c;
}
QColor SWidget::getRectColor() const
{
return rectColor;
}
void SWidget::setRectColor( QColor c )
{
qDebug() << "FUNCTION";
rectColor = c;
}
Let's go into main.cpp now. We'll create very simple gui app that will show our custom widget:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//create widget
SWidget *w = new SWidget();
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.
QFile f( "sheet.qss" );
if( f.open( QFile::ReadOnly ) )
{
QString ssheet = QLatin1String( f.readAll() );
a.setStyleSheet( ssheet );
}
Show widget and enter main event loop.
w->show();
return a.exec();
}
Inside sheet.qss we're defining line and rectangle color for SWidget
SWidget
{
qproperty-lineColor: yellow;
qproperty-rectColor: red;
}
Please note that all SWidget widget we'll be styled in the same way.
In Case that we have:
SWidget *w = new SWidget();
SWidget *w2 = new SWidget();
and want custom look of each of them, we need to add names for them:
w->setObjectName("w1");
w2->setObjectName("w2);
Noe, sheet.qss may look like this:
SWidget#w1
{
qproperty-lineColor: yellow;
qproperty-rectColor: red;
}
SWidget#w2
{
qproperty-lineColor: blue;
qproperty-rectColor: white;
}