Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Qt startup function
I need to find out a good way to have a certain function run soon as my Qt program starts. I have goggled around and haven't seen a good explanation, could you tell me how to do this?
Thanks in advance!!
jeremy_k last edited by
How do you define as soon as the program starts?
If the event loop being active isn't required, call the function from main() before QCoreApplication::exec(). If the event loop is required, post an event prior to exec(), and then implement event() or eventFilter() somewhere to receive it. One way to post an event is with a QTimer with duration = 0.
What do you mean as soon as Qt program starts ? Is it when you create the QCoreApplicaiton/QApplication/QGUIApplication object ? You can subclass QCoreApplication and write what do you want in constructor
I want to hide some pushbuttons when the program opens. but when I put it in main it gives me the error
'ui' was not declared in this scope
it is in main.cpp and I am including
what am I doing wrong
also when I call the function from main it says
error: cannot call member function 'void Profile::startup()' without object
@ Widget::Widget(QWidget *parent) :
You can set in the constructor itself. If show or hide is based on some condition, you can define different method in your class with argument, before show you can call method. Inside the method you can hide or show whatever you want.
Could you be more specific on where the code goes, should I put it in main.cpp or the other source file? also do I need to change the header file at all
As you observer, code is in the constructor. This code does not go in main.cpp. It goes to source file
Ok, then what code do I put in main to call it?
giesbert last edited by
It is the constructor of the UI class. The UI class is typically created in main.cpp. The constructor is always called, when you create the objedct (e.g. with new or on the stack).