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. Connection between classes.

Connection between classes.

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 648 Views
  • 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.
  • X Offline
    X Offline
    Xilit
    wrote on last edited by Xilit
    #1

    Hi guys! I almost finish my project and there is only one thing to do.

    So there is the problem.

    I have two classes: editBook and mainWindow. When user in form edit some book's information and press the button on_btnEditBook_clicked() then signal emitted with info that user made:

    editbook.h

    signals:
        void sigEditedBookInfoByUser(QVector<QString>&EditedBookInfoByUserToDB);
    

    editbook.cpp

    void EditBook::on_btnEditBook_clicked()
    {
        //Here I create vector with user's info
        // ... code
    
        QVector<QString>editedBookInfoByUser;
    
        // ... code
        
        // And here is the signal that I want to "catch" in mainWindow:
        emit sigEditedBookInfoByUser(editedBookInfoByUser);
    }
    

    And here is mainWindow class.

    mainWindow.h

    private slots:
    void getEditedBookInfoByUserToDB(QVector<QString>&EditedBookInfoByUserToDB);
    
    private:
        Ui::MainWindow *ui;
       /* this is reference that I use in my Windows class when I create edit-book-window  (and this is the reference that I use when I create connect()). */
        EditBook *editBook;
    

    mainWindow.cpp
    Constructor

        connect(&editBook, SIGNAL(EditBook::sigEditedBookInfoByUser(QVector<QString>&)), this, SLOT(getEditedBookInfoByUserToDB(QVector<QString>&)));
    

    And realization of slot:

    void MainWindow::getEditedBookInfoByUserToDB(QVector<QString> &EditedBookInfoByUserToDB)
    {
        /* Here I want to "catch" my QVector with Info from user.*/
    }
    

    So I have errors when I try to run my program. How can I correctly connect this two slots?

    KroMignonK 1 Reply Last reply
    0
    • X Xilit

      Hi guys! I almost finish my project and there is only one thing to do.

      So there is the problem.

      I have two classes: editBook and mainWindow. When user in form edit some book's information and press the button on_btnEditBook_clicked() then signal emitted with info that user made:

      editbook.h

      signals:
          void sigEditedBookInfoByUser(QVector<QString>&EditedBookInfoByUserToDB);
      

      editbook.cpp

      void EditBook::on_btnEditBook_clicked()
      {
          //Here I create vector with user's info
          // ... code
      
          QVector<QString>editedBookInfoByUser;
      
          // ... code
          
          // And here is the signal that I want to "catch" in mainWindow:
          emit sigEditedBookInfoByUser(editedBookInfoByUser);
      }
      

      And here is mainWindow class.

      mainWindow.h

      private slots:
      void getEditedBookInfoByUserToDB(QVector<QString>&EditedBookInfoByUserToDB);
      
      private:
          Ui::MainWindow *ui;
         /* this is reference that I use in my Windows class when I create edit-book-window  (and this is the reference that I use when I create connect()). */
          EditBook *editBook;
      

      mainWindow.cpp
      Constructor

          connect(&editBook, SIGNAL(EditBook::sigEditedBookInfoByUser(QVector<QString>&)), this, SLOT(getEditedBookInfoByUserToDB(QVector<QString>&)));
      

      And realization of slot:

      void MainWindow::getEditedBookInfoByUserToDB(QVector<QString> &EditedBookInfoByUserToDB)
      {
          /* Here I want to "catch" my QVector with Info from user.*/
      }
      

      So I have errors when I try to run my program. How can I correctly connect this two slots?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @Xilit said in Connection between classes.:

      connect(&editBook, SIGNAL(EditBook::sigEditedBookInfoByUser(QVector<QString>&)), this, SLOT(getEditedBookInfoByUserToDB(QVector<QString>&)));

      As far as I can read from code extract, editBook is already a pointer, so right statement is, I guess:
      connect(editBook, &EditBook::sigEditedBookInfoByUser, this, &MainWindow::getEditedBookInfoByUserToDB);

      I prefere new connect() syntax to SIGNAL() / SLOT(), because it is checked at compilation time. This eliminate most of the common errors (invalid syntax, invalid signal or slot name).

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      X 1 Reply Last reply
      1
      • KroMignonK KroMignon

        @Xilit said in Connection between classes.:

        connect(&editBook, SIGNAL(EditBook::sigEditedBookInfoByUser(QVector<QString>&)), this, SLOT(getEditedBookInfoByUserToDB(QVector<QString>&)));

        As far as I can read from code extract, editBook is already a pointer, so right statement is, I guess:
        connect(editBook, &EditBook::sigEditedBookInfoByUser, this, &MainWindow::getEditedBookInfoByUserToDB);

        I prefere new connect() syntax to SIGNAL() / SLOT(), because it is checked at compilation time. This eliminate most of the common errors (invalid syntax, invalid signal or slot name).

        X Offline
        X Offline
        Xilit
        wrote on last edited by
        #3

        @KroMignon

        Hmmm. The program compile now and immediatly crashes.

        In application output:

        18:54:30: Starting E:\Qt\build-BooksCatalogDBSqlite-Desktop_Qt_5_13_1_MinGW_64_bit-Debug\debug\BooksCatalogDBSqlite.exe ...
        
        ASSERT: "c->sender == q_ptr" in file kernel\qobject.cpp, line 391
        
        18:54:33: The program has unexpectedly finished.
        
        18:54:33: The process was ended forcefully.
        
        18:54:33: E:/Qt/build-BooksCatalogDBSqlite-Desktop_Qt_5_13_1_MinGW_64_bit-Debug/debug/BooksCatalogDBSqlite.exe crashed.
        

        What can it be?

        KroMignonK 1 Reply Last reply
        0
        • X Xilit

          @KroMignon

          Hmmm. The program compile now and immediatly crashes.

          In application output:

          18:54:30: Starting E:\Qt\build-BooksCatalogDBSqlite-Desktop_Qt_5_13_1_MinGW_64_bit-Debug\debug\BooksCatalogDBSqlite.exe ...
          
          ASSERT: "c->sender == q_ptr" in file kernel\qobject.cpp, line 391
          
          18:54:33: The program has unexpectedly finished.
          
          18:54:33: The process was ended forcefully.
          
          18:54:33: E:/Qt/build-BooksCatalogDBSqlite-Desktop_Qt_5_13_1_MinGW_64_bit-Debug/debug/BooksCatalogDBSqlite.exe crashed.
          

          What can it be?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @Xilit Stupid question, but have you initialized editBook? (e.g. editBook = new EditBook();) Of course, before the connect() statement!

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          X 1 Reply Last reply
          1
          • KroMignonK KroMignon

            @Xilit Stupid question, but have you initialized editBook? (e.g. editBook = new EditBook();) Of course, before the connect() statement!

            X Offline
            X Offline
            Xilit
            wrote on last edited by
            #5

            @KroMignon

            I change my code to this:

            EditBook *editInfoFromUser;
            connect(editInfoFromUser, &EditBook::sigEditedBookInfoByUser, this, &MainWindow::getEditedBookInfoByUserToDB);
            

            I compile correctly, but in my getEditedBookInfoByUserToDB() function I didn't get any vector array.

            I emulate function work:

            void MainWindow::getEditedBookInfoByUserToDB(QVector<QString> &EditedBookInfoByUserToDB)
            {
                QString title = EditedBookInfoByUserToDB[0];
                QMessageBox::information(this,"Test",title);
            }
            

            After I press edit-button programm don't show any QMessageBox with info from array. It just crashes.

            KroMignonK 1 Reply Last reply
            0
            • X Xilit

              @KroMignon

              I change my code to this:

              EditBook *editInfoFromUser;
              connect(editInfoFromUser, &EditBook::sigEditedBookInfoByUser, this, &MainWindow::getEditedBookInfoByUserToDB);
              

              I compile correctly, but in my getEditedBookInfoByUserToDB() function I didn't get any vector array.

              I emulate function work:

              void MainWindow::getEditedBookInfoByUserToDB(QVector<QString> &EditedBookInfoByUserToDB)
              {
                  QString title = EditedBookInfoByUserToDB[0];
                  QMessageBox::information(this,"Test",title);
              }
              

              After I press edit-button programm don't show any QMessageBox with info from array. It just crashes.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @Xilit said in Connection between classes.:

              EditBook *editInfoFromUser;
              connect(editInfoFromUser, &EditBook::sigEditedBookInfoByUser, this, &MainWindow::getEditedBookInfoByUserToDB)

              This NOT correct.
              You have declared a dangling/uninitialized pointer!!

              auto *editInfoFromUser = new EditBook ();
              connect(editInfoFromUser, &EditBook::sigEditedBookInfoByUser, this, &MainWindow::getEditedBookInfoByUserToDB);
              

              This is correct and will work!

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              X 1 Reply Last reply
              3
              • KroMignonK KroMignon

                @Xilit said in Connection between classes.:

                EditBook *editInfoFromUser;
                connect(editInfoFromUser, &EditBook::sigEditedBookInfoByUser, this, &MainWindow::getEditedBookInfoByUserToDB)

                This NOT correct.
                You have declared a dangling/uninitialized pointer!!

                auto *editInfoFromUser = new EditBook ();
                connect(editInfoFromUser, &EditBook::sigEditedBookInfoByUser, this, &MainWindow::getEditedBookInfoByUserToDB);
                

                This is correct and will work!

                X Offline
                X Offline
                Xilit
                wrote on last edited by Xilit
                #7

                @KroMignon

                Thank you, I change it!

                But situation the same - after I press edit-button programm crashes.

                The good new I guess I understand where the problem is.

                Actually I've already opened the editWindow from mainWindow, and now I need to return info back to mainWindow.

                So the scheme:

                In mainWindow user press button "Edit book" -> editWindow opens ->user make edits and press button "Edit" -> Vector with info comes back to mainWindow.

                So in mainWindow I've already have pointer to editBook class in order to open editWindow from mainWindow.

                mainWindow.h

                private:
                Ui::MainWindow *ui;
                EditBook *editBook;

                mainWindow.cpp
                This is button in mainWindow to open editWindow:

                void MainWindow::on_btnEditBook_clicked()
                {
                   //code
                    editBook->editBookInfo(editBookInfoVector);
                    editBook->show();
                }
                

                So as you can see I've already opened editWindow, so I don't need to create new one by doing this:

                auto *editInfoFromUser = new EditBook ();
                

                Because it opens a new window without any information, and I want to get info back from already opened editWindow from mainWindow.

                So haw can I do my connect() correctly?

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  Xilit
                  wrote on last edited by
                  #8

                  Thank you guys, I soleve my problem. Finally!

                  If it's interesting or maybe usefull to anyone, proble was in incorrect place of connect(). I move it to void MainWindow::on_btnEditBook_clicked(){} from I called to editWindow and it is working! Finally I understand how SnS works.:)

                  Thank you!

                  Pablo J. RoginaP 1 Reply Last reply
                  0
                  • X Xilit

                    Thank you guys, I soleve my problem. Finally!

                    If it's interesting or maybe usefull to anyone, proble was in incorrect place of connect(). I move it to void MainWindow::on_btnEditBook_clicked(){} from I called to editWindow and it is working! Finally I understand how SnS works.:)

                    Thank you!

                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by
                    #9

                    @Xilit said in Connection between classes.:

                    I soleve my problem

                    So please mark your post as such! Thanks

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    X 1 Reply Last reply
                    0
                    • Pablo J. RoginaP Pablo J. Rogina

                      @Xilit said in Connection between classes.:

                      I soleve my problem

                      So please mark your post as such! Thanks

                      X Offline
                      X Offline
                      Xilit
                      wrote on last edited by
                      #10

                      @Pablo-J-Rogina

                      Sure.:)

                      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