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

  • 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:


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


    and the functions are:

       void MainWindow::forward()
      void MainWindow::backwards()

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


    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,

  • 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
        MainWIndow(QWidget *parent=0);
    public slots:
        void forward();
        void backward();
        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()
    void MainWindow::forward()

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

  • Moderators

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

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

  • Hi,

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

  • @koahnig Thank you very much for your answer.

Log in to reply

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