Help with QWidget::show()
-
wrote on 6 Nov 2021, 19:08 last edited by
I need to call a function when a QWidget gets its show() method gets called and I have looked at examples for Mainwindow but I don't understand how this works.
The widget that needs to implement show() is called ChartProperties. For declaring it in the header,
void MainWindow::showEvent(QShowEvent *ev);
doesn't work because this isn't a mainwindow and
void ChartProperties::showEvent(QShowEvent *ev)
complains about an extra member showevent.Any suggestions would be much appreciated.
I'm clueless here and thanks for any response.
-
Sorry, I'm feeling pretty dumb here.
Another class executes the ChartProperties->Show() method and when the dialog is rendered, I want to trap the ChartProperties show() event.
I have irtual void showEvent(QShowEvent *event) override;
in my ChartProperties header but I don't understand how to receive the showEvent..Hi,
@mmikeinsantarosa said in Help with QWidget::show():
I have irtual void showEvent(QShowEvent *event) override;
This is just a declaration, you still have to implement the function itself.
-
@mmikeinsantarosa said in Help with QWidget::show():
void ChartProperties::showEvent(QShowEvent *ev)
complains about an extra member showevent.What exact error message do you get?
-
wrote on 6 Nov 2021, 19:27 last edited by
@Christian-Ehrlicher said in Help with QWidget::show():
void ChartProperties::showEvent(QShowEvent *ev)
I get "extra qualification on member 'showEvent'"
-
wrote on 6 Nov 2021, 19:28 last edited by
And the widget is actually a "QDialog".
-
And please show how you defined and declared this function.
-
wrote on 6 Nov 2021, 19:34 last edited by
Realizing that the class is actually a QDialog so I just changed the declaration to
virtual void showEvent(QShowEvent *event) override;
and it doesn't complain now.So now, how do I hook it up to execute another function?
Right clicking on it and selecting refactor doesn't provide a way to create a function in my source. Do I need to create a slot to connect it to? -
@mmikeinsantarosa said in Help with QWidget::show():
So now, how do I hook it up to execute another function?
I don't understand this question. Simply create a function in your class and call it from within your other function.
-
wrote on 6 Nov 2021, 19:43 last edited by mmikeinsantarosa 11 Jun 2021, 19:43
Sorry, I'm feeling pretty dumb here.
Another class executes the ChartProperties->Show() method and when the dialog is rendered, I want to trap the ChartProperties show() event.
I have irtual void showEvent(QShowEvent *event) override;
in my ChartProperties header but I don't understand how to receive the showEvent.. -
As explained in the documentation this function is called when the widget is shown, you must not call it by yourself.
-
Sorry, I'm feeling pretty dumb here.
Another class executes the ChartProperties->Show() method and when the dialog is rendered, I want to trap the ChartProperties show() event.
I have irtual void showEvent(QShowEvent *event) override;
in my ChartProperties header but I don't understand how to receive the showEvent..Hi,
@mmikeinsantarosa said in Help with QWidget::show():
I have irtual void showEvent(QShowEvent *event) override;
This is just a declaration, you still have to implement the function itself.
-
wrote on 6 Nov 2021, 20:03 last edited by
Do I create a slot then connect this event to my slot?
-
@mmikeinsantarosa said in Help with QWidget::show():
Do I create a slot then connect this event to my slot?
What slot? showEvent() is a class function, not a slot. Please learn C++ basics first.
-
Do I create a slot then connect this event to my slot?
wrote on 6 Nov 2021, 20:16 last edited by mpergand 11 Jun 2021, 20:17Your question seems pretty close this this one
As an alternative, you can simply do:
MyDialog::show() { myFunction(); QDialog::show(); }
Using showEvent is dangerous cause it can be called multi times !
-
wrote on 6 Nov 2021, 20:19 last edited by
The method name is "showEvent" which leads me to think it's a signal. It's also listed in the signals list in the documentation for QDialog so I thought it might actually be a signal.
thanks for clearing this up SGaist.
-
@mmikeinsantarosa said in Help with QWidget::show():
It's also listed in the signals list in the documentation for QDialog
That's wrong, neither in the Qt 5 nor in the Qt 6 documentation.
It is listed under Reimplemented Protected Functions.
Beside that, signals name do not finish in "event". They usually have a name in relation to some action like rejected in QDialog.
-
wrote on 6 Nov 2021, 22:37 last edited by
I stand corrected. entering QDialog in the Qt help and selecting signals reveals signals at the top, with 3 items under it then Reimplemented Protected Functions where the showEvent function is listed.
-
I stand corrected. entering QDialog in the Qt help and selecting signals reveals signals at the top, with 3 items under it then Reimplemented Protected Functions where the showEvent function is listed.
wrote on 7 Nov 2021, 07:26 last edited by@mmikeinsantarosa
Indeed, that is how Qt documentation lays out its methods.To reiterate something @Christian-Ehrlicher said. In Qt nomenclature:
-
Methods ending in
Event
(showEvent()
,mouseMoveEvent()
) are not signals. Instead they areprotected virtual
methods. You must sub-class andoverride
if you want to access them. -
Signals tend to be named as the past tense of something that has happened (
clicked()
,customContextMenuRequested()
). You cannotoverride
them. You canconnect()
to them, without needing to sub-class. -
And slots are just named as an action to be performed (
show()
,setDiabled()
).
-
1/17