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. [solved]Call class function in another cpp file

[solved]Call class function in another cpp file

Scheduled Pinned Locked Moved General and Desktop
classheadercppobjects
16 Posts 3 Posters 12.6k Views
  • 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.
  • JohanSoloJ Offline
    JohanSoloJ Offline
    JohanSolo
    wrote on last edited by
    #5

    OK, according to what I understood from your design, MainWindow spawns a CustomCode instance when requested. Therefore when constructing the CustomCode instance you know which MainWindow instance you have to connect to. If I were you I'd use the calling MainWindow instance as parent of the CustomCode instance. This would allow to connect the send_test() signal to the test() slot of your MainWindow instance.

    `They did not know it was impossible, so they did it.'
    -- Mark Twain

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

      How could I do that?

      If I do this in mainwindow.cpp:

          codewindow = new CustomCode;
          codewindow->setParent(this);
          connect(codewindow,SIGNAL(send_test()),SLOT(test()));
          codewindow->show();
      

      This makes it compile but instead of a seperate window the current mainwindow gets overwritten with the codewindow and the send_test() is in jsfunctions.h, not in CustomCode.h. Would I have to make a function that calls another function that calls the signal? Or am I missing it here? Sorry

      Edit:

      Succes!

      I used : (in mainwindow.cpp)

      void MainWindow::on_actionAdd_custom_logic_triggered()
      {
          codewindow = new CustomCode;
          QObject::connect(&jsfunction,SIGNAL(send_test()),this,SLOT(test()));
          codewindow->show();
      }
      

      added extern jsfunctions jsfunction; to mainwindow.h and customcode.h
      added jsfunctions jsfunction; in jsfunctions.cpp

      and changed jsfunctions.h to

      signals:
          void send_test();
      
      public slots:
          void call_test()
          {
              emit send_test();
          }
      

      Now it works! test() gets properly called and my UI actually changes. Many thanks! :)

      PS: How do I change the topic to solved?

      JohanSoloJ 1 Reply Last reply
      0
      • T TheHawk

        How could I do that?

        If I do this in mainwindow.cpp:

            codewindow = new CustomCode;
            codewindow->setParent(this);
            connect(codewindow,SIGNAL(send_test()),SLOT(test()));
            codewindow->show();
        

        This makes it compile but instead of a seperate window the current mainwindow gets overwritten with the codewindow and the send_test() is in jsfunctions.h, not in CustomCode.h. Would I have to make a function that calls another function that calls the signal? Or am I missing it here? Sorry

        Edit:

        Succes!

        I used : (in mainwindow.cpp)

        void MainWindow::on_actionAdd_custom_logic_triggered()
        {
            codewindow = new CustomCode;
            QObject::connect(&jsfunction,SIGNAL(send_test()),this,SLOT(test()));
            codewindow->show();
        }
        

        added extern jsfunctions jsfunction; to mainwindow.h and customcode.h
        added jsfunctions jsfunction; in jsfunctions.cpp

        and changed jsfunctions.h to

        signals:
            void send_test();
        
        public slots:
            void call_test()
            {
                emit send_test();
            }
        

        Now it works! test() gets properly called and my UI actually changes. Many thanks! :)

        PS: How do I change the topic to solved?

        JohanSoloJ Offline
        JohanSoloJ Offline
        JohanSolo
        wrote on last edited by
        #7

        I don't think it is necessary to have a static instance of jsfunctions to do this. Why did you add it? As far as I understand, the jsfunctions instance was meant to be used in the CustomCode instance? Assuming my hypothesis is correct, I would do the connect in the CustomCode, where both the MainWindow and jsfunctions instance are used:

        void MainWindow::on_actionAdd_custom_logic_triggered()
        {
            codewindow = new CustomCode( this );
            codewindow->show();
        }
        
        CustomCode::CustomCode( QObject* parent )
            : QMainWindow(parent)
            , ui(new Ui::CustomCode)
            , jsfunction( new jsfunctions() )
        {
            connect( jsfunction, SIGNAL( send_test() ), parent, SLOT( test() ) );
        }
        
        @TheHawk said:
        > PS: How do I change the topic to solved?
        
        You can simply edit the topic title (IIRC by editing your first post),

        `They did not know it was impossible, so they did it.'
        -- Mark Twain

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

          I have no particular reason why I chose it to be static, I did it because after googling some errors it was suggested to use static.

          Anyhow, your code indeed works as well and your assumption was correct! :)
          I do have a little issue now. Calling codewindow = new CustomCode( this ); makes the new window always on top. I previously called codewindow = new CustomCode;, this made the 2 windows independant (if you clicked the one in the back it came to front). I don't really see an option how to fix this behavior besides using signals and slots at a click to bring a window up and lower the other one. Am I missing the obvious here?

          thanks!

          1 Reply Last reply
          0
          • JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on last edited by JohanSolo
            #9

            I think you can tune this behaviour with the correct window flag, which is the second argument of the QMainWindow constructor.

            Edit:
            As another way to do it, you can pass a null pointer to the QMainWindow constructor in the CustomCode constructor, but still use the parent pointer in the connect statement.

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TheHawk
              wrote on last edited by
              #10

              I tried all the window flag settings without succes, it always stays on top.

              How would I pass a null pointer to the QMainWindow constructor in the CustomCode constructor? Can't really get my head around it. Changing the call to:
              codewindow = new CustomCode(0); or codewindow = new CustomCode; creates the desired effect but makes me unable to call the function (since no parent exists I think?).

              Am I even calling the windowflag correctly?

              void MainWindow::on_actionAdd_custom_logic_triggered()
              {
                  CustomCode *codewindow;
                  codewindow = new CustomCode(0);
                  //QObject::connect(&jsfunction,SIGNAL(send_test()),this,SLOT(test()));
                  codewindow->setWindowFlags(Qt::Window);
                  codewindow->show();
              
              }
              
              1 Reply Last reply
              0
              • JohanSoloJ Offline
                JohanSoloJ Offline
                JohanSolo
                wrote on last edited by
                #11

                I was thinking about

                CustomCode::CustomCode( QObject* parent )
                    : QMainWindow( 0 )
                    , ui( new Ui::CustomCode )
                    , jsfunction( new jsfunctions() )
                {
                    connect( jsfunction, SIGNAL( send_test() ), parent, SLOT( test() ) );
                }
                

                `They did not know it was impossible, so they did it.'
                -- Mark Twain

                T 1 Reply Last reply
                1
                • JohanSoloJ JohanSolo

                  I was thinking about

                  CustomCode::CustomCode( QObject* parent )
                      : QMainWindow( 0 )
                      , ui( new Ui::CustomCode )
                      , jsfunction( new jsfunctions() )
                  {
                      connect( jsfunction, SIGNAL( send_test() ), parent, SLOT( test() ) );
                  }
                  
                  T Offline
                  T Offline
                  TheHawk
                  wrote on last edited by
                  #12

                  @JohanSolo

                  Ah yes, seems so obvious now. Works like a charm!

                  Thanks :)

                  1 Reply Last reply
                  0
                  • JohanSoloJ Offline
                    JohanSoloJ Offline
                    JohanSolo
                    wrote on last edited by
                    #13

                    You're welcome! I'm glad I could help.

                    `They did not know it was impossible, so they did it.'
                    -- Mark Twain

                    T 1 Reply Last reply
                    1
                    • JohanSoloJ JohanSolo

                      You're welcome! I'm glad I could help.

                      T Offline
                      T Offline
                      TheHawk
                      wrote on last edited by
                      #14

                      @JohanSolo

                      I got a question though (kinda off-topic). Would it be possible to create a signal from the MainWindow to a slot (or function) inside the javascript script that I evaluate inside CustomCode? So for example:

                      I want to plot a graph using the javascript script. I want that graph to use actual, changing values from mainwindow. So I would like to create a signal that triggers upon a value change and then call a function inside the javascript script to update the graph with the new values.

                      1 Reply Last reply
                      0
                      • JohanSoloJ Offline
                        JohanSoloJ Offline
                        JohanSolo
                        wrote on last edited by
                        #15

                        @TheHawk said:

                        I got a question though (kinda off-topic). Would it be possible to create a signal from the MainWindow to a slot (or function) inside the javascript script that I evaluate inside CustomCode? So for example:

                        Better ask a new question in a new thread, for the future, it helps when looking for a problem on the forum.

                        You are free to define any signal / slot you want, so the short answer is yes.
                        You can emit a signal in MainWindowwhen the data needs to be reprocessed by the js script managed by CustomCode.

                        `They did not know it was impossible, so they did it.'
                        -- Mark Twain

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          TheHawk
                          wrote on last edited by
                          #16

                          My apologies, I will create a new thread. Thanks for all your help! (and putting up with my stuff :) )

                          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