PDA

View Full Version : error: collect2: ld returned 1 exit status while implementing a simple signal/slot



htroyo
2nd May 2011, 03:18
hello, i posted some days ago a problem with that error, but i noticed that i have to use it whit a .h and a separated .cpp, but know i cant find the solution, i just want to create a program to read, edit and update a file, but if i use the Q_OBJECT macro i get that error, i tried include the QObject header,including all widget classes in .h and tried in .cpp and i always get that error, here is my code

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QObject>
#include <QFile>
#include <QDataStream>
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QWidget>

class MainWindow : public QWidget {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
private slots:
void openfile();
void updatefile();
private:
QLabel *state;
QLineEdit *filePath;
QTextEdit *fileContent;
QPushButton *open_create;
QPushButton *updateFile;
QFile *file;
QDataStream *ds;
};

#endif // MAINWINDOW_H


and here my constructor

MainWindow::MainWindow(QWidget *parent) : QWidget(parent) {


resize(600,500);

filePath = new QLineEdit(".\\test.txt",this);
filePath->setGeometry(10,10,400,20);

open_create = new QPushButton("Open/Create",this);
open_create->setGeometry(420,10,90,20);
QObject::connect(open_create,SIGNAL(clicked()),thi s,SLOT(openfile()));

updateFile = new QPushButton("Update file",this);
updateFile->setGeometry(520,10,70,20);

state = new QLabel("Ready",this);
state->setGeometry(10,40,500,50);
state->setWordWrap(true);
state->setAlignment(Qt::AlignTop);

fileContent = new QTextEdit(this);;
fileContent->setGeometry(10,100,580,390);
}

meazza
2nd May 2011, 08:31
check what you get under "Compile Output" during your build

squidge
2nd May 2011, 10:49
Ensure you run qmake.

If you want the .h and .cpp in the same file, add #include "filename.moc" to your cpp.

htroyo
2nd May 2011, 21:42
here it is the compiler output, i dont understand the error message...
and if i remove the Q_OBJECT macro it builds correctly, but signals doesnt work

squidge
2nd May 2011, 22:59
Yes, that error message means a missing moc, see my post above.

Q_OBJECT is required for signals to function.

Did you run qmake?

htroyo
2nd May 2011, 23:59
hello, thanks for replying, i dont know so much about qmake command, but i right clicked on project name and picked run qmake, and it just told me that process exited normally, i rebuilded the project and i got the same error

htroyo
3rd May 2011, 20:23
any suggestion please?

squidge
3rd May 2011, 20:33
You could post your complete output log so we can see if the moc files are being compiled and linked.

Or you could post your code in an archived format and we can see if we can replicate the problem.

htroyo
3rd May 2011, 23:47
well, thanks everyone for replying... i don't know what happened, but i just copied the .h and .cpp files, then i added them to a new project and it builds correctly and signals work ok, i don't know why and how but i works hahahaah
thanks anyway