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] - Qt - GUI - Problem with Reference / Pointers
Forum Update on Monday, May 27th 2025

[Solved] - Qt - GUI - Problem with Reference / Pointers

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.0k 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.
  • Y Offline
    Y Offline
    yerbaguy
    wrote on 18 May 2015, 18:36 last edited by yerbaguy
    #1

    Hallo,

    I am learning C++ and trying to re-implement / add some code to the application that I gathered from the base of examples of Qt. It's about the Qt C++ GUI application. I have got a problem with sending by a reference a variable to a function ( actually to two functions - to which functions I created the buttons ). In the constructor I defined a variable named 'click' of the int type. Names of these functions are:

             forward();
                and 
              backwards();
    

    I sent the click variable to these functions by reference, like:

        forward(&click);
        backwards(&click);
    

    and the functions are:

       void MainWindow::forward()
      {
            (*click_forward)++;
      }
      void MainWindow::backwards()
      {
            (*click_backwards)--;
      }
    

    In the header file, I defined these two functions as:

            forward();
            backwards();
    

    I also defined the connections SIGNAL-SLOT, as:

         QObject::connect(nextbtn, &QPushButton::clicked, this, &MainWindow::forward());
    
         QObject::connect(beforebtn, &QPushButton::clicked, this, &MainWindow::backwards());
    
    But when I compile the program, I receive an error like:
    
         no matching function for call to 'MainWindow::forward(int*);
    
         no matching function for call to 'MainWindow::backwards(int*);
    

    When I put the parameters into the above functions:

             MainWindow::forward(int *click_forward)
             {
    
             }
           
            MainWindow::backwards(int *click_backwards)
            {
    
            }
    
       I received an error, like:
    
       /opt/Qt/5.4/gcc_64/include/QtCore/qglobal.h:694: błąd: invalid application of 'sizeof' to incomplete type 'QStaticAssertFailure<false>'
     enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) = sizeof(QStaticAssertFailure<!!(Condition)>)}
    
         Could I please ask you to direct me, what I am doing wrong?
    
             Thank you,
                                                                                     ^
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 18 May 2015, 19:06 last edited by mcosta
      #2

      Hi and welcome to devnet,

      Qt is a C++ framework so the first requirement to learn how to use it is to have a good knowledge of the language.

      The first concept is the declaration/definition of a method: in both case the signature of the function MUST be the same.

      In you case you don't neet to pass arguments to your slots

      // MainWindow.h
      class MainWindow: public QWidget
      {
      Q_OBJECT // you need it to use signals/slots
      public:
          MainWIndow(QWidget *parent=0);
      
      public slots:
          void forward();
          void backward();
      
      private:
          int _click;
      };
      
      // MainWindow.cpp
      
      MainWindow::MainWindow(QWidget* parent): QWidget(parent)
      {
      .....
          connect(nextbtn, &QPushButton::clicked, this, &MainWindow::forward);
          connect(beforebtn, &QPushButton::clicked, this, &MainWindow::backward);
      }
      
      void MainWindow::backward()
      {
          _click--;
      }
      
      void MainWindow::forward()
      {
          _click++;
      }
      

      BTW, I suggest to learn a little bit more the language before to go forward with Qt

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      Y 1 Reply Last reply 18 May 2015, 19:50
      0
      • K Offline
        K Offline
        koahnig
        wrote on 18 May 2015, 19:11 last edited by koahnig
        #3

        Hi and welcome to devnet

        You are doing a couple of things not correct.

        My personal recommendation would be to start with C++ tutorial (e.g. this one, but there are many others) first. The GUI stuff and especially signals and slots make it more difficult. It will be a very frustrating experience when you continue to plow along those lines as you do at the moment.

        void foo(int &ref)  // here we have a reference 
        {
             ++ref;
        }
        
        void foo1(int *ptr) // here we have a ponter to an int
        {
             *ptr += 1;   // increments by one 
        }
        
        void main ( int argc, char **argv) 
        {
             int i;
             foo(i);  // calls by reference 
        
            foo1(&i);  // here the '&' does get the address/pointer 
        }
        

        Those are really the basics in a very tiny nutshell. Both functions do basically the same incrementing by one and also passing back the value. You should go through a tutorial and look especially for those differences in detail.

        Vote the answer(s) that helped you to solve your issue(s)

        Y 1 Reply Last reply 18 May 2015, 20:04
        0
        • M mcosta
          18 May 2015, 19:06

          Hi and welcome to devnet,

          Qt is a C++ framework so the first requirement to learn how to use it is to have a good knowledge of the language.

          The first concept is the declaration/definition of a method: in both case the signature of the function MUST be the same.

          In you case you don't neet to pass arguments to your slots

          // MainWindow.h
          class MainWindow: public QWidget
          {
          Q_OBJECT // you need it to use signals/slots
          public:
              MainWIndow(QWidget *parent=0);
          
          public slots:
              void forward();
              void backward();
          
          private:
              int _click;
          };
          
          // MainWindow.cpp
          
          MainWindow::MainWindow(QWidget* parent): QWidget(parent)
          {
          .....
              connect(nextbtn, &QPushButton::clicked, this, &MainWindow::forward);
              connect(beforebtn, &QPushButton::clicked, this, &MainWindow::backward);
          }
          
          void MainWindow::backward()
          {
              _click--;
          }
          
          void MainWindow::forward()
          {
              _click++;
          }
          

          BTW, I suggest to learn a little bit more the language before to go forward with Qt

          Y Offline
          Y Offline
          yerbaguy
          wrote on 18 May 2015, 19:50 last edited by
          #4

          @mcosta Thank you very much for your answer, I know that I have to learn C++ much more, but the thing is, that I would like to implement the GUI application - where users would have the manual ability to interact with the program. I understand that, frameworks themselves have got a lot of things which you have to deal with - I mean probably more that the language (C++) itself. May be you could suggest any other framework for C++ which I could use under Linux just to try to implement the code (GUI) and in the same time learn C++, or just to learn faster C++ and Qt? Once again, thank you.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on 18 May 2015, 19:55 last edited by
            #5

            Hi,

            in my opinion you should before learn more the language and after start with GUI frameworks (obviously I suggest Qt).

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            0
            • K koahnig
              18 May 2015, 19:11

              Hi and welcome to devnet

              You are doing a couple of things not correct.

              My personal recommendation would be to start with C++ tutorial (e.g. this one, but there are many others) first. The GUI stuff and especially signals and slots make it more difficult. It will be a very frustrating experience when you continue to plow along those lines as you do at the moment.

              void foo(int &ref)  // here we have a reference 
              {
                   ++ref;
              }
              
              void foo1(int *ptr) // here we have a ponter to an int
              {
                   *ptr += 1;   // increments by one 
              }
              
              void main ( int argc, char **argv) 
              {
                   int i;
                   foo(i);  // calls by reference 
              
                  foo1(&i);  // here the '&' does get the address/pointer 
              }
              

              Those are really the basics in a very tiny nutshell. Both functions do basically the same incrementing by one and also passing back the value. You should go through a tutorial and look especially for those differences in detail.

              Y Offline
              Y Offline
              yerbaguy
              wrote on 18 May 2015, 20:04 last edited by
              #6

              @koahnig Thank you very much for your answer.

              1 Reply Last reply
              0

              1/6

              18 May 2015, 18:36

              • Login

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