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. Signals and Slots error
Forum Updated to NodeBB v4.3 + New Features

Signals and Slots error

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 2.7k Views 3 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.
  • K khanhmg

    I tried to write some code after reading the example of Signals and Slots in Qt document. However, when hit run, I encountered a bunch of errors which basically said that "File not found: main.obj". This is the code:

    #include <iostream>
    #include <qobject.h>
    
    
    class A:public QObject
    {
    Q_OBJECT
    
    public:
        A():m_value(0)
        {
        }
        int value() const {return m_value;}
    public slots:
        void setValue(int value)
        {
            if (value!=m_value)
            {
                m_value=value;
                emit valueChanged(value);
            }
        }
    
    signals:
        void valueChanged(int newValue){};
    
    
    private:
        int m_value;
    };
    
    int main(int argc, char *argv[])
    {
    
        QCoreApplication a(argc, argv);
    
        A a1,b;
        QObject::connect(&a1, SIGNAL(valueChanged(int)),
                         &b, SLOT(setValue(int)));
    
        a1.setValue(6);
        std::cout << a1.value() << b.value();
    
        return a.exec();
    }
    
    A Offline
    A Offline
    AntonZelenin
    wrote on last edited by AntonZelenin
    #2

    @khanhmg
    I don't know where exactly problem is, but I changed your code such way and everything works fine)

    A.h
    #pragma once
    #include <QtCore\qobject.h>
    class A :
    public QObject
    {
    Q_OBJECT

    public:
    A();
    ~A();
    int value();
    public slots :
    void set_value(int value);

    signals:
    void valueChanged(int);
    private:
    int m_value;
    };

    A.cpp
    #include "A.h"

    A::A()
    {
    m_value = 0;
    }

    A::~A()
    {
    }

    void A::set_value(int value)
    {
    if (value != m_value)
    {
    m_value = value;
    emit valueChanged(m_value);
    }
    }

    int A::value()
    {
    return m_value;
    }

    main.cpp
    #include <QtCore\qobject.h>
    #include <QtCore\QDebug>
    #include "A.h"
    #include <QtCore\qcoreapplication.h>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    A *a1 = new A;
    A *b = new A;
    
    a1->set_value(6);
    
    QObject::connect(a1, SIGNAL(valueChanged(int)), b, SLOT(set_value(int)));
    
    qDebug() << a1->value() << b->value();
    
    return a.exec();
    

    }

    1 Reply Last reply
    0
    • K khanhmg

      I tried to write some code after reading the example of Signals and Slots in Qt document. However, when hit run, I encountered a bunch of errors which basically said that "File not found: main.obj". This is the code:

      #include <iostream>
      #include <qobject.h>
      
      
      class A:public QObject
      {
      Q_OBJECT
      
      public:
          A():m_value(0)
          {
          }
          int value() const {return m_value;}
      public slots:
          void setValue(int value)
          {
              if (value!=m_value)
              {
                  m_value=value;
                  emit valueChanged(value);
              }
          }
      
      signals:
          void valueChanged(int newValue){};
      
      
      private:
          int m_value;
      };
      
      int main(int argc, char *argv[])
      {
      
          QCoreApplication a(argc, argv);
      
          A a1,b;
          QObject::connect(&a1, SIGNAL(valueChanged(int)),
                           &b, SLOT(setValue(int)));
      
          a1.setValue(6);
          std::cout << a1.value() << b.value();
      
          return a.exec();
      }
      
      A Offline
      A Offline
      AntonZelenin
      wrote on last edited by AntonZelenin
      #3

      @khanhmg
      Hmm, or almost fine. At least there are no errors. Can't understand, m_value of b object must be 6 too?

      1 Reply Last reply
      0
      • K khanhmg

        I tried to write some code after reading the example of Signals and Slots in Qt document. However, when hit run, I encountered a bunch of errors which basically said that "File not found: main.obj". This is the code:

        #include <iostream>
        #include <qobject.h>
        
        
        class A:public QObject
        {
        Q_OBJECT
        
        public:
            A():m_value(0)
            {
            }
            int value() const {return m_value;}
        public slots:
            void setValue(int value)
            {
                if (value!=m_value)
                {
                    m_value=value;
                    emit valueChanged(value);
                }
            }
        
        signals:
            void valueChanged(int newValue){};
        
        
        private:
            int m_value;
        };
        
        int main(int argc, char *argv[])
        {
        
            QCoreApplication a(argc, argv);
        
            A a1,b;
            QObject::connect(&a1, SIGNAL(valueChanged(int)),
                             &b, SLOT(setValue(int)));
        
            a1.setValue(6);
            std::cout << a1.value() << b.value();
        
            return a.exec();
        }
        
        A Offline
        A Offline
        AntonZelenin
        wrote on last edited by
        #4

        Now I'm confused too, what's wrong with this code? Why slot is not called??

        1 Reply Last reply
        0
        • K khanhmg

          I tried to write some code after reading the example of Signals and Slots in Qt document. However, when hit run, I encountered a bunch of errors which basically said that "File not found: main.obj". This is the code:

          #include <iostream>
          #include <qobject.h>
          
          
          class A:public QObject
          {
          Q_OBJECT
          
          public:
              A():m_value(0)
              {
              }
              int value() const {return m_value;}
          public slots:
              void setValue(int value)
              {
                  if (value!=m_value)
                  {
                      m_value=value;
                      emit valueChanged(value);
                  }
              }
          
          signals:
              void valueChanged(int newValue){};
          
          
          private:
              int m_value;
          };
          
          int main(int argc, char *argv[])
          {
          
              QCoreApplication a(argc, argv);
          
              A a1,b;
              QObject::connect(&a1, SIGNAL(valueChanged(int)),
                               &b, SLOT(setValue(int)));
          
              a1.setValue(6);
              std::cout << a1.value() << b.value();
          
              return a.exec();
          }
          
          A Offline
          A Offline
          AntonZelenin
          wrote on last edited by AntonZelenin
          #5

          @khanhmg
          Hah, so simple. Correct main:
          int main(int argc, char *argv[])
          {
          QCoreApplication a(argc, argv);

          A a1, b;
          QObject::connect(&a1, SIGNAL(valueChanged(int)), &b, SLOT(set_value(int)));
          
          a1.set_value(6);
          
          qDebug() << a1.value() << b.value();
          
          return a.exec();
          

          }

          In my first variant I put QObject::connect(...); after calling set_value function

          1 Reply Last reply
          0
          • K Offline
            K Offline
            khanhmg
            wrote on last edited by
            #6

            After a while, I find out what caused the problem. It's the line where I create the instances of the class. Here is the screenshot:

            alt text

            A 2 Replies Last reply
            0
            • K khanhmg

              After a while, I find out what caused the problem. It's the line where I create the instances of the class. Here is the screenshot:

              alt text

              A Offline
              A Offline
              AntonZelenin
              wrote on last edited by
              #7

              @khanhmg
              So it still does not work? Or what?

              1 Reply Last reply
              0
              • K khanhmg

                After a while, I find out what caused the problem. It's the line where I create the instances of the class. Here is the screenshot:

                alt text

                A Offline
                A Offline
                AntonZelenin
                wrote on last edited by
                #8

                @khanhmg
                And a1 and b objects are underlined just because you created them but not has used, if you mean that (I'm not sure is this sentence correct, if not - sorry for my English)). If you add, for example, a1.set_value(5) - a1 will not be underlined anymore

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  code_fodder
                  wrote on last edited by
                  #9

                  It appears to be a linker error... can you post your .pro file (or is that now sloved?)
                  You have to add the path as well as the file to the .pro file - in case you have not done this :o

                  A 1 Reply Last reply
                  0
                  • C code_fodder

                    It appears to be a linker error... can you post your .pro file (or is that now sloved?)
                    You have to add the path as well as the file to the .pro file - in case you have not done this :o

                    A Offline
                    A Offline
                    AntonZelenin
                    wrote on last edited by
                    #10

                    @code_fodder
                    I think my .pro file will not help you, because I didn't change anything there, I just created new Qt Console Application, there - a new class and copied code, that I posted, that's all.
                    Just in case here is the .pro file:

                    QT += core
                    QT -= gui

                    TARGET = fddcgh //name of a project,
                    CONFIG += console
                    CONFIG -= app_bundle

                    TEMPLATE = app

                    SOURCES += main.cpp
                    a.cpp

                    HEADERS +=
                    a.h

                    C 1 Reply Last reply
                    0
                    • A AntonZelenin

                      @code_fodder
                      I think my .pro file will not help you, because I didn't change anything there, I just created new Qt Console Application, there - a new class and copied code, that I posted, that's all.
                      Just in case here is the .pro file:

                      QT += core
                      QT -= gui

                      TARGET = fddcgh //name of a project,
                      CONFIG += console
                      CONFIG -= app_bundle

                      TEMPLATE = app

                      SOURCES += main.cpp
                      a.cpp

                      HEADERS +=
                      a.h

                      C Offline
                      C Offline
                      code_fodder
                      wrote on last edited by code_fodder
                      #11

                      @AntonZelenin Is that .pro file copied + pasted in exactly? if it is then there is a little issue there. When you add source or header file on new lines you need to use the "line continuation" character, which is the backslash "\" at the end of each line. Just check that you have that : )

                      i.e. like this:

                      SOURCES += main.cpp \
                      a.cpp
                      
                      HEADERS += \
                      a.h
                      
                      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