Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt startup function
Forum Updated to NodeBB v4.3 + New Features

Qt startup function

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 4.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tyler1234
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      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.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tyler1234
          wrote on last edited by
          #4

          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();
          ^

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

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

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tyler1234
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                As you observer, code is in the constructor. This code does not go in main.cpp. It goes to source file

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Tyler1234
                  wrote on last edited by
                  #8

                  Ok, then what code do I put in main to call it?

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #9

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

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved