Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unresolved external Symbol formInterface::forminterface => Constructor

    General and Desktop
    2
    14
    2348
    Loading More Posts
    • 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.
    • Flaming Moe
      Flaming Moe last edited by

      Hi there,

      i guess i´m doing steps Forward by taking the calculator example as a reference.
      I´m trying to Change a variable in a class with the spinbox of a widget
      I made

      • a simple widget with only one spinbox and called it "Form" with the creator

      • made a class "formInterface" with a SLOT to set an internal Variable

      • connect Form´s SIGNAL valueChanged and formInterface´s SLOT SetVal();

      • checked if everything is listed in the .pro File

      As the Headline says i´m getting the error
      unresolved external Symbol formInterface::forminterface
      which seems to me to be the constructor. I don´t understand what is missing.

      best regards

      @#ifndef MAIN_H
      #define MAIN_H
      #include <QApplication>
      #include "form.h"
      #include "forminterface.h"
      #include <iostream>

      class main
      {
      public:
      main();
      };

      #endif // MAIN_H
      @
      @#include "main.h"
      using namespace std;

      void main(int argc, char *argv[])
      {

      QApplication app(argc, argv);
      Form *test = new Form;
      formInterface *FI = new formInterface;
      QObject::connect(test, SIGNAL(valueChanged(int)), FI, SLOT(updateVal&#40;int&#41;&#41;);
      test->show();
      cout << "TEST\n";
      app.exec&#40;&#41;;
      

      }
      @
      @#ifndef FORM_H
      #define FORM_H

      #include <QWidget>

      namespace Ui {
      class Form;
      }

      class Form : public QWidget
      {
      Q_OBJECT

      public:
      explicit Form(QWidget *parent = 0);
      ~Form();

      private:
      Ui::Form *ui;
      };

      #endif // FORM_H
      @
      @#include "form.h"
      #include "ui_form.h"

      Form::Form(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::Form)
      {
      ui->setupUi(this);
      }

      Form::~Form()
      {
      delete ui;
      }
      @
      @#ifndef FORMINTERFACE_H
      #define FORMINTERFACE_H

      class formInterface : public QObject
      {
      Q_OBJECT
      public:
      formInterface();
      public slots :
      void updateVal(int i);

      private:

      int val;
      

      };

      #endif // FORMINTERFACE_H
      @
      @#include "forminterface.h"
      #include <iostream>

      formInterface::formInterface()
      {
      }

      void formInterface::updateVal(int i)
      {
      val = i;
      std::cout << i << "\n";
      }
      @
      @FORMS +=
      form.ui

      HEADERS +=
      form.h
      main.h
      forminterface.h

      SOURCES +=
      form.cpp
      main.cpp
      forminterface.cpp
      QT += core gui widgets
      CONFIG += qt
      @

      A lovely day for a ̶g̶̶u̶̶i̶̶n̶̶n̶̶e̶̶s̶ DUFF^^

      1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        You need to initialise the base class (QObject) in your formInterface class, like this:
        @
        formInterface::formInterface() : QObject()
        @

        Or, even better, do it the Qt way and include parent info in the constructor:
        @
        // H
        public:
        formInterface(QObject *parent = 0);

        // CPP
        formInterface::formInterface(QObject *parent) : QObject(parent)
        @

        (Z(:^

        1 Reply Last reply Reply Quote 0
        • Flaming Moe
          Flaming Moe last edited by

          Hey hey,

          i also included <QObject> to formInterface.h and tryed both variants, but still getting the error massage.

          A lovely day for a ̶g̶̶u̶̶i̶̶n̶̶n̶̶e̶̶s̶ DUFF^^

          1 Reply Last reply Reply Quote 0
          • sierdzio
            sierdzio Moderators last edited by

            If you are pasting the error message here exactly as it is printed, then the matter is simple: you have a typo in the constructor name (forminterface instead of formInterface).

            (Z(:^

            1 Reply Last reply Reply Quote 0
            • Flaming Moe
              Flaming Moe last edited by

              Mh... i set up a new Project and copyed everything from the first one and then it was ok.

              Since i want to Change a variable inside formInterface.cpp i gave it a Slot "updateValue(int) and gave the Form.h a Signal valueChanged(int)
              and connected them in main
              QObject::connect(test, SIGNAL(valueChanged(int)), FI, SLOT(updateVal(int));
              The updateValue function is
              @
              void formInterface::updateVal(int i)
              {
              val = i;
              std::cout << val << "\n";
              }
              @
              so, everytime the function is triggered i should get an Output, what doesn´t happen. But during compiling i don´t get warnings, that Connection failed.
              Also a cout command in the constructor Comes to Action, when i cancel the window when running...
              Complete Code beneath

              main:
              @#include "main.h"
              using namespace std;

              void main(int argc, char *argv[])
              {

              QApplication app(argc, argv&#41;;
              Form *test = new Form;
              formInterface *FI = new formInterface;
              QObject::connect(test, SIGNAL(valueChanged(int)), FI, SLOT(updateVal&#40;int&#41;&#41;&#41;;
              test->show();
              cout << "TEST\n";
              app.exec&#40;&#41;;
              

              }
              @
              Form.h
              @#ifndef FORM_H
              #define FORM_H

              #include <QWidget>

              namespace Ui {
              class Form;
              }

              class Form : public QWidget
              {
              Q_OBJECT

              public:
              explicit Form(QWidget *parent = 0);
              ~Form();

              private:
              Ui::Form *ui;
              signals:
              void valueChanged(int);
              };

              #endif // FORM_H
              @
              Form.cpp
              @#include "form.h"
              #include "ui_form.h"

              Form::Form(QWidget *parent) :
              QWidget(parent),
              ui(new Ui::Form)
              {
              ui->setupUi(this);
              }

              Form::~Form()
              {
              delete ui;
              }
              @
              forminterface.h
              @#ifndef FORMINTERFACE_H
              #define FORMINTERFACE_H
              #include <QObject>
              class formInterface : public QObject
              {
              Q_OBJECT
              public:
              formInterface();
              public slots :
              void updateVal(int i);

              private:

              int val;
              

              };

              #endif // FORMINTERFACE_H
              @
              formInterface,cpp
              @#include "forminterface.h"
              #include <iostream>

              formInterface::formInterface(): QObject()
              {
              std::cout << "formInterface Konstruktor "<< val << "\n";
              }

              void formInterface::updateVal(int i)
              {
              val = i;
              std::cout << val << "\n";
              }
              @

              A lovely day for a ̶g̶̶u̶̶i̶̶n̶̶n̶̶e̶̶s̶ DUFF^^

              1 Reply Last reply Reply Quote 0
              • sierdzio
                sierdzio Moderators last edited by

                Connections are created at runtime. Check out the console output when the application is running (compiled in debug mode!).

                Just to be sure, please use qDebug() instead of std::cout.

                In order to updateVal() to be triggered, you need to emit the valueChanged() signal somewhere in Form class. I don't see any place where you do that.

                (Z(:^

                1 Reply Last reply Reply Quote 0
                • Flaming Moe
                  Flaming Moe last edited by

                  At first I tryed to have a member function wich Points to the valueChanged() of the ui

                  @
                  void Form::valueChanged(int i)
                  {
                  ui->spinBox->valueChanged(i);
                  }@

                  But when i´m commenting this code in, i get the error,

                  moc_form.obj:-1: error: LNK2005: "public: void __thiscall Form::valueChanged(int)" (?valueChanged@Form@@QAEXH@Z) is allready defined in form.obj

                  and plus i have a book which says that code from Signal methods is written by the MOC

                  A lovely day for a ̶g̶̶u̶̶i̶̶n̶̶n̶̶e̶̶s̶ DUFF^^

                  1 Reply Last reply Reply Quote 0
                  • sierdzio
                    sierdzio Moderators last edited by

                    You have to specify a place where this signal will be emitted. The rest of the magic is done by MOC, indeed. But you need to tell it when to run. This is done by using the emit keyword (not really, but don't worry about that right now).

                    You should not define a function that has the same name as your signal! The code you posted in your last post is wrong, and that is precisely why you are not getting any output. The spinbox will emit the signal for you, all you need to do is to connect it to your slot. You do not need to define a signal in your Form class.

                    (Z(:^

                    1 Reply Last reply Reply Quote 0
                    • Flaming Moe
                      Flaming Moe last edited by

                      Originally i didn´t had that Signal method in the form.cpp

                      So i guess this
                      @
                      QObject::connect(test, SIGNAL(valueChanged(int)), FI, SLOT(updateVal(int)));
                      @
                      is wrong and to connect the spinBoxe´s valueChanged somehow i Need to reference the spinbox via the form variable?
                      I tryed like this test->ui->... but originally the ui is a private pointer.

                      A lovely day for a ̶g̶̶u̶̶i̶̶n̶̶n̶̶e̶̶s̶ DUFF^^

                      1 Reply Last reply Reply Quote 0
                      • sierdzio
                        sierdzio Moderators last edited by

                        Something like this would work, but would require a bit of refactoring:
                        @
                        connect(ui->spinBox, SIGNAL(valueChanged(int)), formInterfacePointer, SLOT(updateVal(int)));
                        @

                        Don't worry, I know signals and slots can be confusing in the beginning, but knowing them is well worth the effort.

                        You could also create a signal chain and thus expose the spinbox's signal through your Form class, but that's a lesson for later.

                        (Z(:^

                        1 Reply Last reply Reply Quote 0
                        • Flaming Moe
                          Flaming Moe last edited by

                          I also tryed this
                          @
                          QObject::connect(test->ui->spinBox, SIGNAL(valueChanged(int)), FI, SLOT(updateVal(int)));
                          @
                          but getting the warning, "Usagae of undefined Type Ui::Form" and i should take a look at the declaration. wich is done in Form.h
                          and that "left of ->spinBox must be a pointer to a Class/Struct/Uonion/generic Type"

                          @
                          #ifndef FORM_H
                          #define FORM_H

                          #include <QWidget>

                          namespace Ui {
                          class Form;
                          }

                          class Form : public QWidget
                          {
                          Q_OBJECT

                          public:
                          explicit Form(QWidget *parent = 0);
                          ~Form();
                          Ui::Form *ui;
                          private:

                          signals:
                          void valueChanged(int);

                          };@

                          A lovely day for a ̶g̶̶u̶̶i̶̶n̶̶n̶̶e̶̶s̶ DUFF^^

                          1 Reply Last reply Reply Quote 0
                          • sierdzio
                            sierdzio Moderators last edited by

                            [quote author="Flaming Moe" date="1409809494"]but getting the warning, "Usagae of undefined Type Ui::Form" and i should take a look at the declaration. wich is done in Form.h
                            and that "left of ->spinBox must be a pointer to a Class/Struct/Uonion/generic Type"[/quote]

                            That is because Ui class is not visible from your main() routine. As said, it does take a bit of getting used to.

                            (Z(:^

                            1 Reply Last reply Reply Quote 0
                            • Flaming Moe
                              Flaming Moe last edited by

                              Ok, i think i got it.

                              I throwed out the formInterface.h/.cpp and do everything in form.h/.cpp
                              and connect the Signal and the Slot in the constructor.

                              Is the qt Application blocking the outputstream via cout? I get the Output when i Close the app´s window.

                              main:
                              @#include "main.h"
                              using namespace std;

                              void main(int argc, char *argv[])
                              {
                              QApplication app(argc, argv);
                              Form *test = new Form;
                              //formInterface *FI = new formInterface;
                              test->show();
                              cout << "TEST\n";
                              app.exec();
                              }@
                              form.h:
                              @#ifndef FORM_H
                              #define FORM_H
                              #include <iostream>
                              #include <QWidget>

                              namespace Ui {
                              class Form;
                              }

                              class Form : public QWidget
                              {
                              Q_OBJECT

                              public:
                              explicit Form(QWidget *parent = 0);
                              ~Form();
                              Ui::Form *ui;

                              public slots :
                              void updateVal(int i);

                              signals:
                              void valueChanged(int);

                              private:
                              QWidget window;
                              int val;
                              };

                              #endif // FORM_H
                              @
                              form.cpp:
                              @#include "form.h"
                              #include "ui_form.h"

                              Form::Form(QWidget *parent) :
                              QWidget(parent),
                              ui(new Ui::Form)
                              {
                              ui->setupUi(this);
                              QObject::connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(updateVal(int)));
                              window.setWindowTitle("Hallo Qt");
                              window.setGeometry(30,30,200,200);
                              }

                              void Form::updateVal(int i)
                              {
                              val = i;
                              if(val == 5) window.show();
                              if(val == 7) window.close();
                              std::cout << val << "\n";
                              }

                              Form::~Form()
                              {
                              delete ui;
                              }
                              @

                              A lovely day for a ̶g̶̶u̶̶i̶̶n̶̶n̶̶e̶̶s̶ DUFF^^

                              1 Reply Last reply Reply Quote 0
                              • sierdzio
                                sierdzio Moderators last edited by

                                Yes, it can happen. Better use qDebug().

                                (Z(:^

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post