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. Adding your own function to QFrame or any other class
Qt 6.11 is out! See what's new in the release blog

Adding your own function to QFrame or any other class

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 3.8k 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.
  • AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by Ahti
    #1

    How can i add my own function to QFrame Class so that i can use it with the QFrame object.

    what is a signature ?? Lol

    mrjjM 1 Reply Last reply
    0
    • AhtiA Ahti

      How can i add my own function to QFrame Class so that i can use it with the QFrame object.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @Ahti

      Used it where ?
      You can always subclass a QFrame.

      AhtiA 1 Reply Last reply
      1
      • mrjjM mrjj

        @Ahti

        Used it where ?
        You can always subclass a QFrame.

        AhtiA Offline
        AhtiA Offline
        Ahti
        wrote on last edited by Ahti
        #3

        @mrjj I simply wanna add a function namely "Drawshadow" of my own which would draw shadow around the borders of QFrame object.

        This is what i wanna do:

        QFrame test ;
        test.drawshadow(...);
        

        but before i use drawshadow(); function i must have to first define it in the QFrame class so that QFrame object ( in this case test ) would work.

        what is a signature ?? Lol

        mrjjM 1 Reply Last reply
        0
        • AhtiA Ahti

          @mrjj I simply wanna add a function namely "Drawshadow" of my own which would draw shadow around the borders of QFrame object.

          This is what i wanna do:

          QFrame test ;
          test.drawshadow(...);
          

          but before i use drawshadow(); function i must have to first define it in the QFrame class so that QFrame object ( in this case test ) would work.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Ahti
          Hi
          You can just create your own subclass

          #include <QFrame>
          
          class MyFrame : public QFrame {
            Q_OBJECT
           public:
           void drawshadow(.....) /// your func
            MyFrame(QWidget* parent = 0, Qt::WindowFlags f = 0): QFrame(parent, f) {
            }
           protected:
            virtual void paintEvent(QPaintEvent* e) override {
              QFrame::paintEvent(e); // pass event to base class
            }
          };
          

          But there a note:
          You cannot draw outside of the QFrame. So keep that in mind.
          Also you cannot use a Painter object outside of paintEvent.
          But if you use the Effect classes, it might work.
          http://doc.qt.io/qt-5/qgraphicsdropshadoweffect.html

          So if its ok the shadow is inside the QFrame area, then u can use the paintEvent.
          Elese the qgraphicsdropshadoweffect might help you out.

          If you just need a shadow, it can be done with no function inside class.

          QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
              effect->setBlurRadius(5);
              effect->setXOffset(5);
              effect->setYOffset(5);
              effect->setColor(Qt::black);
              ui->frame->setAutoFillBackground(true);
              ui->frame->setGraphicsEffect(effect);
          

          alt text

          Updated code to use setAutoFillBackground so drop is not drawn inside.

          AhtiA 1 Reply Last reply
          4
          • mrjjM mrjj

            @Ahti
            Hi
            You can just create your own subclass

            #include <QFrame>
            
            class MyFrame : public QFrame {
              Q_OBJECT
             public:
             void drawshadow(.....) /// your func
              MyFrame(QWidget* parent = 0, Qt::WindowFlags f = 0): QFrame(parent, f) {
              }
             protected:
              virtual void paintEvent(QPaintEvent* e) override {
                QFrame::paintEvent(e); // pass event to base class
              }
            };
            

            But there a note:
            You cannot draw outside of the QFrame. So keep that in mind.
            Also you cannot use a Painter object outside of paintEvent.
            But if you use the Effect classes, it might work.
            http://doc.qt.io/qt-5/qgraphicsdropshadoweffect.html

            So if its ok the shadow is inside the QFrame area, then u can use the paintEvent.
            Elese the qgraphicsdropshadoweffect might help you out.

            If you just need a shadow, it can be done with no function inside class.

            QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
                effect->setBlurRadius(5);
                effect->setXOffset(5);
                effect->setYOffset(5);
                effect->setColor(Qt::black);
                ui->frame->setAutoFillBackground(true);
                ui->frame->setGraphicsEffect(effect);
            

            alt text

            Updated code to use setAutoFillBackground so drop is not drawn inside.

            AhtiA Offline
            AhtiA Offline
            Ahti
            wrote on last edited by
            #5

            @mrjj Thanks :)

            what is a signature ?? Lol

            mrjjM 1 Reply Last reply
            1
            • AhtiA Ahti

              @mrjj Thanks :)

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @Ahti
              Np :)
              If you need many of them , just make it a function and call it pr Frame.

              void AddDrop(QFrame * frame )  {
              if (!frame) return;
              QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
                  effect->setBlurRadius(5);
                  effect->setXOffset(5);
                  effect->setYOffset(5);
                  effect->setColor(Qt::black);
                  frame->setAutoFillBackground(true);
                  frame->setGraphicsEffect(effect);
              }
              

              and call it from mainwin (after setupUI ) like
              AddDrop(ui->frame1);
              AddDrop(ui->frame2);
              AddDrop(ui->frame3);
              etc

              AhtiA 1 Reply Last reply
              2
              • mrjjM mrjj

                @Ahti
                Np :)
                If you need many of them , just make it a function and call it pr Frame.

                void AddDrop(QFrame * frame )  {
                if (!frame) return;
                QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
                    effect->setBlurRadius(5);
                    effect->setXOffset(5);
                    effect->setYOffset(5);
                    effect->setColor(Qt::black);
                    frame->setAutoFillBackground(true);
                    frame->setGraphicsEffect(effect);
                }
                

                and call it from mainwin (after setupUI ) like
                AddDrop(ui->frame1);
                AddDrop(ui->frame2);
                AddDrop(ui->frame3);
                etc

                AhtiA Offline
                AhtiA Offline
                Ahti
                wrote on last edited by Ahti
                #7

                @mrjj I already created that generic function :D thanks anyways :) what does autofillbackground method does btw ?

                what is a signature ?? Lol

                mrjjM 1 Reply Last reply
                0
                • AhtiA Ahti

                  @mrjj I already created that generic function :D thanks anyways :) what does autofillbackground method does btw ?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Ahti said in Adding your own function to QFrame or any other class:

                  autofillbackground

                  http://doc.qt.io/qt-5/qwidget.html#autoFillBackground-prop

                  Basically, it controls if the background is filled before the Paint is called.

                  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