I am trying to connect the addDevice() function from my Device class to my connectButton on my GUI. However no matter how I try and code the connect function, it does not work. Currently it is giving me the error: primary expression expected before the ',' token.
我试图将我的Device类中的addDevice()函数连接到我的GUI上的connectButton。但无论我如何尝试编写连接功能,它都不起作用。目前它给了我错误:','令牌之前预期的主表达式。
I have tried the follow syntaxes:
我尝试了以下语法:
connect(ui->connectButton,SIGNAL(clicked(bool)),d,SLOT(startDeviceDiscovery()));
connect(ui->connectButton,SIGNAL(clicked(bool)),d,&Device::startDeviceDiscovery());
connect(ui->connectButton,SIGNAL(clicked(bool)),this,SLOT(startDeviceDiscovery()));
connect(ui->connectButton,SIGNAL(clicked(bool)),this,Device::startDeviceDiscovery());
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QObject>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth>
#include <QDebug>
#include <QtWidgets>
#include "device.h"
#include "deviceinfo.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Device d;
connect(ui->connectButton,SIGNAL(clicked(bool)),Device,SLOT(startDeviceDiscovery()));
//
.
.
.
}
Device.h
#ifndef DEVICE_H
#define DEVICE_H
#include <QObject>
#include <qbluetoothglobal.h>
#include <qbluetoothlocaldevice.h>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QLowEnergyController>
#include <QBluetoothDeviceInfo>
#include <QBluetoothServiceInfo>
#include "deviceinfo.h"
#include <QList>
#include <QVariant>
class Device : public QObject
{
Q_OBJECT
public:
explicit Device(QObject *parent = 0);
QVariant name();
~Device();
signals:
void address(QVariant);
public slots:
void startDeviceDiscovery();
void connectDeivce(const QString &address);
};
#endif // DEVICE_H
1 个解决方案
#1
0
For a connect you need a pointer, and you created your Device on the constructor, this means that just after the connect is used, constructor would finish, and this would destroy your device, disconnecting the function.
对于连接,您需要一个指针,并且您在构造函数上创建了您的设备,这意味着在使用连接之后,构造函数将完成,这将破坏您的设备,断开该功能。
Correct syntax:
Device *myDevice = new Device();
connect(
ui->connectButton, // the pointer of the signal emitter
&QPushButton::clicked, // the signal you are interested
myDevice, // the pointer to receiving end
&Device::startDeviceDiscovery); // what you wanna trigger
you mixed every possibility in your tests, but in all of them you treated the slot as a method, calling the () operator. you can't call the call operator, on the connect you are passing the pointer of the method to a function that will call that for you when the time is right.
你在测试中混合了所有可能性,但在所有这些中你将插槽视为一种方法,调用()运算符。你不能调用调用操作符,在连接上你将方法的指针传递给一个函数,当时间正确时会为你调用它。
#1
0
For a connect you need a pointer, and you created your Device on the constructor, this means that just after the connect is used, constructor would finish, and this would destroy your device, disconnecting the function.
对于连接,您需要一个指针,并且您在构造函数上创建了您的设备,这意味着在使用连接之后,构造函数将完成,这将破坏您的设备,断开该功能。
Correct syntax:
Device *myDevice = new Device();
connect(
ui->connectButton, // the pointer of the signal emitter
&QPushButton::clicked, // the signal you are interested
myDevice, // the pointer to receiving end
&Device::startDeviceDiscovery); // what you wanna trigger
you mixed every possibility in your tests, but in all of them you treated the slot as a method, calling the () operator. you can't call the call operator, on the connect you are passing the pointer of the method to a function that will call that for you when the time is right.
你在测试中混合了所有可能性,但在所有这些中你将插槽视为一种方法,调用()运算符。你不能调用调用操作符,在连接上你将方法的指针传递给一个函数,当时间正确时会为你调用它。