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!!



  • 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

    ui->pushButton->setVisible(false);
    ^

    it is in main.cpp and I am including
    #include "Profile.h"
    #include <QApplication>
    #include <QtGui>

    what am I doing wrong

    EDIT:

    also when I call the function from main it says

    error: cannot call member function 'void Profile::startup()' without object
    Profile::startup();
    ^



  • @ Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
    {
    ui->setupUi(this);
    ui->pushButton->setVisible(false);
    ui->radioButton->setVisible(false);
    }@

    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?



  • Hey man,

    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).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.