QMainWindow paint event
-
Hello, sorry for the stupid question, but the Main window has a paint function? That is, a function that starts once the window open.
What I would do is this:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {} MainWindow::paint() { CreateActions(); CreateToolBars(); }
For now I only found a similar method
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { QTimer::singleShot(1, this, SLOT(paint())); } MainWindow::paint() { CreateActions(); CreateToolBars(); }
Thanks
-
@Gianluca86
what is the question?!
And what are you actually trying to do? There is no such paint() method... -
In C# windows can be created "empty", then it is called a design tool that "draws" all objects, such as buttons, toolbar, etc.
This is to avoid calling a function in the constructor of mainwindow that can crash even before the window is created.
I hope I explained better. -
@Gianluca86 said:
I hope I explained better.
no not really, sorry.
Why not calling your paint() method - which seems to me like a n ordinary init method? Does it crash in your case? -
Yes I can. I wanted to know if in the windows in Qt had a similar function like C#, and from what I understand there is not.
I have a question: the "void QWidget :: update ()" how does it work? Is there an example to look at? Unfortunately, I understand the concept but not how to use it -
@Gianluca86 said:
That is, a function that starts once the window open.
Note that your current implementation in general doesn't execute your
paint
method when the window becomes visible ("once the window open") for the first time but already after the instantiation of the window.You may want to connect to the
void visibleChanged(bool arg)
signal.{ // inside constructor connect(this, &QMainWindow::visibleChanged, this, &QMainWindow::paint); } { // inside paint method do_some_init_stuff(); disconnect(this, &QMainWindow::visibleChanged, this, &QMainWindow::paint); }
-
@Gianluca86 said:
Yes I can. I wanted to know if in the windows in Qt had a similar function like C#, and from what I understand there is not.
normally you are fine to create/init your ui in the constructor - unless you have a good reason to delay it of course.
Anyway the delaying similar to you've done is ok in such case:QmetaObject::invokeMethod(this, "delayedCalledMethodName", Qt::QueuedConnection);
Or even later, like the example shown by @Wieland
I have a question: the "void QWidget :: update ()" how does it work? Is there an example to look at? Unfortunately, I understand the concept but not how to use it
update() needs to be called whenever you need a visual (painting) update of your widget. A paint event is scheduled in the global event queue and redrawn in the next event loop iteration. Multiple update() calls are optimized here, and just a single paint event will be delivered.
If you need an immediate update you can call QWidget::repaint(). But normally update() is the way to go. -
Yes I can. I wanted to know if in the windows in Qt had a similar function like C#, and from what I understand there is not.
There's no need. Qt's classes have well behaved constructors that will not throw exceptions.
Additionally, Qt doesn't really create the GUI elements in the constructors, there's a dedicated method that does that and it's called after the constructors have run.Kind regards.
Edit: Ignore erroneous statements, please.
-
@kshegunov
QWidget::create() isn't virtual and thus not meant to be overloaded and anyway not meant to be used this way. -
@raven-worx
I didn't say it's supposed to be overridden or be used by the user. -
Thank you all, I will try to use your advice.