Solved Connection between classes.
-
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
Constructorconnect(&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?
-
@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).
-
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?
-
@Xilit Stupid question, but have you initialized editBook? (e.g.
editBook = new EditBook();
) Of course, before the connect() statement! -
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. -
@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!
-
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? -
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!
-
@Xilit said in Connection between classes.:
I soleve my problem
So please mark your post as such! Thanks
-
Sure.:)