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. trying to run a Qt4 sample on Qt5 version. getting error!!!

trying to run a Qt4 sample on Qt5 version. getting error!!!

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 248 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.
  • N Offline
    N Offline
    nullbuil7
    wrote on last edited by
    #1

    hello. I am currently studying c++/Qt . I'm currently reading foundation of Qt development by Johan Thelin. but since the book is for Qt4 I'm forced to adapt and make changes to the source code for it to run; but I've hit a roadblock.
    currently this is the sample code i wrote using the book:

    #include <QtWidgets>
    
    #include <QString>
    #include <QObject>
    
    class MyClass : public QObject {
        Q_OBJECT
    
    public:
        MyClass( const QString &text, QObject *parent = nullptr);
    
        const QString& text() const;
        int getLengthOfText() const;
    
    public slots:
        void setText( const QString &text);
    
    signals:
        void textChanged( const QString& );
    
    private:
        QString m_text;
    };
    
    void MyClass::setText( const QString &text )
    {
        if( m_text == text)
            return;
    
        m_text = text;
        emit textChanged( m_text );
    }
    
    
    int
    main(int argc, char **argv)
    {
        QApplication app( argc, argv );
    
        QObject parent;
        MyClass *a, *b, *c;
    
        a = new MyClass( "foo", &parent );
        b = new MyClass( "bar", &parent );
        c = new MyClass( "baz", &parent );
    
        QWidget widget;
        QLineEdit *lineEdit = new QLineEdit;
        QLabel *label = new QLabel;
    
        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget( lineEdit );
        layout->addWidget( label );
        widget.setLayout( layout );
    
        MyClass *bridge = new MyClass( "", &app );
    
        QObject::connect(
                    lineEdit, SIGNAL(textChanged(const QString&)),
                    bridge, SLOT(setText(const QString&)) );
        QObject::connect(
                    bridge, SIGNAL(textChanged(const QString&)),
                    label, SLOT(setText(const QString&)) );
        widget.show();
    
        return app.exec();
    }
    
    

    I've replaced QtGui to QtWidgets in includes and also changed

    MyClass( const QString &text, QObject *parent = 0);
    

    to

    MyClass( const QString &text, QObject *parent = nullptr);
    

    these are the warnings and errors i get:
    variable a, b, c in this line are not used :

    MyClass *a, *b, *c;
    

    these three lines give undefined reference error:

        a = new MyClass( "foo", &parent );
        b = new MyClass( "bar", &parent );
        c = new MyClass( "baz", &parent );
    

    also this line gives undefined reference too:

        MyClass *bridge = new MyClass( "", &app );
    

    needless to say the tutorial sample code doesn't run. how do i fix this?
    is this error from the cpp compiler or Qt framework error?

    aha_1980A 1 Reply Last reply
    0
    • N nullbuil7

      hello. I am currently studying c++/Qt . I'm currently reading foundation of Qt development by Johan Thelin. but since the book is for Qt4 I'm forced to adapt and make changes to the source code for it to run; but I've hit a roadblock.
      currently this is the sample code i wrote using the book:

      #include <QtWidgets>
      
      #include <QString>
      #include <QObject>
      
      class MyClass : public QObject {
          Q_OBJECT
      
      public:
          MyClass( const QString &text, QObject *parent = nullptr);
      
          const QString& text() const;
          int getLengthOfText() const;
      
      public slots:
          void setText( const QString &text);
      
      signals:
          void textChanged( const QString& );
      
      private:
          QString m_text;
      };
      
      void MyClass::setText( const QString &text )
      {
          if( m_text == text)
              return;
      
          m_text = text;
          emit textChanged( m_text );
      }
      
      
      int
      main(int argc, char **argv)
      {
          QApplication app( argc, argv );
      
          QObject parent;
          MyClass *a, *b, *c;
      
          a = new MyClass( "foo", &parent );
          b = new MyClass( "bar", &parent );
          c = new MyClass( "baz", &parent );
      
          QWidget widget;
          QLineEdit *lineEdit = new QLineEdit;
          QLabel *label = new QLabel;
      
          QVBoxLayout *layout = new QVBoxLayout;
          layout->addWidget( lineEdit );
          layout->addWidget( label );
          widget.setLayout( layout );
      
          MyClass *bridge = new MyClass( "", &app );
      
          QObject::connect(
                      lineEdit, SIGNAL(textChanged(const QString&)),
                      bridge, SLOT(setText(const QString&)) );
          QObject::connect(
                      bridge, SIGNAL(textChanged(const QString&)),
                      label, SLOT(setText(const QString&)) );
          widget.show();
      
          return app.exec();
      }
      
      

      I've replaced QtGui to QtWidgets in includes and also changed

      MyClass( const QString &text, QObject *parent = 0);
      

      to

      MyClass( const QString &text, QObject *parent = nullptr);
      

      these are the warnings and errors i get:
      variable a, b, c in this line are not used :

      MyClass *a, *b, *c;
      

      these three lines give undefined reference error:

          a = new MyClass( "foo", &parent );
          b = new MyClass( "bar", &parent );
          c = new MyClass( "baz", &parent );
      

      also this line gives undefined reference too:

          MyClass *bridge = new MyClass( "", &app );
      

      needless to say the tutorial sample code doesn't run. how do i fix this?
      is this error from the cpp compiler or Qt framework error?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @nullbuil7,

      Can you show your *.pro file?

      Have you added the C++ file for MyClass to it?

      Also note, that posting the exact error message will help us to help you faster.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      2
      • mranger90M Offline
        mranger90M Offline
        mranger90
        wrote on last edited by
        #3

        A couple of suggestions:

        1. split the MyClass out into its own myclass.h and myclass.cpp file. Otherwise you will get the dreaded "no vtable" error when linking.
        2. The constructor
        MyClass( const QString &text, QObject *parent = nullptr);
        

        needs a body. Also, you might want to declare it as explicit.

        1 Reply Last reply
        3

        • Login

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