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. Why GUI (form) header file placed in .cpp file ?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 571 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.
  • V Offline
    V Offline
    Vinoth Rajendran 4
    wrote on last edited by A Former User
    #1

    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

    mrjjM 1 Reply Last reply
    0
    • V Vinoth Rajendran 4

      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

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

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

      1 Reply Last reply
      2

      • Login

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