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. [SOLVED] Passing an object to QWidget inherited classes

[SOLVED] Passing an object to QWidget inherited classes

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.6k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    spruzzer
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        First thing to clarify: what does your dbManager do ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • S Offline
          S Offline
          spruzzer
          wrote on last edited by
          #4

          [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! :)

          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