Unsolved A couple of questions on a Widget Application
-
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 *)
inCustomWidget.cpp
please. It has no pointer of typeQPaintEvent
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.
-
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 warning2: 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 -
@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.
-
@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 warningDo 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?
-
@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 :)
-
-
Thank you.
And isn't there any easier way to useQColors
, than"#FFBB33"
?
for example, blue, orange or so on. -
@tomy Well, there are millions of colors :-)
You can define your own constant variables as needed. -
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.
-
This post is deleted!