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. Passing class reference object to another form class

Passing class reference object to another form class

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 187 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
    russjohn834
    wrote on last edited by russjohn834
    #1

    Hi all,

    i have a basic question.

    I have a class instance which need to be passed to other classes being constructed tetra_grip_api api;

    I did this in the main

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        tetra_grip_api api;
    
        api.openSerialPort();
        QObject::connect(api.serial, SIGNAL(readyRead()), &api, SLOT(readData()));
    
        Settings w(&api);
    

    Also, I changed the constructor of the Settings as follows:

     Settings :: Settings ( tetra_grip_api api, QWidget *parent) : QMainWindow(parent)
        , ui(new Ui:: Settings )
    {
    ...
    
    connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler);
    

    Here I get an error complaining:

    main.cpp:21:18: error: no matching constructor for initialization of 'Settings'
    settings.h:23:6: note: candidate constructor not viable: no known conversion from 'tetra_grip_api *' to 'tetra_grip_api' for 1st argument; remove &
    settings.h:18:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'tetra_grip_api *' to 'const Settings' for 1st argument
    

    I dont understand what's happening, any idea?

    Thanks

    jsulmJ 1 Reply Last reply
    0
    • R russjohn834

      Hi all,

      i have a basic question.

      I have a class instance which need to be passed to other classes being constructed tetra_grip_api api;

      I did this in the main

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          tetra_grip_api api;
      
          api.openSerialPort();
          QObject::connect(api.serial, SIGNAL(readyRead()), &api, SLOT(readData()));
      
          Settings w(&api);
      

      Also, I changed the constructor of the Settings as follows:

       Settings :: Settings ( tetra_grip_api api, QWidget *parent) : QMainWindow(parent)
          , ui(new Ui:: Settings )
      {
      ...
      
      connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler);
      

      Here I get an error complaining:

      main.cpp:21:18: error: no matching constructor for initialization of 'Settings'
      settings.h:23:6: note: candidate constructor not viable: no known conversion from 'tetra_grip_api *' to 'tetra_grip_api' for 1st argument; remove &
      settings.h:18:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'tetra_grip_api *' to 'const Settings' for 1st argument
      

      I dont understand what's happening, any idea?

      Thanks

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

      @russjohn834 said in Passing class reference object to another form class:

      Settings :: Settings ( tetra_grip_api api, QWidget *parent) : QMainWindow(parent)

      You're passing by value (means copy!), not by reference. Pass a pointer:

      Settings :: Settings ( tetra_grip_api *api, QWidget *parent) : QMainWindow(parent)
      

      The error message says what is wrong.

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

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

        Thank you @jsulm

        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