Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] Passing an object to QWidget inherited classes



  • Hi, I created a QTwidget project via the QTDesigner and then I have coded into the autogenerated *.cpp and *.h files. I need to pass to the QtWidged inherited class an object but I get errors. The generated code is here:

    @Trainer::Trainer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Trainer)
    {
    ui->setupUi(this);

    //  --- Query Model + TableView
    queryModelcol = new QSqlQueryModel(this);
    queryModelcol->setQuery("SELECT content FROM tweet");
    queryModelcol->setHeaderData(3, Qt::Horizontal, tr("Message"));
    
    ui->tableView->setModel(queryModelcol);
    ui->tableView->resizeColumnsToContents();
    //  --- Query Model for row data extraction
    queryModelrow = new QSqlQueryModel(this);
    queryModelcol->setQuery("SELECT content FROM tweet");
    //Connect row selection action to tabeView
    indexes = ui->tableView->selectionModel()->selection().indexes();
    connect(ui->tableView, SIGNAL(clicked()), this, SLOT(on_tableView_clicked(indexes)));
    
    //Button Init : Voting buttons disabled
    enableVoting(false);
    
    //  --- Combo Box: TOPIC selection via combobox
    QSqlQueryModel *topicModel = new QSqlQueryModel(this);
    topicModel->setQuery("SELECT DISTINCT TOPIC FROM TWEET");
    ui->topicSelector->setModel(topicModel);
    connect(ui->topicSelector, SIGNAL(currentIndexChanged(QString)),
            this ,
            SLOT(on_topicSelector_currentIndexChanged(ui->topicSelector->currentText())));
    setWindowTitle(tr("Trainer"));
    

    }@

    @#ifndef TRAINER_H
    #define TRAINER_H

    #include <QWidget>
    #include <QSqlQueryModel>
    #include <QModelIndex>
    #include <QDialog>
    #include <QMessageBox>
    #include <QSqlTableModel>
    #include <QSqlQuery>
    #include "../pyTry/dbmanager.h"

    namespace Ui {
    class Trainer;
    }

    class Trainer : public QWidget
    {
    Q_OBJECT

    public:
    Trainer(QWidget *parent = 0);
    ~Trainer();
    private slots:
    void on_tableView_clicked(const QModelIndex &index);
    void on_topicSelector_currentIndexChanged(const QString &arg1);

    private:
    Ui::Trainer *ui;

    QSqlTableModel *tableModel;
    QSqlQueryModel *queryModelcol;
    QSqlQueryModel *queryModelrow;
    QModelIndexList indexes;
    
    void enableVoting(bool value);
    

    };

    #endif // TRAINER_H@

    I have an object dbManager which allows me to insert data into the database and check the connection status.

    At first I thought that i had to pass the dbManager object as reference to the Trainer class, but:

    @int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    dbManager dbCon = dbManager();
    
    Trainer w;
    w.show();
    
    return a.exec&#40;&#41;;
    

    }@

    works fine with the autogenarated code, and rise errors when i want to use the modified class

    @#include ....

    namespace Ui {
    class Trainer;
    }

    class Trainer : public QWidget
    {
    Q_OBJECT

    public:
    Trainer(dbManager &db, QWidget *parent = 0);

    //....
    }@

    with
    @int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    dbManager dbCon = dbManager();
    
    Trainer w = Trainer(dbCon);
    w.show();
    
    return a.exec&#40;&#41;;
    

    }@

    but using
    @int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    dbManager dbCon = dbManager();
    
    Trainer *w = new Trainer(dbCon);
    w->show();
    
    return a.exec(&#41;;
    

    }@

    works, but the reference is only reachable in the scope of the constructor.
    How can I manage this situation? I want to use dbManager in different widgets to access the database.


  • Qt Champions 2017

    Trainer {

    public :
    setDbRefererence(dbManager *db);
    dbManager *mManager;
    }

    Trainer *w = new Trainer;
    w->setDbReference(dbCon)

    Instead of passing it as constructor, create member variable in trainer class itself. Create seperate a method to set the db variable. You can pass the dbreference to this method. Now it will be accessible in complete class. it will not be accessible in constructor. If you want to access in contructor, set the db member variable in the constructor itself.
    Hope this helps.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    First thing to clarify: what does your dbManager do ?



  • [quote author="dheerendra" date="1388856820"]Trainer {

    public :
    setDbRefererence(dbManager *db);
    dbManager *mManager;
    }

    Trainer *w = new Trainer;
    w->setDbReference(dbCon)

    [/quote]

    This solution works, I thought that every object and variable should all be initialized in the constructor. Thank you! :)


Log in to reply