Adding QBluetoothDeviceInfo to QList error
-
wrote on 10 Feb 2023, 13:05 last edited by
Here is a code snippet which oasses "intelisese" ("append: is a valid function ) , but gives me an error.
void MainWindow_BT_Terminal::addDevice(const QBluetoothDeviceInfo &info) { text = "add remote device to list... "; text += " "; qDebug()<< text; // QList<QBluetoothDeviceInfo*> RemoteDevicesList; RemoteDevicesList.append(info);
I do not understand why I am getting this error.
/mnt/07b7c3f8-0efb-45ab-8df8-2a468771de1f/PROJECTS/RECOVERY/BLUETOOTH/SOURCE/CCC_SOURCE/terminal_SERIAL_BLUETOOTH_VER_1/terminal_SERIAL_BLUETOOTH/terminal_BLUETOOTH/mainwindow_BT_Terminal.cpp:799: error: no matching member function for call to 'append' mainwindow_BT_Terminal.cpp:799:23: error: no matching member function for call to 'append' RemoteDevicesList.append(info); ~~~~~~~~~~~~~~~~~~^~~~~~ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qlist.h:210:10: note: candidate function not viable: no known conversion from 'const QBluetoothDeviceInfo' to 'QBluetoothDeviceInfo *const' for 1st argument void append(const T &t); ^ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qlist.h:211:10: note: candidate function not viable: no known conversion from 'const QBluetoothDeviceInfo' to 'const QList<QBluetoothDeviceInfo *>' for 1st argument void append(const QList<T> &t); ^
could somebody PLEASE explain to me why I am getting the error and OFFER A SOLUTION ?
PS I do not need a confirmation that I am getting an error, I need a solution.
Thanks
-
Here is a code snippet which oasses "intelisese" ("append: is a valid function ) , but gives me an error.
void MainWindow_BT_Terminal::addDevice(const QBluetoothDeviceInfo &info) { text = "add remote device to list... "; text += " "; qDebug()<< text; // QList<QBluetoothDeviceInfo*> RemoteDevicesList; RemoteDevicesList.append(info);
I do not understand why I am getting this error.
/mnt/07b7c3f8-0efb-45ab-8df8-2a468771de1f/PROJECTS/RECOVERY/BLUETOOTH/SOURCE/CCC_SOURCE/terminal_SERIAL_BLUETOOTH_VER_1/terminal_SERIAL_BLUETOOTH/terminal_BLUETOOTH/mainwindow_BT_Terminal.cpp:799: error: no matching member function for call to 'append' mainwindow_BT_Terminal.cpp:799:23: error: no matching member function for call to 'append' RemoteDevicesList.append(info); ~~~~~~~~~~~~~~~~~~^~~~~~ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qlist.h:210:10: note: candidate function not viable: no known conversion from 'const QBluetoothDeviceInfo' to 'QBluetoothDeviceInfo *const' for 1st argument void append(const T &t); ^ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qlist.h:211:10: note: candidate function not viable: no known conversion from 'const QBluetoothDeviceInfo' to 'const QList<QBluetoothDeviceInfo *>' for 1st argument void append(const QList<T> &t); ^
could somebody PLEASE explain to me why I am getting the error and OFFER A SOLUTION ?
PS I do not need a confirmation that I am getting an error, I need a solution.
Thanks
why I am getting the error
Your list holds pointers and you're trying to pass an object reference to
append()
.OFFER A SOLUTION
Change the list type to match what you're passing or change what you're passing to match the list type.
-
why I am getting the error
Your list holds pointers and you're trying to pass an object reference to
append()
.OFFER A SOLUTION
Change the list type to match what you're passing or change what you're passing to match the list type.
wrote on 11 Feb 2023, 17:25 last edited by@Chris-Kawa Let me ask really stupid question - can I then select the "value" or pointer myself? I am having few issues implementing QList.
-
@Chris-Kawa Let me ask really stupid question - can I then select the "value" or pointer myself? I am having few issues implementing QList.
Lifetime Qt Championwrote on 11 Feb 2023, 18:07 last edited by Chris Kawa 2 Nov 2023, 18:10@AnneRanch QList is a container. You put stuff in and you take stuff out of containers. I'm not sure I understand. What do you mean by "select a value"?
Example:
QList<QBluetoothDeviceInfo> RemoteDevicesList; //list of values RemoteDevicesList.append(info); //append a value (copy it into the back of the list) RemoteDevicesList.insert(17, info); //insert a copy of info at position 17 RemoteDevicesList.removeFirst(); //remove first element RemoteDevicesList.removeLast(); //remove last element RemoteDevicesList.removeOne(info); //remove first element which value equals info QBluetoothDeviceInfo i1 = RemoteDevicesList.first(); //copy first element out to i1 variable QBluetoothDeviceInfo i2 = RemoteDevicesList.last(); //copy first element out to i2 variable QBluetoothDeviceInfo i3 = RemoteDevicesList.at(42); //copy 42nd element out to i3 variable
The above is when you have a list of values. If you have a list of pointers it's pretty much the same, but you deal with addresses, so & when you want to take an address of an object and * when you want to get an object a pointer points to:
QList<QBluetoothDeviceInfo*> RemoteDevicesList; //list of pointers RemoteDevicesList.append(&info); //append an address of info object (pointer) to the back of the list RemoteDevicesList.insert(17, &info); //insert a pointer to the info object at position 17 QBluetoothDeviceInfo i1 = *RemoteDevicesList.first(); //take the first pointer from the list, dereference it with * and copy it to i1 variable QBluetoothDeviceInfo* i2 = RemoteDevicesList.first(); //take the first pointer from the list and copy it to i2 variable
Keep in mind that when you're dealing with pointers you have to make sure the objects they point to are still alive, otherwise the pointers will point to garbage and cause a crash when you try to access the objects through them.
-
@AnneRanch QList is a container. You put stuff in and you take stuff out of containers. I'm not sure I understand. What do you mean by "select a value"?
Example:
QList<QBluetoothDeviceInfo> RemoteDevicesList; //list of values RemoteDevicesList.append(info); //append a value (copy it into the back of the list) RemoteDevicesList.insert(17, info); //insert a copy of info at position 17 RemoteDevicesList.removeFirst(); //remove first element RemoteDevicesList.removeLast(); //remove last element RemoteDevicesList.removeOne(info); //remove first element which value equals info QBluetoothDeviceInfo i1 = RemoteDevicesList.first(); //copy first element out to i1 variable QBluetoothDeviceInfo i2 = RemoteDevicesList.last(); //copy first element out to i2 variable QBluetoothDeviceInfo i3 = RemoteDevicesList.at(42); //copy 42nd element out to i3 variable
The above is when you have a list of values. If you have a list of pointers it's pretty much the same, but you deal with addresses, so & when you want to take an address of an object and * when you want to get an object a pointer points to:
QList<QBluetoothDeviceInfo*> RemoteDevicesList; //list of pointers RemoteDevicesList.append(&info); //append an address of info object (pointer) to the back of the list RemoteDevicesList.insert(17, &info); //insert a pointer to the info object at position 17 QBluetoothDeviceInfo i1 = *RemoteDevicesList.first(); //take the first pointer from the list, dereference it with * and copy it to i1 variable QBluetoothDeviceInfo* i2 = RemoteDevicesList.first(); //take the first pointer from the list and copy it to i2 variable
Keep in mind that when you're dealing with pointers you have to make sure the objects they point to are still alive, otherwise the pointers will point to garbage and cause a crash when you try to access the objects through them.
wrote on 14 May 2023, 20:42 last edited by@Chris-Kawa I did not realize I made a duplicate post , so I am adding my goof here . Sorry for wasting space.
I think I got the concept of containers, but I am obviously missing something else.What am I missing in my code ?
Decaration
QList<const QBluetoothDeviceInfo* > RemoteDeviceInfo;
This works
RemoteDeviceInfo.append(&info);
// verify text = "!!!!!!!!! DEBUG verify RemoteDeviceInfo \n"; text += " name \n"; text += RemoteDeviceInfo.at(0)->name(); qDebug() << text; m_ui->plainTextEdit_8->appendPlainText(text); // m_ui->plainTextEdit_3->appendPlainText(text for(auto &device:RemoteDeviceInfo) { text = " BUILD range loop test "; m_ui->plainTextEdit_8->appendPlainText(text); text = " device name "; text += device->name(); m_ui->plainTextEdit_8->appendPlainText(text); }
when I attempt to verify RemoteDeviceInfo) using another method
it fails
if(!RemoteDeviceInfo.isEmpty()) passes OK
{
for (auto &device:RemoteDeviceInfo)
{
text = " For loop test ";
qDebug() << text;
text = " device address \n";
text += device->address().toString(); fails here
qDebug() << text;
m_ui->plainTextEdit_8->appendPlainText(text);
text = " device name \n";
text += device->name(); // >address().toString()
qDebug() << text;
m_ui->plainTextEdit_8->appendPlainText(text);} } }
My debug shows no contents in RemoteDeviceInfo when used in another method.