Click here to Skip to main content
15,306,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The attached C++ code gives errors:

/media/f/QT/Qt/QT/qtconnectivity/examples/bluetooth/CAT_BT_18112020/device.cpp:125: error: cannot call member function 'void QListWidget::itemClicked(QListWidgetItem*)' without object
             &QListWidget::itemClicked(&item), // &item),
                                            ^

/pre>

What am I doing wrong ? 

<pre lang="c++">
   QListWidgetItem item;

    QObject::connect(ui->list,                     object 
            &QListWidget::itemClicked(&item),      signal 
            ui->list_2,                            object 
            &QListWidget::addItem(&item));         slot 


Edit 
After looking at samo exmapkes I have chaged my code 



<pre lang="c++">   QObject ::connect(ui->list,
            QListWidget.itemClicked(item),
            ui->list_2,
            QListWidget.addItem(item)
            );


Now I am getting an different error and I do not understand what I should do to fix it .


/media/f/QT/Qt/QT/qtconnectivity/examples/bluetooth/CAT_BT_18112020/device.cpp:131: error: expected primary-expression before '.' token
             QListWidget.itemClicked(item),
                        ^


What I have tried:

Loked at sample "connect" codes, but none helped.
It is basic C++ I am doing wrong.
Posted
Updated 1-Dec-20 6:03am
v2

The error message, minus the name, is 'cannot call member function ... without object'. That means you are attempting to call a non-static member function without an object. If it was a static member then it would not need an object.

The message isn't exactly clear but I think what you want there is :
C++
QObject::connect(
          ui->list
        , &QListWidget::itemClicked
        , ui->list_2
        , &QListWidget::addItem
        );
When passing a pointer to a function as an argument to a function no arguments can be used with it.
   
Comments
Member 14980433 30-Nov-20 22:24pm
   
The task is to pass an "item" between list and list_2 - the methods itemClicked parameter should be "item" , by same token the addItem method should have "item" parameter passed. Without parameters the compiler reject the whole "connect" function as "unknown".

/media/f/QT/Qt/QT/qtconnectivity/examples/bluetooth/CAT_BT_18112020/device.cpp:127: error: no matching function for call to 'DeviceDiscoveryDialog::connect(QListWidget*&, void (QListWidget::*)(QListWidgetItem*), QListWidget*&, <unresolved overloaded="" function="" type="">)'
);
^
Member 14980433 30-Nov-20 22:28pm
   
The error message, minus the name, is 'cannot call member function ... without object'. That means you are attempting to call a non-static member function without an object.

This simply duplicates and justifies (?) the error message. OK , but I am looking for a solution.
Richard MacCutchan 1-Dec-20 4:02am
   
You need to study the C++ documentation to understand the difference between calling a static function (Classname::StaticFunction()) and a non-static (object.OrdinaryFunction()). Unless and until you fully understand C++ you will continue to have problems trying to use QT.
Member 14980433 1-Dec-20 21:48pm
   
Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Richard MacCutchan 2-Dec-20 6:08am
   
I did provide an answer, it is up to you to make use of it.
connect(list_widget_1,
&QListWidget::itemClicked,
list_widget_2,
QOverload<qlistwidgetitem*>::of(&QListWidget::addItem));

Because addItem() has 2 overloads, QOverload must be used

QListWidget::addItem(const QString &label)
QListWidget::addItem(QListWidgetItem *item)

But you don't want to do this, since the documentation specifically said that:

Warning: A QListWidgetItem can only be added to a QListWidget once. Adding the same QListWidgetItem multiple times to a QListWidget will result in undefined behavior.

Instead you should do something like this

connect(list_widget_1,
&QListWidget::itemClicked,
[list_widget_2](QListWidgetItem *item)
{
list_widget_2->addItem(item->clone());
});
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900