Signals and Slots - Closed



  • Hi there,

    For my university assignment, I have created three classes. Film class, FilmGUI class (it is a form that accepts info about the Film) and FilmWriter class (this class writes to a file on the disk).

    When I click on Save button in the FilmGUI class, I want the FilmWriter class to write to the text file on the disk.

    I have declared a signal in the FilmGUI class that emits when the button is clicked. But I am not able to declare a slot in the Main class that would then pass the Film object to the FilmWriter class so that it can write to the file.

    I want to declare a slot in the Main class and connect it with the signal in the FilmGUI class.

    Please assist.

    Thanks


  • Moderators

    what do you have so far? What exactly do you mean "you're not able to declare a slot"?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Have a look at Qt's documentation examples to see how in works.

    As for the Main class, do you mean the main function ? If so, you can't declare a slot in there.

    Just curious, how many are you in that class ?



  • Ok. Let me take it one step at a time.

    Below is the Form that I have made

    #include <QDialog>
    #include "filmwriter.h"

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    QString title;
    QString director;
    int duration;
    QDate releaseDate;

    singals:
    write();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::Dialog *ui;
    };

    #endif // DIALOG_H



  • When I try to compile it I get C3861 error. Identifier not found.

    Below is the CPP file:

    #include "dialog.h"
    #include "ui_dialog.h"
    #include "film.h"
    #include "filmwriter.h"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    }

    Dialog::~Dialog()
    {
    delete ui;
    }

    void Dialog::on_pushButton_clicked()
    {
    title = ui->lineEdit->text();
    director = ui->lineEdit_2->text();
    duration = ui->lineEdit_3->text().toInt();
    releaseDate = ui->dateEdit->date();

     emit write();
    

    }



  • 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


  • Moderators

    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.

    1. I click the button in the GUI class.
    2. In the button clicked event, I make a Film class with the data entered in the form.
    3. When the button is clicked in the GUI class then it should emit a signal to write the info to the file.
    4. 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


  • Lifetime Qt Champion

    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&#40;mFileName&#41;;
    
    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.


  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    Sorry, I meant "solved" not "closed"


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.