Why GUI (form) header file placed in .cpp file ?



  • Hi all,
    When a new Qt Designer Form Class is created , 1 header file , 1 cpp file and 1 form is added.
    for eg: dialog.h , dialog.cpp and dialog.ui files...

    My doubt is why ui_dialog.h is included in dialog.cpp file, rather than in dialog.h file...Is there any design consideration followed for this implementation ???

    Any suggestion/view is appreciable....

    Regards,
    Vinoth R


  • Qt Champions 2016

    @Vinoth-Rajendran-4
    This is because its best to limit includes in the .h file ( for compile speed)
    and you do not need
    ui_dialog.h there as .h should be for definition and .CPP for the implementation.

    So in .h you do not need to know anything ui_xxx declares. only in the
    .cpp where you might use ui->xxxx in some of your functions.

    Hope it makes sense :)

    Its mostly only something you will notice when you try to declare the full function in the .H file.

    class MainWindow : public QMainWindow {
      Q_OBJECT
     protected:
      virtual void mouseMoveEvent(QMouseEvent* event) override {
       // this code belongs to the .cpp
      };
    };
    

    instead of

    class MainWindow : public QMainWindow {
      Q_OBJECT
     protected:
      virtual void mouseMoveEvent(QMouseEvent* event) override;
    };
    
    and in cpp file
    void MainWindow::mouseMoveEvent(QMouseEvent *event)
    {
    ui->xxxxx
    }
    
    

    Note that creator can move it for you. ( from .h -> cpp)
    Simple right click function and choose Refactor.


Log in to reply
 

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