我正在使用Qt / C ++开发应用程序,此应用程序的目标只是定期扫描USB端口,如果应用程序找到特定的USB设备,它就会启动应用程序。
我使用过QTimer,但似乎无法正常工作。不同的搜索显示需要Q_OBJECT才能使其工作,但是一旦我添加Q_OBJECT,构建就失败了。
这是错误
Undefined symbols for architecture x86_64:
"vtable for UsbDetection", referenced from:
UsbDetection::UsbDetection() in usbdetection.o
UsbDetection::~UsbDetection() in usbdetection.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
这是标题
class UsbDetection : public QObject
{
Q_OBJECT
public:
UsbDetection();
~UsbDetection();
bool isConnected();
QTimer *activeTimer;
void StartAgent();
public slots:
void ScanUSB();
};
这里是类
的c代码UsbDetection::UsbDetection()
{
}
UsbDetection::~UsbDetection()
{
activeTimer->deleteLater();
}
void UsbDetection::StartAgent() {
activeTimer = new QTimer(0);
activeTimer->setSingleShot(false);
activeTimer->setInterval(1000);
connect(activeTimer, SIGNAL(timeout()), this, SLOT(UsbDetection::ScanUSB()));
activeTimer->start();
}
bool UsbDetection::isConnected() {
libusb_context *context = NULL;
libusb_device **list = NULL;
bool DeviceConnected = false;
int rc = 0;
ssize_t count = 0;
rc = libusb_init(&context);
count = libusb_get_device_list(context, &list);
if(count == 0) {
return DeviceConnected;
}
for (size_t idx = 0; idx < count; ++idx) {
libusb_device *device = list[idx];
libusb_device_descriptor desc = {0};
rc = libusb_get_device_descriptor(device, &desc);
if(rc != 0) {
return DeviceConnected;
}
if(desc.idVendor == PRODUCT_ID) {
DeviceConnected = true;
return DeviceConnected;
}
}
return DeviceConnected;
}
void UsbDetection::ScanUSB() {
if(isConnected() == true) {
QString agentExecutableDirPath = QApplication::applicationDirPath();
QString fileBrowserExecutablePath = agentExecutableDirPath + "/../../../../MacOS/connect";
QProcess *process = new QProcess();
process->startDetached(fileBrowserExecutablePath);
}
}
我不明白为什么构建失败。