trying to run a Qt4 sample on Qt5 version. getting error!!!
-
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 changedMyClass( 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? -
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 changedMyClass( 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?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
-
A couple of suggestions:
- split the MyClass out into its own myclass.h and myclass.cpp file. Otherwise you will get the dreaded "no vtable" error when linking.
- The constructor
MyClass( const QString &text, QObject *parent = nullptr);
needs a body. Also, you might want to declare it as explicit.