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. signal slot between 2 classes
QtWS25 Last Chance

signal slot between 2 classes

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 611 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.
  • M Offline
    M Offline
    Mogli123
    wrote on last edited by Mogli123
    #1

    Hi,

    I want to create a connection between a signal and slot, which are in different classes.
    I update a database and want tell the tableview that he should update, too.

    That's my code so far.

    .h

    private: 
        void fillDatabaseTableCustomiseToolChange_1();
        QTimer *timer;
    public:
        signals:
            void valueChanged();
    private slots:
        void fillDatabaseTableCustomiseToolChange_1();
    
    

    .cpp

    detailScreen::detailScreen(QWidget *parent) :
        QMainWindow(parent), 
        ui(new Ui::test_screen)
    {
        ui->setupUi(this);
     ...
    timer = new QTimer(this);
    timer->setInterval(3000);
    timer->start();
    connect(timer, SIGNAL(timeout()), this, SLOT(fillDatabaseTableCustomiseToolChange_1()));
    
    // I'm not shure how to do that
    connect(this, Signal(valueChaged()), ???);
    ...
    }
    
    void detailScreen::fillDatabaseTableCustomiseToolChange_1()
    {
        int j = 0;
    
        for (int indexRow = 0; indexRow < 10; indexRow ++)
        {
            for (int indexColumn = 1; indexColumn < 4; indexColumn ++)
            {
                queryManipulation->setData(queryManipulation->index(indexRow, indexColumn), arrayCustomiseToolChange_1[j]);
                j++;
            }
        }
        // signal from detailScreen 
        emit valueChanged();
    }
    
    

    tableview.cpp

    TableCustomiseToolChange::TableCustomiseToolChange(QWidget *parent) :
        QWidget(parent)
    {
    myModelTableCustomiseToolChange = new TableCustomise(this);
    myModelTableCustomiseToolChange->setTable("CustomiseToolChange");
    
    myTableview = new QTableView(this);
    myTableview->setModel(myModelTableCustomiseToolChange);
    ...
    }
    

    I don't know how I have to tell the tableview that data is changed to update the table.

    I hope someone can help me.

    Thank you.
    Regards mogli.

    raven-worxR 1 Reply Last reply
    0
    • M Mogli123

      Hi,

      I want to create a connection between a signal and slot, which are in different classes.
      I update a database and want tell the tableview that he should update, too.

      That's my code so far.

      .h

      private: 
          void fillDatabaseTableCustomiseToolChange_1();
          QTimer *timer;
      public:
          signals:
              void valueChanged();
      private slots:
          void fillDatabaseTableCustomiseToolChange_1();
      
      

      .cpp

      detailScreen::detailScreen(QWidget *parent) :
          QMainWindow(parent), 
          ui(new Ui::test_screen)
      {
          ui->setupUi(this);
       ...
      timer = new QTimer(this);
      timer->setInterval(3000);
      timer->start();
      connect(timer, SIGNAL(timeout()), this, SLOT(fillDatabaseTableCustomiseToolChange_1()));
      
      // I'm not shure how to do that
      connect(this, Signal(valueChaged()), ???);
      ...
      }
      
      void detailScreen::fillDatabaseTableCustomiseToolChange_1()
      {
          int j = 0;
      
          for (int indexRow = 0; indexRow < 10; indexRow ++)
          {
              for (int indexColumn = 1; indexColumn < 4; indexColumn ++)
              {
                  queryManipulation->setData(queryManipulation->index(indexRow, indexColumn), arrayCustomiseToolChange_1[j]);
                  j++;
              }
          }
          // signal from detailScreen 
          emit valueChanged();
      }
      
      

      tableview.cpp

      TableCustomiseToolChange::TableCustomiseToolChange(QWidget *parent) :
          QWidget(parent)
      {
      myModelTableCustomiseToolChange = new TableCustomise(this);
      myModelTableCustomiseToolChange->setTable("CustomiseToolChange");
      
      myTableview = new QTableView(this);
      myTableview->setModel(myModelTableCustomiseToolChange);
      ...
      }
      

      I don't know how I have to tell the tableview that data is changed to update the table.

      I hope someone can help me.

      Thank you.
      Regards mogli.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Mogli123 said in signal slot between 2 classes:

      // I'm not shure how to do that
      connect(this, Signal(valueChaged()), ???);

      if you need the target instance to connect to here you will need to pass it somehow to the constructor of course.

      Or alternatively move this connect statement outside of the constructor to a place where you have pointers to the source and target objects for the connection.

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

      M 1 Reply Last reply
      1
      • raven-worxR raven-worx

        @Mogli123 said in signal slot between 2 classes:

        // I'm not shure how to do that
        connect(this, Signal(valueChaged()), ???);

        if you need the target instance to connect to here you will need to pass it somehow to the constructor of course.

        Or alternatively move this connect statement outside of the constructor to a place where you have pointers to the source and target objects for the connection.

        M Offline
        M Offline
        Mogli123
        wrote on last edited by
        #3

        @raven-worx

        Thank you for your help.

        I'm very new in cpp and qt and don't know how to solve that problem.
        Can you give me a small example please?

        raven-worxR 1 Reply Last reply
        0
        • M Mogli123

          @raven-worx

          Thank you for your help.

          I'm very new in cpp and qt and don't know how to solve that problem.
          Can you give me a small example please?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Mogli123

          1. within constructor
          detailScreen::detailScreen(ObjectToConnect* obj, QWidget *parent)
          {
              ...
              connect(this, SIGNAL(valueChanged()), obj, SLOT(...));
              ...
          }
          
          // somewhere where you create the object
          ObjectToConnect* obj = new ObjectToConnect();
          new detailScreen(obj, parent);
          
          1. outside of constructor
          ObjectToConnect* obj = new ObjectToConnect();
          detailScreen* screen = new detailScreen();
          ...
          connect(screen, SIGNAL(valueChanged()), obj, 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

          M 1 Reply Last reply
          2
          • raven-worxR raven-worx

            @Mogli123

            1. within constructor
            detailScreen::detailScreen(ObjectToConnect* obj, QWidget *parent)
            {
                ...
                connect(this, SIGNAL(valueChanged()), obj, SLOT(...));
                ...
            }
            
            // somewhere where you create the object
            ObjectToConnect* obj = new ObjectToConnect();
            new detailScreen(obj, parent);
            
            1. outside of constructor
            ObjectToConnect* obj = new ObjectToConnect();
            detailScreen* screen = new detailScreen();
            ...
            connect(screen, SIGNAL(valueChanged()), obj, SLOT(...));
            
            M Offline
            M Offline
            Mogli123
            wrote on last edited by Mogli123
            #5

            @raven-worx

            Is that right so far?

            .h

            class detailScreen : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit detailScreen(TableCustomiseToolChange* connectToTableCustomiseToolChange = nullptr, QWidget *parent = nullptr);
                ~detailScreen();
            ...
            

            .cpp

            etailScreen::detailScreen(TableCustomiseToolChange* connectToTableCustomiseToolChange, QWidget *parent) :
                QMainWindow(parent), 
                ui(new Ui::test_screen)
            {
                ui->setupUi(this);
            
            ...
            
            timer = new QTimer(this);
            timer->setInterval(3000);
            timer->start();
            connect(timer, SIGNAL(timeout()), this, SLOT(fillDatabaseTableCustomiseToolChange_1()));
            
            connect(this, SIGNAL(valueChanged()), connectToTableCustomiseToolChange, SLOT(????????????));
            
            }
            ...
            

            Do you happen to know if tableview has a public slot that updates the view?

            Is it enough to connect the model from the view to the database for updating the view data if data from the database changed?




            update:

            If i write the code like this: explicit detailScreen(TableCustomiseToolChange* connectToTableCustomiseToolChange, QWidget*parent = nullptr);
            I get an error in my main.

            detailScreen w;
            w.show();
            

            error: no matching constructor for initialization of 'detailScreen'

            I noticed I overlooked the following below:

            // somewhere where you create the object
            ObjectToConnect* obj = new ObjectToConnect();
            new detailScreen(obj, parent);```
            

            Where do I have to create the object?




            Sorry for the many questions.

            If someone need more of my code please ask?

            Regards mogli.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mogli123
              wrote on last edited by Mogli123
              #6

              Now it works :)
              Thank you for your help.

              .cpp

              detailScreen::detailScreen(QWidget *parent) :
                  QMainWindow(parent), // code: Title Bar verstecken "(parent, Qt::FramelessWindowHint)"
                  ui(new Ui::test_screen)
              {
                  ui->setupUi(this);
              myTableToolChangeWarnings = new TableToolChangeWarnings(this);
              ...
              timer = new QTimer(this);
                  timer->setInterval(1000);
                  timer->start();
               connect(this, SIGNAL(refreshTable()), myTableToolChangeWarnings, SLOT(updateTable()));
              }
              
              //Method
              
              void detailScreen::fillDatabaseToolChangeWarnings()
              {
                  int j = 0;
                  for (int indexColumn = 1; indexColumn < 4; indexColumn ++)
                  {
                      for (int indexRow = 0; indexRow < 10; indexRow ++)
                      {
                        int randInt;
                        randInt = rand() % (-40-20);
                          queryManipulation->setData(queryManipulation->index(indexRow, indexColumn), /*arrayToolChangeWarnings[j] +*/ randInt);
                          qDebug()<<"arrayToolChangeWarnings[j] ="<</*arrayToolChangeWarnings[j] +*/ randInt;
                          j++;
                      }
                  }
                  // signal for table refresh
                  emit refreshTable();
              }
              
              

              .h

              #include "TableToolChangeWarnings.h"
              
              namespace Ui {
              class test_screen;
              }
              
              class detailScreen : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                 explicit detailScreen( QWidget *parent = nullptr);
                  ~detailScreen();
              
              private:
                   TableToolChangeWarnings *myTableToolChangeWarnings;
                   QTimer *timer;
              
              private signals:
                  void refreshTable();
              
              }
              

              table.cpp

              
              void TableToolChangeWarnings::updateTable()
              {
                  myModelTableCustomiseToolChange->setQuery("SELECT * FROM ToolChangeWarnings");
              };
              
              

              table.h

              
              private slots:
                  void updateTable();
              
              
              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