showEvent Linux vs. Windows11
-
Greetings together,
I write my coder under Ubuntu and port it to Windows 11 from time to time. Now I recognized, that the showEvent of the main windows is not executed reliably under Windows. Sometimes the code holds at a break point in showEvent, sometimes it doesn't. So as a workaround I called it from the constructor. But this is ugly. Can anyone tell the reason or what I did wrong ? Can't I assume, that the showEvent is called each time a window is shown ? (making showEvent public does not help)
Thank you very much in advance.from my .h file...
class MainWindow : public QMainWindow { Q_OBJECT private: void showEvent(QShowEvent* event); public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); };
from my .cpp file...
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { showEvent(nullptr);//<-- not necessary under Linux } MainWindow::~MainWindow() { } void MainWindow::showEvent(QShowEvent *event) { TestWidget testWidget=TestWidget(); testWidget.exec(); }
-
@Der_andere_Sven said in showEvent Linux vs. Windows11:
private: void showEvent(QShowEvent* event);
What do you think this does? Does your compiler not give you a warning for this line?
Can't I assume, that the showEvent is called each time a window is shown ?
WhichshowEvent()
? Yours should never be called by the Qt system.UPDATE
You are intending to overrideQWidget::showEvent()
. But you do not use theoverride
qualifier. I thought that C++ (like, say, C#) would then treat your declaration as a brand new, unrelated method with the same name as the base one. But apparentlyoverride
is only a "compiler hint", your does still overrideQWidget::showEvent()
. So ignore above, but please mark your override methods withoverride
, so that we are all sure, e.g.:> protected: > virtual void showEvent(QShowEvent* event) override;
Please do that now, so we are certain where we are.
So far as know,
QWidget::showEvent()
should be called the same whether Linux or Windows. Just try (e.g. frommain()
) something like:QApplication a(argc, argv); MainWindow mw = new MainWindow; mw->show(); return a.exec();
Your
void MainWindow::showEvent(QShowEvent *event)
override should be called. It should call the base implementation, like:void MainWindow::showEvent(QShowEvent *event) { qDebug() << "MainWindow::showEvent"; QMainWindow::showEvent(event); }
Try that on both platforms.
Note that you must have the
a.exec()
, else it won't get shown.showEvent()
does not get called on the line where you gomw->show()
. That "marks" the widget to be shown. The event loop causes it to actually be shown, and it is at that instant (i.e. when the window is actually visually shown) thatshowEvent()
gets called.If, using the code, above you still find
showEvent()
is not called under Windows, then please report the exact version of Qt you are using. -
Hallo Jon,
yeah, adding override seems to work. Thank you very much.
Another thing: In the past I've already wondered what will prevent me from misspelling methods like "showEvent". But override will do it. The compiler will complain if there is nothing to override.
best regards
Sven -
@Der_andere_Sven said in showEvent Linux vs. Windows11:
Another thing: In the past I've already wondered what will prevent me from misspelling methods like "showEvent". But override will do it. The compiler will complain if there is nothing to override.
That is precisely why you should always use
override
when you want to override! If you have misspelled the method name that would make it error.yeah, adding override seems to work. Thank you very much.
I am surprised at this. You can see from what I crossed out in my previous post I discovered that in C++ my understanding is that your original
private: void showEvent(QShowEvent* event);
should nonetheless have found & overridden the base method even though you did not specify
override
.override
in C++ turns out to be only a hint, not a code-changing thing. See e.g. https://stackoverflow.com/questions/41505335/how-to-make-showevent-get-called or https://www.reddit.com/r/cpp_questions/comments/kp3v7w/is_override_keyword_required/. So (unless those have been changed in more recent C++) I am not sure how just addingoverride
could have altered any behaviour.... -
On a more philosophical level, don't look for workarounds for things that "should work". Strive to understand what you did wrong in the first place that made you look for a work-around. showEvent should work reliably on all Qt supported platforms. If it doesnt' then I'm fairly certain then you aren't using it correctly...and for what its worth...you don't do events directly. You call slots that indirectly trigger events. You should rarely if ever have to screw with showEvent. The Q7 way is QWidget::setVisible() or QWidget::show().