Signals and Slots - Closed
-
Hi, welcome to devnet!
A quick comment about your post, for code examples/post always use the code insert option, that is"@code@" so it becomes readable to other programmers.
Second when stating an error, the compiler usually gives a line number where the error is detected. That shortens searching for us.
Did you read the tutorial of signal/slots? "here!":http://qt-project.org/doc/qt-5.0/qtcore/signalsandslots.html -
oh no ... post messages got lost again ... :/
To your question where you should define a slot:
Do this in every QObject subclass. In your case probably in the FilmWriter class. -
Ok. Lets say I do define the slot in FilmWriter.
- I click the button in the GUI class.
- In the button clicked event, I make a Film class with the data entered in the form.
- When the button is clicked in the GUI class then it should emit a signal to write the info to the file.
- First I need to create the FilmWriter class to use its slot. The FilmWriter class takes a Film as parameter in the constructor.
My question then is where should I do this part?
Thanks
-
As I was saying in my lost message:
Why not do the writing in on_pushButton_clicked ? Would be a lot simpler
-
Hi,
You can't emit a signal to a slot that's not there yet. If you still need to create the FilmWriter class the slot will not exist when the signal is emitted. Like SGalst says is probably the easiest way to do so. In the on_pushbutton create a FilmWriter class (function scope), handle the write to file there, and exit the function (FilmWriter class) get's deleted.
Greetz -
Thanks guys done that. Now a new problem.
The header for FilmWriter:
@#include <QtCore>
#include <QTextStream>
#include <QFile>
#include <QString>
#include "film.h"
class FilmWriter
{
public:
FilmWriter();
FilmWriter(Film myFilm);private:
};
@CPP for filmWriter
@FilmWriter::FilmWriter(Film myyFilm){
Film myFilm = myyFilm; QString mFileName = "F:/myFilms.txt"; //File to store the Films QFile mFile(mFileName); if (!mFile.open(QFile::WriteOnly | QFile::Text)) { qDebug()<< "Cannot open File"; return; } QTextStream out(&mFile); out<< myFilm.getDirector(); out<< myFilm.getDuration(); out<< myFilm.getTitle(); mFile.flush(); mFile.close();
}@
I am getting two errors:
c:\qt\qt5.0.2\tools\qtcreator\bin\assignment1ques1\filmwriter.h:17: error: C2061: syntax error : identifier 'Film'
c:\qt\qt5.0.2\tools\qtcreator\bin\assignment1ques1\filmwriter.h:17: error: C2535: 'FilmWriter::FilmWriter(void)' : member function already defined or declared
Please help.
-
Sorted. Circular dependency. I had declared a FilmWriter class in Film class and Film class in FilmWriter Class.
Thanks all for the help. Much appreciated.
-
If it's all good now, don't forget to update the thread's title to closed so other forum users may know a solution has been found
-
I am trying to update the title to closed but not happening. Will keep trying.
-
Sorry, I meant "solved" not "closed"