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. How to pass object as pointer - C++ syntax ?
QtWS25 Last Chance

How to pass object as pointer - C++ syntax ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++
4 Posts 3 Posters 790 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by Anonymous_Banned275
    #1

    This is plain C++ question I am struggling with, nothing to do with Qt

    I like to have MainWindow Widget to list local bluetooth device info.
    I have build a class to retrieve the info, now I need MainWindow to access it. .
    I just cannot figure out the correct syntax how to pass / retrieve / use the list pointer to my object / class so I can use it in MainWindow .

    Here are snippets of my code

    This class uses QList , QBluetoothHostInfo and QBluetoothLocalDevice and works as expected returning "infos" - locally for test purpose.

    CCC_LocalAdapters::CCC_LocalAdapters(QObject *parent) : QObject(parent)
    {

    // get bluetooth device information
    // bluetooth test for local device - works as expected locally
    QList<QBluetoothHostInfo> infos = QBluetoothLocalDevice::allDevices();
    …
    }

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ...
    // build instance of class , pass object pointer
    CCC_LocalAdapters *LocalAdapters = new CCC_LocalAdapters(infos);

    this syntax / usage is wrong and I am getting this compiler error

    /home/f/QT_PROJECT/TEST_PROJECT_3/mainwindow.cpp:60: error: missing template arguments before 'infos'
     CCC_LocalAdapters *LocalAdapters_info = new CCC_LocalAdapters(QList infos);
                                                                         ^
    

    ...
    I did try this

    CCC_LocalAdapters *LocalAdapters_info = new CCC_LocalAdapters(QList<QBluetoothHostInfo> infos);

    And getting this error

    /home/f/QT_PROJECT/TEST_PROJECT_3/mainwindow.cpp:60: error: expected primary-expression before 'infos'
     CCC_LocalAdapters *LocalAdapters_info = new CCC_LocalAdapters(QList<QBluetoothHostInfo> infos);
                                                                                             ^
    

    I am totally lost. Please help C++ ignorant person .

    }

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        //add 21/11/2020
        // local BT device info
        QList<QBluetoothHostInfo> *infos;
       ….
       } 
    
    

    In not so technical terms – I need to pass “infos” upstream so I can let main
    window GUI print it.

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

      Hi,

      @AnneRanch said in How to pass object as pointer - C++ syntax ?:

      CCC_LocalAdapters

      The only constructor you have take a QObject as parameter.

      Add a getter to that class that returns the internal list you store in it.

      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
      2
      • A Anonymous_Banned275

        This is plain C++ question I am struggling with, nothing to do with Qt

        I like to have MainWindow Widget to list local bluetooth device info.
        I have build a class to retrieve the info, now I need MainWindow to access it. .
        I just cannot figure out the correct syntax how to pass / retrieve / use the list pointer to my object / class so I can use it in MainWindow .

        Here are snippets of my code

        This class uses QList , QBluetoothHostInfo and QBluetoothLocalDevice and works as expected returning "infos" - locally for test purpose.

        CCC_LocalAdapters::CCC_LocalAdapters(QObject *parent) : QObject(parent)
        {

        // get bluetooth device information
        // bluetooth test for local device - works as expected locally
        QList<QBluetoothHostInfo> infos = QBluetoothLocalDevice::allDevices();
        …
        }

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ...
        // build instance of class , pass object pointer
        CCC_LocalAdapters *LocalAdapters = new CCC_LocalAdapters(infos);

        this syntax / usage is wrong and I am getting this compiler error

        /home/f/QT_PROJECT/TEST_PROJECT_3/mainwindow.cpp:60: error: missing template arguments before 'infos'
         CCC_LocalAdapters *LocalAdapters_info = new CCC_LocalAdapters(QList infos);
                                                                             ^
        

        ...
        I did try this

        CCC_LocalAdapters *LocalAdapters_info = new CCC_LocalAdapters(QList<QBluetoothHostInfo> infos);

        And getting this error

        /home/f/QT_PROJECT/TEST_PROJECT_3/mainwindow.cpp:60: error: expected primary-expression before 'infos'
         CCC_LocalAdapters *LocalAdapters_info = new CCC_LocalAdapters(QList<QBluetoothHostInfo> infos);
                                                                                                 ^
        

        I am totally lost. Please help C++ ignorant person .

        }

        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
            //add 21/11/2020
            // local BT device info
            QList<QBluetoothHostInfo> *infos;
           ….
           } 
        
        

        In not so technical terms – I need to pass “infos” upstream so I can let main
        window GUI print it.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @AnneRanch

        So you want that info QList from CCC_LocalAdapters in your MainWindow class?!

        You can either make that info list in CCC_LocalAdapters public by declaring it as public member variable in CCC_LocalAdapters header
        OR
        you add a public function ("getter"), that returns the (private) member (e.g. as reference) ( @SGaist's approach) .

        Something like:

        // ####### CCC_LocalAdapters.h ###########
        public:
        QList< QBluetoothHostInfo > & getInfoList();
        
        private:
        QList< QBluetoothHostInfo > info;
        
        
        // ####### CCC_LocalAdapters.cpp ###########
        QList<QBluetoothHostInfo>& CCC_LocalAdapters::getInfoList(){
                return info;
        }
        

        Then you can do something like this in your MainWindow class:

        CCC_LocalAdapters *LocalAdapters = new CCC_LocalAdapters();
        info = LocalAdapters->getInfoList(); // will return a reference to that list in CC_LocalAdapters
        

        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1
        • A Offline
          A Offline
          Anonymous_Banned275
          wrote on last edited by
          #4

          @AnneRanch said in How to pass object as pointer - C++ syntax ?:

          CCC_LocalAdapters::CCC_LocalAdapters(QObject *parent) : QObject(parent)

          Thanks fellas.

          So you want that info QList from CCC_LocalAdapters in your MainWindow class?!
          Yes, that is my goal.
          I though I had it using the "second" solution.
          I'll digest you suggestions and report back later. I am "nursing" my bedridden partner - broken leg and wrist - so have not much time to bang on the keyboard.
          Later

          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