【问题标题】:How to list /dev/sda usb storages mounted with a combobox如何列出使用组合框安装的 /dev/sda usb 存储
【发布时间】:2019-01-30 16:51:47
【问题描述】:

我正在寻找在插入 USB 存储路径时显示它们的方法,该路径必须显示在组合框中(在我使用 qt creator (qt 5.9) 设计的 gui 中)。我一直在寻找如何做到这一点,但我没有找到任何东西。我想要的是这样的:

https://catenarios2.files.wordpress.com/2012/11/002.jpg

你能帮我继续我的项目吗?如果你能提供一个例子,我将非常感激。

非常感谢

【问题讨论】:

  • 也许,如果对您来说足够的话,可以通过 QProcess 启动适当的 Linux 命令,例如 lsblk -o KNAME(然后解析结果)?使用 libudev API 或 libsysfs 也是一种选择,但需要更多的努力。
  • 这可能是一个体贴的选择,我会研究如何将它实现到我的程序中,非常感谢你:)。我怎么能得到mountpint呢?最后,这是我真正需要的。

标签: list qt combobox usb udev


【解决方案1】:

基本思想是一样的——通过QProcess 启动Linux 工具并解析结果。这是一个简单的草图:

#include <QCoreApplication>
#include <QProcess>
#include <QDebug>

#include <usb.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QProcess devList;
    devList.start("lsblk", QStringList() << "-o" << "KNAME");
    if (!devList.waitForStarted())
        return false;

    if (!devList.waitForFinished())
        return false;

    QString result = QString(devList.readAll());
    qDebug() << result;

    return a.exec();
}

您可以使用任何其他合适的命令(很容易找到它们)并且应该改进解析,当然,但通常都是一样的。

AFAIK,挂载点可以从/proc/mounts 获得类似...

#include <QCoreApplication>

#include <mntent.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    struct mntent *ent;
    FILE *aFile;

    aFile = setmntent("/proc/mounts", "r");
    if (aFile == NULL) {
      perror("setmntent");
      exit(1);
    }
    while (NULL != (ent = getmntent(aFile))) {
      printf("%s %s\n", ent->mnt_fsname, ent->mnt_dir);
    }
    endmntent(aFile);

    return a.exec();
}

catlaunching 或其他更好,也取自一些sn-p,应该改进。 最后,如果您需要美元设备信息,它可能类似于...

#include <QCoreApplication>

#include <usb.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    struct usb_bus *bus;
    struct usb_device *dev;
    usb_init();
    usb_find_busses();
    usb_find_devices();
    for (bus = usb_busses; bus; bus = bus->next)
    {
        for (dev = bus->devices; dev; dev = dev->next)
        {
            printf("Trying device %s/%s\n", bus->dirname, dev->filename);
            printf("\tID_VENDOR = 0x%04x\n", dev->descriptor.idVendor);
            printf("\tID_PRODUCT = 0x%04x\n", dev->descriptor.idProduct);
        }
    }

    return a.exec();
}

这需要sudo apt-get libusb-dev + 用-lusb 编译。

Qt 在问题中的作用并不大,可能还有更基本的“编码”解决方案,但希望这会推动您找到合适的解决方案。

【讨论】:

  • 哇,非常感谢您的出色回答,明天我将尝试在我的程序中实施:D
猜你喜欢
  • 1970-01-01
  • 2013-08-27
  • 2022-06-15
  • 2015-01-02
  • 1970-01-01
  • 2012-09-01
  • 2022-07-22
  • 2018-04-06
  • 1970-01-01
相关资源
最近更新 更多