Important: Please read the Qt Code of Conduct -

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

  • 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) :
    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
        explicit MainWindow(QWidget *parent = 0);
        //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.

  • Lifetime Qt Champion


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


    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.

  • @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
    you add a public function ("getter"), that returns the (private) member (e.g. as reference) ( @SGaist's approach) .

    Something like:

    // ####### CCC_LocalAdapters.h ###########
    QList< QBluetoothHostInfo > & getInfoList();
    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

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

Log in to reply