#include "usbdetectdialog.h" #include #include #include #include #include #include #include #include #include UsbDetectDialog::UsbDetectDialog(QWidget *parent) : BaseDlg(parent) { setAttribute(Qt::WA_DeleteOnClose); setWindowTitle(tr("Usb upgrade program")); resize(240, 220); auto vBox = new QVBoxLayout(this); auto hBox = new QHBoxLayout(); hBox->addStretch(); auto bnClose = new QPushButton("X"); bnClose->setProperty("ssType","progManageTool"); bnClose->setFixedWidth(32); connect(bnClose, &QPushButton::clicked, this, &BaseDlg::close); hBox->addWidget(bnClose); vBox->addLayout(hBox); hBox = new QHBoxLayout(); hBox->addWidget(new QLabel(tr("Password"))); fdPassword = new QLineEdit(); fdPassword->setEchoMode(QLineEdit::Password); fdPassword->setPlaceholderText(tr("Input password")); hBox->addWidget(fdPassword); vBox->addLayout(hBox); fdDrives = new QListWidget(); fdDrives->setSelectionRectVisible(true); fdDrives->setProperty("ssType", "usbList"); fdDrives->setProperty("ssName", "usbListName"); vBox->addWidget(fdDrives); hBox = new QHBoxLayout(); hBox->addStretch(); auto bnOk = new QPushButton(tr("OK")); bnOk->setProperty("ssType","progManageTool"); connect(bnOk, &QPushButton::clicked, this, [this] { auto selects = fdDrives->selectedItems(); if(selects.count() > 0) { for(auto select : selects) emit acceptData(select->text(), fdPassword->text()); accept(); return; } if(fdDrives->count() <= 0) QMessageBox::warning(this, tr("Tip"),tr("No checked USB device")); else QMessageBox::warning(this, tr("Tip"),tr("please select usb device in list")); }); hBox->addWidget(bnOk); hBox->addStretch(); auto bnCancel = new QPushButton(tr("Cancel")); bnCancel->setProperty("ssType","progManageTool"); connect(bnCancel, &QPushButton::clicked, this, &BaseDlg::reject); hBox->addWidget(bnCancel); hBox->addStretch(); vBox->addLayout(hBox); detectDriver(); timerId = startTimer(3000); } void UsbDetectDialog::timerEvent(QTimerEvent *e) { if(e->timerId() == timerId) detectDriver(); } void UsbDetectDialog::detectDriver() { auto drives = QDir::drives(); //获取当前系统的盘符 QStringList movables; for(auto drive : drives) if(GetDriveType((WCHAR *) drive.filePath().utf16())==2) movables.append(drive.filePath()); if(movables.size() == lastCnt) return; if(movables.size() > lastCnt) { for(auto movable : movables) { for(int n=0; n < fdDrives->count(); n++) if(movable == fdDrives->item(n)->text()) goto exist; fdDrives->addItem(movable); exist:; } } else { fdDrives->clear(); fdDrives->addItems(movables); } lastCnt = movables.size(); if(lastCnt==1) { QListWidgetItem *item = fdDrives->item(0); item->setSelected(true); } }