Slots in the second window?
-
In my main window, I have
QString movie = ui->movie_name->text(); connect(this, SIGNAL(send(QString)), List, SLOT(get_movie(QString))); emit send(movie);And in the List window, I need to have that slot. My lists.h is:
QString getMovieName(QString movie) { return movie; }And in my lists.cpp, I have:
QString a; a = getMovieName(m); ui->label->setText(a); }But, the 'm' variable cannot be used in it as it is not declared. How do I assign the movie name to the variable 'a' in lists.cpp?
-
In my main window, I have
QString movie = ui->movie_name->text(); connect(this, SIGNAL(send(QString)), List, SLOT(get_movie(QString))); emit send(movie);And in the List window, I need to have that slot. My lists.h is:
QString getMovieName(QString movie) { return movie; }And in my lists.cpp, I have:
QString a; a = getMovieName(m); ui->label->setText(a); }But, the 'm' variable cannot be used in it as it is not declared. How do I assign the movie name to the variable 'a' in lists.cpp?
@RootLearner Please read https://doc.qt.io/qt-5/signalsandslots.html
I don't understand what you are doing? Where is your slot get_movie(QString) definition and what is it doing?
All you have to do is:// In List class void List::get_movie(QString m) { ui->label->setText(m); } -
I understand how we update the value in the ui. But I want to assign that value to a new variable in my second window. As in, I want to pass that username in a database and run a query. I cannot get that movie name under a variable. That is my main problem. I read documentations and also searched other projects. I just do not tend to get the idea of how I can assign the value into a new variable.
-
I understand how we update the value in the ui. But I want to assign that value to a new variable in my second window. As in, I want to pass that username in a database and run a query. I cannot get that movie name under a variable. That is my main problem. I read documentations and also searched other projects. I just do not tend to get the idea of how I can assign the value into a new variable.
@RootLearner Can you please explain what variable in what second window?
Is this second window the List class?
If so then its dead easy:// In List class void List::get_movie(QString m) { a = m; // I assume a was defined as class member, but could also be a local variable ui->label->setText(m); } -
@RootLearner Can you please explain what variable in what second window?
Is this second window the List class?
If so then its dead easy:// In List class void List::get_movie(QString m) { a = m; // I assume a was defined as class member, but could also be a local variable ui->label->setText(m); }@jsulm Okay, I understood this. But I want to know how I am going to call this slot?
My lists.cpp is:#include "lists.h" #include "ui_lists.h" lists::lists(QWidget *parent) : QMainWindow(parent), ui(new Ui::lists) { ui->setupUi(this); QString a; //I should be calling the slot here??? If yes, how? qDebug()<<a;//just an example } lists::~lists() { delete ui; } void lists::get_movie(QString m) { a = m; // I ui->label->setText(m); }This says a is an undeclared identifier. How do I call this slot and where?
While calling, will it not ask for variable 'm'? -
@jsulm Okay, I understood this. But I want to know how I am going to call this slot?
My lists.cpp is:#include "lists.h" #include "ui_lists.h" lists::lists(QWidget *parent) : QMainWindow(parent), ui(new Ui::lists) { ui->setupUi(this); QString a; //I should be calling the slot here??? If yes, how? qDebug()<<a;//just an example } lists::~lists() { delete ui; } void lists::get_movie(QString m) { a = m; // I ui->label->setText(m); }This says a is an undeclared identifier. How do I call this slot and where?
While calling, will it not ask for variable 'm'?@RootLearner said in Slots in the second window?:
Okay, I understood this. But I want to know how I am going to call this slot?
Please, start to learn C++ basics:
- what are classes
- variable scope
If you define a local variable in a function, this variable is local to this function and can not be used in another function.
-
@jsulm Okay, I understood this. But I want to know how I am going to call this slot?
My lists.cpp is:#include "lists.h" #include "ui_lists.h" lists::lists(QWidget *parent) : QMainWindow(parent), ui(new Ui::lists) { ui->setupUi(this); QString a; //I should be calling the slot here??? If yes, how? qDebug()<<a;//just an example } lists::~lists() { delete ui; } void lists::get_movie(QString m) { a = m; // I ui->label->setText(m); }This says a is an undeclared identifier. How do I call this slot and where?
While calling, will it not ask for variable 'm'?@RootLearner As @KroMignon suggested: learn C++ first.
You declared a as local variable in lists constructor, so it only exists as long as this constructor is executed.
Declare a as class member...And why do you want to call the slot? Slot is called automatically as soon as the signal is emitted.
-
@RootLearner said in Slots in the second window?:
Okay, I understood this. But I want to know how I am going to call this slot?
Please, start to learn C++ basics:
- what are classes
- variable scope
If you define a local variable in a function, this variable is local to this function and can not be used in another function.
@KroMignon It was dumb of me to not recognize it as a local variable. I didn't see I am actually declaring it in the constructor. I was thinking if the variable declared will have global scope. I messed up my concepts. Thank you for highlighting it.
-
@RootLearner As @KroMignon suggested: learn C++ first.
You declared a as local variable in lists constructor, so it only exists as long as this constructor is executed.
Declare a as class member...And why do you want to call the slot? Slot is called automatically as soon as the signal is emitted.
@jsulm I assumed I was making it a class member by declaring it in the constructor. And I actually didn't know slots gets called automatically as soon as the signal is emitted. I only worked with 'onButtonClicked()' as a slot and it worked when I clicked the button. So, I assumed every slots will be required to be called by doing something specifically. Thank you for clearing out my misconception. I got the concept and now, I can apply it in my project!
-
#include "movielist.h" #include "ui_movielist.h" #include <QSqlDatabase> #include <QSqlError> #include <QSqlQueryModel> #include <QSqlQuery> #include <QDebug> #include <QSqlRecord> #include <QMessageBox> #include "movie.h" Movielist::Movielist(QWidget *parent) : QDialog(parent), ui(new Ui::Movielist) { ui->setupUi(this); QSqlDatabase myfile; myfile = QSqlDatabase::addDatabase("QSQLITE"); myfile.setDatabaseName("C:/Users/user/Documents/New folder/qtcode/movie.db"); if (!myfile.open()) { qDebug() << ("Failed to open the database"); } else { qDebug() << ("Connected"); } QSqlQueryModel *model = new QSqlQueryModel(); QSqlQuery qry; } Movielist::~Movielist() { delete ui; } void Movielist::on_tableView_activated(const QModelIndex &index) { movie m; int c = index.column(); QString temp = ui->tableView->model()->data(index).toString(); qDebug() << temp; m.setModal(true); hide(); m.exec(); } void Movielist::receive(int a) { qDebug()<<"opening movielist"; switch (a) { case 0: { qDebug()<<"case 0"; QSqlQueryModel *model = new QSqlQueryModel(); QSqlQuery qry; qry.prepare("select Name, Date, Ratings, Genre from movie order by Ratings DESC"); qDebug()<<"Case 0 ko query"; if (qry.exec()) { model->setQuery(qry); ui->label_2->setText("Top Rated Movies"); ui->tableView->setModel(model); ui->tableView->resizeColumnsToContents(); qDebug()<<"case 0 query executed"; } break; } case 1: { QSqlQueryModel *model1 = new QSqlQueryModel(); QSqlQuery qry1; qry1.prepare("select Name, Date, Ratings, Genre from movie where Genre = 'Romantic' order by Ratings DESC "); if (qry1.exec()) { model1->setQuery(qry1); ui->label_2->setText("Top Romantic Movies"); ui->tableView->setModel(model1); ui->tableView->resizeColumnsToContents(); } break; } } }When receive slot is called, the ui is not updated but when I close the window, the qDebug in "opening movielist" and case 0 is run. How do I solve this? I need it to update the ui as per the integer passed via signals.
-
#include "movielist.h" #include "ui_movielist.h" #include <QSqlDatabase> #include <QSqlError> #include <QSqlQueryModel> #include <QSqlQuery> #include <QDebug> #include <QSqlRecord> #include <QMessageBox> #include "movie.h" Movielist::Movielist(QWidget *parent) : QDialog(parent), ui(new Ui::Movielist) { ui->setupUi(this); QSqlDatabase myfile; myfile = QSqlDatabase::addDatabase("QSQLITE"); myfile.setDatabaseName("C:/Users/user/Documents/New folder/qtcode/movie.db"); if (!myfile.open()) { qDebug() << ("Failed to open the database"); } else { qDebug() << ("Connected"); } QSqlQueryModel *model = new QSqlQueryModel(); QSqlQuery qry; } Movielist::~Movielist() { delete ui; } void Movielist::on_tableView_activated(const QModelIndex &index) { movie m; int c = index.column(); QString temp = ui->tableView->model()->data(index).toString(); qDebug() << temp; m.setModal(true); hide(); m.exec(); } void Movielist::receive(int a) { qDebug()<<"opening movielist"; switch (a) { case 0: { qDebug()<<"case 0"; QSqlQueryModel *model = new QSqlQueryModel(); QSqlQuery qry; qry.prepare("select Name, Date, Ratings, Genre from movie order by Ratings DESC"); qDebug()<<"Case 0 ko query"; if (qry.exec()) { model->setQuery(qry); ui->label_2->setText("Top Rated Movies"); ui->tableView->setModel(model); ui->tableView->resizeColumnsToContents(); qDebug()<<"case 0 query executed"; } break; } case 1: { QSqlQueryModel *model1 = new QSqlQueryModel(); QSqlQuery qry1; qry1.prepare("select Name, Date, Ratings, Genre from movie where Genre = 'Romantic' order by Ratings DESC "); if (qry1.exec()) { model1->setQuery(qry1); ui->label_2->setText("Top Romantic Movies"); ui->tableView->setModel(model1); ui->tableView->resizeColumnsToContents(); } break; } } }When receive slot is called, the ui is not updated but when I close the window, the qDebug in "opening movielist" and case 0 is run. How do I solve this? I need it to update the ui as per the integer passed via signals.
@RootLearner said in Slots in the second window?:
When receive slot is called, the ui is not updated but when I close the window, the qDebug in "opening movielist" and case 0 is run. How do I solve this? I need it to update the ui as per the integer passed via signals.
For me it looks like there is a forever loop which locks the thread event loop.
The emitted signals are stored in event loop, but as the thread is lock in a forever loop, the associated slots are called when forever loop ends and event loop is running.What are you doing in
movieclass?