[Qt 4] Signale und Slots bei eigenen Klassen



  • Hallo zusammen,

    ich habe heute angefangen, ein Qt-Buch vom Galileo Computing-Verlag zu lesen und bin leider schon auf ein Problem gestoßen. Folgender Code kompiliert nicht richtig.

    Tut mir leid, dass ich hier alles vollspamme, aber ich weiß nicht, wo der Fehler liegt.

    myclass.h

    #ifndef MYCLASS_H
    #define MYCLASS_H
    #include <QObject>
    
    class MyClass : public QObject {
    	        Q_OBJECT
    	public:
    		MyClass();
    		int value() const {return val;}
    	public slots:
    		void setValue(int);
    	signals:
    		void valueChanged(int);
    	private:
    		int val;
    };
    #endif
    

    myclass.cpp

    #include "myclass.h"
    
    MyClass::MyClass() {
    	value = 0;
    }
    
    void MyClass::setValue(int v) {
    	if(v!=val) {
    		val = v;
    		emit valueChanged(v);
    	}
    }
    

    main.cpp

    #include "myclass.h"
    #include <QApplication>
    #include <QMessageBox>
    #include <QString>
    
    void MyMessageBox(MyClass& a, MyClass& b, QString title) {
    	QString Qstr, Qval;
    
    	Qstr.append(title); Qstr.append("\na: ");
    	Qval.setNum(a.value()); Qstr.append(Qval);
    	Qstr.append("\nb: "); Qval.setNum(b.value());
    	Qstr.append(Qval);
    	QMessageBox::information(NULL, "MyClass Information", Qstr, QMessageBox::Ok);
    }
    
    int main(int argc, char *argv[]) {
    	QApplication app(argc, argv);
    	MyClass *a = new MyClass();
    	MyClass *b = new MyClass();
    
    	QObject::connect(a, SIGNAL(valueChanged(int)), b, SLOT(setValue(int)));
    
    	b->setValue(100);
    	MyMessageBox(*a, *b, "b->setValue(100)");
    	a->setValue(99);
    	MyMessageBox(*a, *b, "a->setValue(99)");
    	return 0;
    }
    

    Hab versucht, das Ganze mit "qmake -project && qmake && make" zu kompilieren. Dabei kommt leider immer folgendes raus:

    $ qmake -project && qmake && make
    g++ -c -pipe -Wall -W -O2 -D_REENTRANT -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -DQT_SHARED -DQT_TABLET_SUPPORT -I/usr/share/qt3/mkspecs/default -I. -I. -I/usr/include/qt3 -o main.o main.cpp
    In file included from main.cpp:1:
    myclass.h:3:19: error: QObject: Datei oder Verzeichnis nicht gefunden
    main.cpp:2:24: error: QApplication: Datei oder Verzeichnis nicht gefunden
    main.cpp:3:23: error: QMessageBox: Datei oder Verzeichnis nicht gefunden
    main.cpp:4:19: error: QString: Datei oder Verzeichnis nicht gefunden
    myclass.h:5: error: expected class-name before ‘{’ token
    myclass.h:8: error: ‘Q_OBJECT’ does not name a type
    myclass.h:11: error: expected :' before ‘slots’ myclass.h:12: error: expected primary-expression before ‘void’ myclass.h:12: error: ISO C++ forbids declaration of ‘slots’ with no type myclass.h:12: error: expected ‘;’ before ‘void’ myclass.h:14: error: expected primary-expression before ‘void’ myclass.h:14: error: ISO C++ forbids declaration of ‘signals’ with no type myclass.h:14: error: expected ‘;’ before ‘void’ main.cpp:6: error: ‘QString’ has not been declared main.cpp: In function ‘void MyMessageBox(MyClass&, MyClass&, int)’: main.cpp:7: error: ‘QString’ was not declared in this scope main.cpp:7: error: expected;' before ‘Qstr’
    main.cpp:9: error: ‘Qstr’ was not declared in this scope
    main.cpp:10: error: ‘Qval’ was not declared in this scope
    main.cpp:13: error: ‘QMessageBox’ has not been declared
    main.cpp:13: error: ‘NULL’ was not declared in this scope
    main.cpp:13: error: ‘QMessageBox’ has not been declared
    main.cpp: In function ‘int main(int, char**)’:
    main.cpp:17: error: ‘QApplication’ was not declared in this scope
    main.cpp:17: error: expected `;' before ‘app’
    main.cpp:21: error: ‘QObject’ has not been declared
    main.cpp:21: error: expected primary-expression before ‘int’
    main.cpp:21: error: ‘valueChanged’ was not declared in this scope
    main.cpp:21: error: ‘SIGNAL’ was not declared in this scope
    main.cpp:21: error: expected primary-expression before ‘int’
    main.cpp:21: error: ‘setValue’ was not declared in this scope
    main.cpp:21: error: ‘SLOT’ was not declared in this scope
    main.cpp:23: error: ‘class MyClass’ has no member named ‘setValue’
    main.cpp:24: error: invalid conversion from ‘const char*’ to ‘int’
    main.cpp:24: error: initializing argument 3 of ‘void MyMessageBox(MyClass&, MyClass&, int)’
    main.cpp:25: error: ‘class MyClass’ has no member named ‘setValue’
    main.cpp:26: error: invalid conversion from ‘const char*’ to ‘int’
    main.cpp:26: error: initializing argument 3 of ‘void MyMessageBox(MyClass&, MyClass&, int)’
    main.cpp: At global scope:
    main.cpp:16: warning: unused parameter ‘argc’
    main.cpp:16: warning: unused parameter ‘argv’
    make: *** [main.o] Fehler 1

    Vielen Dank für jede Hilfe! 🙂
    (Hoffe es erbarmt sich einer, diesen ganzen Wust mal anzuschaun 😉 )

    ~moagnus



  • du bindest qt3 und nicht qt4 ein.
    du musst schon die richtigen include-verzeichnisse eingeben. das müssten in deinem fall /usr/include/qt4/Qt, /usr/include/qt4/QtCore und /usr/include/qt4/QtGui sein. alles was qt3 im namen hat, ist in den meisten fällen für qt4 falsch.

    edit nachtrag: probier mal qmake-qt4 anstelle von qmake, was ein symlink auf qmake-qt3 sein sollte.



  • Perfekt. Vielen, vielen Dank! 🙂


Anmelden zum Antworten