104 lines
3.1 KiB
C++
104 lines
3.1 KiB
C++
#include "form_usb.h"
|
||
#include "ui_form_usb.h"
|
||
#include <windows.h>
|
||
#include <dbt.h>
|
||
#include <qtimer.h>
|
||
#include "form_seting.h"
|
||
#include "qmessagebox.h"
|
||
|
||
Form_usb::Form_usb(QWidget *parent)
|
||
: QWidget(parent)
|
||
, ui(new Ui::Form_usb)
|
||
{
|
||
ui->setupUi(this);
|
||
this->winId();//必须,不然nativeEvent不触发
|
||
// myusb.hello();
|
||
// QStringList devices = myusb.listUsbDevices(LIBUSB_CLASS_PRINTER);
|
||
// for (const QString &device : devices) {
|
||
// ui->textEdit->append(device);
|
||
// }
|
||
|
||
connect(this, &Form_usb::usbDeviceChanged, this, &Form_usb::onUsbDeviceChanged);
|
||
}
|
||
|
||
Form_usb::~Form_usb()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
bool Form_usb::nativeEvent(const QByteArray &eventType, void *message, qintptr *result) {
|
||
Q_UNUSED(eventType)
|
||
Q_UNUSED(result)
|
||
|
||
MSG *msg = static_cast<MSG *>(message);
|
||
if (msg->message == WM_DEVICECHANGE) {
|
||
if (msg->wParam == DBT_DEVICEARRIVAL) {
|
||
qDebug() << "USB Device Inserted!";
|
||
ui->textEdit->append("USB Device Inserted!");
|
||
emit usbDeviceChanged(true); // 发送信号
|
||
} else if (msg->wParam == DBT_DEVICEREMOVECOMPLETE) {
|
||
qDebug() << "USB Device Removed!";
|
||
ui->textEdit->append("USB Device Removed!");
|
||
}
|
||
}
|
||
return QWidget::nativeEvent(eventType, message, result);
|
||
}
|
||
|
||
|
||
void Form_usb::on_pushButton_clicked()
|
||
{
|
||
QMessageBox::information(this, "USB打印机", "连接成功");
|
||
emit usbDeviceChanged(true); // 发送信号
|
||
}
|
||
|
||
void Form_usb::onUsbDeviceChanged(bool inserted) {
|
||
if (inserted) {
|
||
qDebug() << "USB Device Inserted!";
|
||
} else {
|
||
qDebug() << "USB Device Removed!";
|
||
}
|
||
QTimer::singleShot(5000, this, [this]() {
|
||
qDebug() << "2 秒后执行的任务";
|
||
ui->textEdit->clear();
|
||
ui->textEdit->append("USB Device Inserted!");
|
||
|
||
// if(myusb.ctx == nullptr) {
|
||
// ui->textEdit->append("myusb.ctx = nullptr");
|
||
// }
|
||
// libusb_device_handle *handle = libusb_open_device_with_vid_pid(myusb.ctx, 0xfe6, 0x8800);
|
||
// if (handle == nullptr) {
|
||
// qDebug() << "Failed to open device!";
|
||
// ui->textEdit->append("Failed to open USB device!");
|
||
// }else{
|
||
// ui->textEdit->append("Successfully to open USB device");
|
||
// }
|
||
if(Printer_Usb.Usb_Open(0x0fe6,0x8800) == true)
|
||
{
|
||
const char data[] = "Hello, USB Printer!"; // 一个字符串数组
|
||
DWORD dataSize = sizeof(data); // 获取数组大小
|
||
ui->textEdit->append(Printer_Usb.DevicePath);
|
||
ui->textEdit->append("Successfully to open USB device");
|
||
if(Printer_Usb.Usb_Data_Send(data,dataSize) == true)
|
||
{
|
||
ui->textEdit->append("Successfully to Usb_Data_Send");
|
||
}else{
|
||
ui->textEdit->append("Failed to Usb_Data_Send");
|
||
}
|
||
Printer_Usb.Usb_Close();
|
||
}else{
|
||
ui->textEdit->append("Failed to open USB device!");
|
||
}
|
||
|
||
});
|
||
|
||
}
|
||
|
||
void Form_usb::on_pushButton_2_clicked()
|
||
{
|
||
Form_Seting settingWindow(this);
|
||
settingWindow.exec(); // 使用 exec(),阻塞主窗口
|
||
}
|
||
|
||
|
||
|