Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signals and Slots - Closed
Qt 6.11 is out! See what's new in the release blog

Signals and Slots - Closed

Scheduled Pinned Locked Moved General and Desktop
15 Posts 4 Posters 9.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            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();
            

            }

            1 Reply Last reply
            0
            • JeroentjehomeJ Offline
              JeroentjehomeJ Offline
              Jeroentjehome
              wrote on last edited by
              #6

              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

              Greetz, Jeroen

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                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.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    As I was saying in my lost message:

                    Why not do the writing in on_pushButton_clicked ? Would be a lot simpler

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • JeroentjehomeJ Offline
                      JeroentjehomeJ Offline
                      Jeroentjehome
                      wrote on last edited by
                      #10

                      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

                      Greetz, Jeroen

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            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

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #14

                              I am trying to update the title to closed but not happening. Will keep trying.

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                Sorry, I meant "solved" not "closed"

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved