Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

signal slot between 2 classes



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


  • Moderators

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



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


  • Moderators

    @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(...));
    


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



  • 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();
    
    

Log in to reply