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. Slots in the second window?
Qt 6.11 is out! See what's new in the release blog

Slots in the second window?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 1.6k 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.
  • R Offline
    R Offline
    RootLearner
    wrote on last edited by
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • R RootLearner

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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);
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RootLearner
        wrote on last edited by
        #3

        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.

        jsulmJ 1 Reply Last reply
        0
        • R RootLearner

          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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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);
          }
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          1
          • jsulmJ jsulm

            @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);
            }
            
            R Offline
            R Offline
            RootLearner
            wrote on last edited by
            #5

            @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'?

            KroMignonK jsulmJ 2 Replies Last reply
            0
            • R RootLearner

              @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'?

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

              @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.

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

              R 1 Reply Last reply
              3
              • R RootLearner

                @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'?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                R 1 Reply Last reply
                2
                • KroMignonK KroMignon

                  @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.

                  R Offline
                  R Offline
                  RootLearner
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @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.

                    R Offline
                    R Offline
                    RootLearner
                    wrote on last edited by
                    #9

                    @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!

                    1 Reply Last reply
                    2
                    • R Offline
                      R Offline
                      RootLearner
                      wrote on last edited by RootLearner
                      #10

                      @jsulm @KroMignon

                      #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.

                      KroMignonK 1 Reply Last reply
                      0
                      • R RootLearner

                        @jsulm @KroMignon

                        #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.

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

                        @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 movie class?

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

                        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