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. Building a custom paintEvent for a custom widget.
Forum Updated to NodeBB v4.3 + New Features

Building a custom paintEvent for a custom widget.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.1k Views
  • 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    Hi. I have a custom widget and according to this document, custom widgets based on QWidget have trouble with stylesheets and will only take background as an argument. As a result, I have attempted to replicate the accompanying custom paintEvent code on my own widget. My confusions on this custom code, which I've placed onto my custom widget's cpp file, are listed in the comments of the file.

    void TitleBar::paintEvent(QPaintEvent *)  //So in order to call this, I would be required to call a QPaintEvent pointer about the widget I am trying to build, right?
    {
        QStyleOption opt;       
        opt.initFrom(this);     
        QPainter p(this); 
        style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);   //Where does "style() come from?
    }
    
    

    Thank you for reading through the series of questions, it seemed easier to understand as a whole rather than making
    separate posts on a single piece of code. Also thanks in advance for those who let me know of any additional information required to solve this issue.

    jsulmJ 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      Hi. I have a custom widget and according to this document, custom widgets based on QWidget have trouble with stylesheets and will only take background as an argument. As a result, I have attempted to replicate the accompanying custom paintEvent code on my own widget. My confusions on this custom code, which I've placed onto my custom widget's cpp file, are listed in the comments of the file.

      void TitleBar::paintEvent(QPaintEvent *)  //So in order to call this, I would be required to call a QPaintEvent pointer about the widget I am trying to build, right?
      {
          QStyleOption opt;       
          opt.initFrom(this);     
          QPainter p(this); 
          style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);   //Where does "style() come from?
      }
      
      

      Thank you for reading through the series of questions, it seemed easier to understand as a whole rather than making
      separate posts on a single piece of code. Also thanks in advance for those who let me know of any additional information required to solve this issue.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Dummie1138 The link you posted is for Qt 4.8! Do you really use Qt 4.8?

      "//So in order to call this, I would be required to call a QPaintEvent pointer about the widget I am trying to build, right?" - wrong, paintEvent is called by Qt framework whenever widget should repaint.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Dummie1138D 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Dummie1138 The link you posted is for Qt 4.8! Do you really use Qt 4.8?

        "//So in order to call this, I would be required to call a QPaintEvent pointer about the widget I am trying to build, right?" - wrong, paintEvent is called by Qt framework whenever widget should repaint.

        Dummie1138D Offline
        Dummie1138D Offline
        Dummie1138
        wrote on last edited by
        #3

        @jsulm I am using Qt 5.12 but this document from 4.8 is the only available resource I found, both from googling my issue (Custom widget not changing text color) and from this stackoverflow post on the same issue. Is this issue not in Qt 5 or 6?

        Also in regards to calling a QPaintEvent pointer, doing something like this in the main code shouldn't happen, right?

        paintEvent(*some_pointer);
        
        Christian EhrlicherC 1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          I remember that if adding setAttribute(Qt::WA_StyledBackground, true) in the constructor, then there's no need to reimplement paintEvent to let stylesheet background work for custom widget, if you are using Qt5 :)

          Dummie1138D 1 Reply Last reply
          0
          • Dummie1138D Dummie1138

            @jsulm I am using Qt 5.12 but this document from 4.8 is the only available resource I found, both from googling my issue (Custom widget not changing text color) and from this stackoverflow post on the same issue. Is this issue not in Qt 5 or 6?

            Also in regards to calling a QPaintEvent pointer, doing something like this in the main code shouldn't happen, right?

            paintEvent(*some_pointer);
            
            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Dummie1138 said in Building a custom paintEvent for a custom widget.:

            from 4.8 is the only available resource I

            https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-stylable-widgets

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • B Bonnie

              I remember that if adding setAttribute(Qt::WA_StyledBackground, true) in the constructor, then there's no need to reimplement paintEvent to let stylesheet background work for custom widget, if you are using Qt5 :)

              Dummie1138D Offline
              Dummie1138D Offline
              Dummie1138
              wrote on last edited by
              #6

              @Bonnie Is that the page constructor or the custom widget constructor?

              B 1 Reply Last reply
              0
              • Dummie1138D Dummie1138

                @Bonnie Is that the page constructor or the custom widget constructor?

                B Offline
                B Offline
                Bonnie
                wrote on last edited by
                #7

                @Dummie1138 custom widget's constructor, setAttribute(Qt::WidgetAttribute attribute, bool on) is a member function of QWidget.

                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