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. Include header issue

Include header issue

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 7 Posters 8.5k Views 5 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by
    #7

    Test project is available here - Mega. Thanks.

    1 Reply Last reply
    0
    • JohanSoloJ JohanSolo

      This is basic C / C++ usage, see for instance forward declaration on wiklipedia or on stackoverflow.

      Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by
      #8

      @JohanSolo

      I know about forward declaration but it's not working in my case.

      1 Reply Last reply
      0
      • sneubertS Offline
        sneubertS Offline
        sneubert
        wrote on last edited by
        #9

        Hi Cobra91151,

        if you forward declarate TestWindow in appsettings.h like @JohanSolo suggested

        #ifndef APPSETTINGS_H
        #define APPSETTINGS_H
        
        #include <QWidget>
        //*********************
        
        class TestWindow;
        
        namespace Ui {
        class AppSettings;
        }
        
        class AppSettings : public QWidget
        {
            Q_OBJECT
        
        public:
            explicit AppSettings(QWidget *parent = 0);
            ~AppSettings();
        
        private:
            Ui::AppSettings *ui;
            TestWindow *mainWindow;
        };
        
        #endif // APPSETTINGS_H
        

        and include testwindow.h in apsettings.cpp your project can be compiled

        #include "appsettings.h"
        #include "ui_appsettings.h"
        
        #include "testwindow.h"
        
        AppSettings::AppSettings(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::AppSettings)
        {
            ui->setupUi(this);
            this->setWindowTitle("Settings");
            this->setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
            mainWindow = new TestWindow();
        }
        
        AppSettings::~AppSettings()
        {
            delete ui;
        }
        

        But a short look at your code showed, that your are creating a AppSettings object in TestWindow constructor.
        In AppSettings construtor you create a TestWindowobject, where you create a AppSettingsobject, where you
        create a TestWindow object, where you create a AppSettingsobject, ...

        I doubt this was your intention.

        Cobra91151C 1 Reply Last reply
        1
        • sneubertS sneubert

          Hi Cobra91151,

          if you forward declarate TestWindow in appsettings.h like @JohanSolo suggested

          #ifndef APPSETTINGS_H
          #define APPSETTINGS_H
          
          #include <QWidget>
          //*********************
          
          class TestWindow;
          
          namespace Ui {
          class AppSettings;
          }
          
          class AppSettings : public QWidget
          {
              Q_OBJECT
          
          public:
              explicit AppSettings(QWidget *parent = 0);
              ~AppSettings();
          
          private:
              Ui::AppSettings *ui;
              TestWindow *mainWindow;
          };
          
          #endif // APPSETTINGS_H
          

          and include testwindow.h in apsettings.cpp your project can be compiled

          #include "appsettings.h"
          #include "ui_appsettings.h"
          
          #include "testwindow.h"
          
          AppSettings::AppSettings(QWidget *parent) :
              QWidget(parent),
              ui(new Ui::AppSettings)
          {
              ui->setupUi(this);
              this->setWindowTitle("Settings");
              this->setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
              mainWindow = new TestWindow();
          }
          
          AppSettings::~AppSettings()
          {
              delete ui;
          }
          

          But a short look at your code showed, that your are creating a AppSettings object in TestWindow constructor.
          In AppSettings construtor you create a TestWindowobject, where you create a AppSettingsobject, where you
          create a TestWindow object, where you create a AppSettingsobject, ...

          I doubt this was your intention.

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #10

          @sneubert

          Yes, it compiles but not working. I just want for example, to set the main window title from settings window (or other communications between two windows). So how to do it?

          1 Reply Last reply
          0
          • Venkatesh VV Offline
            Venkatesh VV Offline
            Venkatesh V
            wrote on last edited by
            #11

            Hi @Cobra91151

            In your case take one more class(mainClass) that contains both headerfile and all connection between these two window should happens throgh this class only.

            whenever window1 sends any signal catch it in your mainClass and connect that to window2 and vice versa. for this, your both window objects shoul create in mainClass.
            this will give solution for your question

            1 Reply Last reply
            5
            • Venkatesh VV Offline
              Venkatesh VV Offline
              Venkatesh V
              wrote on last edited by
              #12

              @Cobra91151

              If it solved then make it solved.

              Cobra91151C 1 Reply Last reply
              1
              • Venkatesh VV Venkatesh V

                @Cobra91151

                If it solved then make it solved.

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by
                #13

                @Venkatesh-V

                I have added main class but the issue is still present.

                Test project is available here - Mega.

                1 Reply Last reply
                0
                • Venkatesh VV Offline
                  Venkatesh VV Offline
                  Venkatesh V
                  wrote on last edited by
                  #14

                  @Cobra91151

                  still you made mistake,

                  you have created communicationWorker object in AppSetting and in communicationWorker you created testWindow object and then in testwindow you created appsetting object, so it again became cycle.

                  what i suggest you is,
                  in communicationWorker create both appsetting and testwindow objects,

                  in communicationWorker made connections between appsetting and testwindow

                  whenever any signal emited in any of these two class catch those on communicationworker class and pass to required class.

                  what you made in above example that is wrong.

                  Cobra91151C 2 Replies Last reply
                  3
                  • Venkatesh VV Venkatesh V

                    @Cobra91151

                    still you made mistake,

                    you have created communicationWorker object in AppSetting and in communicationWorker you created testWindow object and then in testwindow you created appsetting object, so it again became cycle.

                    what i suggest you is,
                    in communicationWorker create both appsetting and testwindow objects,

                    in communicationWorker made connections between appsetting and testwindow

                    whenever any signal emited in any of these two class catch those on communicationworker class and pass to required class.

                    what you made in above example that is wrong.

                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by Cobra91151
                    #15

                    @Venkatesh-V

                    OK. Then in what file I should include communicationWorker.h? Thanks.

                    1 Reply Last reply
                    1
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #16

                      I don't think you have clear the difference between a class and an instance of the class.

                      Personally I think that guerilla programming is evil so: http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      Cobra91151C 1 Reply Last reply
                      2
                      • VRoninV VRonin

                        I don't think you have clear the difference between a class and an instance of the class.

                        Personally I think that guerilla programming is evil so: http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf

                        Cobra91151C Offline
                        Cobra91151C Offline
                        Cobra91151
                        wrote on last edited by Cobra91151
                        #17

                        @VRonin

                        class AppSettings is a class
                        AppSettings *testSettings = new AppSettings() - is the instance of the class (heap);
                        AppSettings testSettings; - is the instance of the class (stack).

                        But to get the instance of a class you should include the appropriate header file.
                        So I'm not right?

                        FlotisableF 1 Reply Last reply
                        1
                        • Venkatesh VV Venkatesh V

                          @Cobra91151

                          still you made mistake,

                          you have created communicationWorker object in AppSetting and in communicationWorker you created testWindow object and then in testwindow you created appsetting object, so it again became cycle.

                          what i suggest you is,
                          in communicationWorker create both appsetting and testwindow objects,

                          in communicationWorker made connections between appsetting and testwindow

                          whenever any signal emited in any of these two class catch those on communicationworker class and pass to required class.

                          what you made in above example that is wrong.

                          Cobra91151C Offline
                          Cobra91151C Offline
                          Cobra91151
                          wrote on last edited by
                          #18

                          @Venkatesh-V

                          I have changed code to your suggestion but it's not emitting a signal.

                          Test project - Mega.

                          Can you show an example? Thanks.

                          1 Reply Last reply
                          1
                          • Cobra91151C Cobra91151

                            @VRonin

                            class AppSettings is a class
                            AppSettings *testSettings = new AppSettings() - is the instance of the class (heap);
                            AppSettings testSettings; - is the instance of the class (stack).

                            But to get the instance of a class you should include the appropriate header file.
                            So I'm not right?

                            FlotisableF Offline
                            FlotisableF Offline
                            Flotisable
                            wrote on last edited by
                            #19

                            @Cobra91151
                            you need to include the header file only when you instantiate an object.

                            in your header file, you only declare pointers, so forward declaration is enough. you only need to include header file in cpp file

                            and in your code, your main function just have a TestWindow, there is neither CommunicationWorker nor AppSettings, so the connection between TestWindow and AppSettings do not exist.

                            I think @Venkatesh-V's suggestion is creating a CommunicationWorker as the top object, so you only need to create CommunicationWorker in the main function.

                            but you can just declare AppSettings as TestWindow's member, and connect them in the constructor of TestWindow

                            Cobra91151C 1 Reply Last reply
                            1
                            • FlotisableF Flotisable

                              @Cobra91151
                              you need to include the header file only when you instantiate an object.

                              in your header file, you only declare pointers, so forward declaration is enough. you only need to include header file in cpp file

                              and in your code, your main function just have a TestWindow, there is neither CommunicationWorker nor AppSettings, so the connection between TestWindow and AppSettings do not exist.

                              I think @Venkatesh-V's suggestion is creating a CommunicationWorker as the top object, so you only need to create CommunicationWorker in the main function.

                              but you can just declare AppSettings as TestWindow's member, and connect them in the constructor of TestWindow

                              Cobra91151C Offline
                              Cobra91151C Offline
                              Cobra91151
                              wrote on last edited by Cobra91151
                              #20

                              @Flotisable

                              I changed code to yours suggestions but windows are not communicate.

                              My codes and test project are available - Mega.

                              Can someone post an example or fix my test project? Thanks in advance.

                              1 Reply Last reply
                              0
                              • Cobra91151C Offline
                                Cobra91151C Offline
                                Cobra91151
                                wrote on last edited by
                                #21

                                I have fixed it by adding a button as a trigger, so now everything works.

                                1 Reply Last reply
                                1

                                • Login

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