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. A couple of questions on a Widget Application

A couple of questions on a Widget Application

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 6 Posters 2.9k Views 4 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #1

    Hi all,
    Please take a look at this Widget Application.
    I deployed and ran it successfully, but I also have a few questions:

    1- Look at the void CustomWidget::paintEvent(QPaintEvent *) in CustomWidget.cpp please. It has no pointer of type QPaintEvent in its arguments list. It seems to be useless but when I remove that argument, , the program shows only a plain window! What is the reason please?

    2- There are these functions declared and implemented:

    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    

    But where are they called!? :(

    3- The implementation of these two functions are identical. So why having both!!?

    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    

    Thank you.

    raven-worxR 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      1:
      paintEvent(QPaintEvent *)
      The variable is just unused. if you remove it, its not longer paintEvent function and
      its NOT called. ( you change its signature)
      Note its ONLY the variable name missing. NOT the type.
      You can omit the name and then u get no unused warning.
      paintEvent(QPaintEvent *im_not_used) will give warning

      2: They are called from Qt system. As part of the event system.
      Those are virtual functions. and you override them to add code.

      3: They are very different
      mousePressEvent = when you click
      mouseMoveEvent = when mouse is moving on screen

      tomyT 1 Reply Last reply
      3
      • tomyT tomy

        Hi all,
        Please take a look at this Widget Application.
        I deployed and ran it successfully, but I also have a few questions:

        1- Look at the void CustomWidget::paintEvent(QPaintEvent *) in CustomWidget.cpp please. It has no pointer of type QPaintEvent in its arguments list. It seems to be useless but when I remove that argument, , the program shows only a plain window! What is the reason please?

        2- There are these functions declared and implemented:

        void paintEvent(QPaintEvent *event);
        void mousePressEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        

        But where are they called!? :(

        3- The implementation of these two functions are identical. So why having both!!?

        void mousePressEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        

        Thank you.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @tomy said in A couple of questions on a Widget Application:

        1- Look at the void CustomWidget::paintEvent(QPaintEvent *) in CustomWidget.cpp please. It has no pointer of type QPaintEvent in its arguments list. It seems to be useless but when I remove that argument, , the program shows only a plain window! What is the reason please?

        best way for such cases is to Q_DECL_OVERRIDE to the method when you know you know to reimplement an existing method. In this case you will immediately receive a compiler error. This can safe you much time and confusion.

        class MyCustomWidget : public QWidget
        {
        ...
        protected:
            virtual void paintEvent(QPaintEvent* event) Q_DECL_OVERRIDE;
        ...
        }
        

        So when the function signature is different (e.g. different name, parameters or return type) from the supposed deriving method an error is thrown.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        2
        • mrjjM mrjj

          Hi
          1:
          paintEvent(QPaintEvent *)
          The variable is just unused. if you remove it, its not longer paintEvent function and
          its NOT called. ( you change its signature)
          Note its ONLY the variable name missing. NOT the type.
          You can omit the name and then u get no unused warning.
          paintEvent(QPaintEvent *im_not_used) will give warning

          2: They are called from Qt system. As part of the event system.
          Those are virtual functions. and you override them to add code.

          3: They are very different
          mousePressEvent = when you click
          mouseMoveEvent = when mouse is moving on screen

          tomyT Offline
          tomyT Offline
          tomy
          wrote on last edited by
          #4

          @mrjj said in A couple of questions on a Widget Application:

          Hi
          1:
          paintEvent(QPaintEvent *)
          The variable is just unused. if you remove it, its not longer paintEvent function and
          its NOT called. ( you change its signature)
          Note its ONLY the variable name missing. NOT the type.
          You can omit the name and then u get no unused warning.
          paintEvent(QPaintEvent *im_not_used) will give warning

          Do these mean that that function is virtual and if I change its return or arguments or order of them it no longer is an overriding of the system's virtual function?

          2: They are called from Qt system. As part of the event system.
          Those are virtual functions. and you override them to add code.

          I didn't know that. Should we know each of those functions?

          mrjjM 1 Reply Last reply
          0
          • tomyT tomy

            @mrjj said in A couple of questions on a Widget Application:

            Hi
            1:
            paintEvent(QPaintEvent *)
            The variable is just unused. if you remove it, its not longer paintEvent function and
            its NOT called. ( you change its signature)
            Note its ONLY the variable name missing. NOT the type.
            You can omit the name and then u get no unused warning.
            paintEvent(QPaintEvent *im_not_used) will give warning

            Do these mean that that function is virtual and if I change its return or arguments or order of them it no longer is an overriding of the system's virtual function?

            2: They are called from Qt system. As part of the event system.
            Those are virtual functions. and you override them to add code.

            I didn't know that. Should we know each of those functions?

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

            @tomy said in A couple of questions on a Widget Application:

            function is virtual and if I change its return or arguments or order of them it no longer is an overriding of the system's virtual function?

            yes. if u change return type or aruguments its no longer the same function and hence you are not overriding anything.

            • I didn't know that. Should we know each of those functions?
              Only if you need to do something when mouse is down or released again.

            • system's virtual function?
              Its actually not the system as such, its from the base classes. But you could call it system :)

            1 Reply Last reply
            1
            • tomyT Offline
              tomyT Offline
              tomy
              wrote on last edited by
              #6

              Thank you.
              And isn't there any easier way to use QColors, than "#FFBB33"?
              for example, blue, orange or so on.

              jsulmJ 1 Reply Last reply
              0
              • tomyT tomy

                Thank you.
                And isn't there any easier way to use QColors, than "#FFBB33"?
                for example, blue, orange or so on.

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

                @tomy Well, there are millions of colors :-)
                You can define your own constant variables as needed.

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

                1 Reply Last reply
                2
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  QColor has a lot of constructors.
                  You can create a color in any of the ways:

                  QColor c1();                //invalid color
                  QColor c2(Qt::red);         //using global constants
                  QColor c3(100,200,255,123); //using RGBA values
                  QColor c4(0xFFAABBCC);      //using QRgb
                  QColor c5("#FF112233");     //using string value
                  QColor c6("blue");          //using string name
                  QColor c7(otherColor);      //using other color
                  

                  The existing global constans for the second variant are listed here and for the string name versions here.

                  1 Reply Last reply
                  4
                  • AmoghA Offline
                    AmoghA Offline
                    Amogh
                    wrote on last edited by
                    #9
                    This post is deleted!
                    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