Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom widget and stylesheet
Qt 6.11 is out! See what's new in the release blog

Custom widget and stylesheet

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elephanten
    wrote on last edited by
    #1

    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);
    }
    @

    1 Reply Last reply
    0
    • E Offline
      E Offline
      elephanten
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gmaro
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          elephanten
          wrote on last edited by
          #4

          Thanks a lot, it is a really brilliant idea!

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved