I’m working on a Linux desktop application that needs to open a USB serial port, typically /dev/ttyUSB0 or /dev/ttyUSB1. I’m using QFileDialog to let the user select the file:
我正在开发一个需要打开USB串口的Linux桌面应用程序,通常是/ dev / ttyUSB0或/ dev / ttyUSB1。我正在使用QFileDialog让用户选择文件:
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setFilter(QDir::System | QDir::AllEntries | QDir::Hidden);
dialog.setViewMode(QFileDialog::Detail);
QStringList fileNames;
if (dialog.exec())
fileNames = dialog.selectedFiles();
When I direct the FileDialog to /dev, none of the files that I can see by typing “ls /dev -al” are there. The directories show up, but for example, this file doesn’t:
当我将FileDialog指向/ dev时,通过键入“ls / dev -al”可以看到的所有文件都没有。目录显示,但例如,此文件不会:
$ ls -al /dev/ttyS0
crw-rw---- 1 root dialout 4, 64 2011-10-09 10:47 /dev/ttyS0
My user is a member of the dialout group:
我的用户是拨出组的成员:
$ groups
luke adm dialout cdrom audio video plugdev users fuse netdev bluetooth lpadmin admin sambashare
I’ve tried adding QDir::Readable and QDir::Writable and the above file still doesn’t show up. What am I doing wrong?
我已经尝试添加QDir :: Readable和QDir :: Writable,上面的文件仍然没有显示出来。我究竟做错了什么?
1 个解决方案
#1
2
It amazes me how often people don't answer the question that was originally asked. I'll try not to do that here, if I can. I've done some homework on this problem since I'm having exactly the same issue. The short answer is that you can't use QFileDialog to reliably list and select nodes in "/dev". When you set the "QDIR::System" bit in the QFileDialog Filter using QFileDialog::setFilter(QDIR::System)
, you would expect that all the files in /dev would show up, but they do not. Admittedly, there are more entries than when it is not set, but most of the device nodes are still not displayed. It is clear that QFileDialog is doing some additional filtering behind the scenes, and that this filtering cannot be turned off in any obvious way.
令人惊讶的是,人们不经常回答最初提出的问题。如果可以的话,我会尽量不这样做。我已经完成了关于这个问题的一些功课,因为我遇到了完全相同的问题。简短的回答是您无法使用QFileDialog可靠地列出和选择“/ dev”中的节点。当您使用QFileDialog :: setFilter(QDIR :: System)在QFileDialog过滤器中设置“QDIR :: System”位时,您会希望/ dev中的所有文件都会显示,但它们不会显示。不可否认,条目比未设置时更多,但大多数设备节点仍未显示。显然,QFileDialog在幕后进行了一些额外的过滤,并且不能以任何明显的方式关闭此过滤。
On the other hand, if you use the QDir class with the QDir::System filter bit set, then in fact all of the /dev entries do appear in the entryList. For example, assuming that you already have a QComboBox named TTYDevices in your user interface, try something like this:
另一方面,如果您使用QDir类并设置了QDir :: System过滤器位,那么实际上所有/ dev条目都会出现在entryList中。例如,假设您的用户界面中已经有一个名为TTYDevices的QComboBox,请尝试以下操作:
DevDir=new QDir("/dev","tty*",QDir::Name,QDir::System);
ui->TTyDevices->addItems(DevDir->entryList());
Then use the standard signals from QComboBox to detect and act on selection of the desired device node. By the way, ui is the standard Qt pointer to the instance of your parent window class and should be set up in the Window's constructor. Just make sure that you don't reference it before the constructor calls ui->setupUi(this)
. If you do, the program will crash.
然后使用来自QComboBox的标准信号来检测并根据所需设备节点的选择进行操作。顺便说一句,ui是指向父窗口类实例的标准Qt指针,应该在Window的构造函数中设置。只需确保在构造函数调用ui-> setupUi(this)之前不引用它。如果你这样做,程序将崩溃。
This trick provides identical functionality to QFileDialog, with the additional features provided by accessing the QDir object directly. It does mean that you cannot easily have the same familiar, uniform interface provided QFileDialog, but it works and is remarkably easy to code.
此技巧为QFileDialog提供了相同的功能,并通过直接访问QDir对象提供了其他功能。它确实意味着您无法轻松拥有QFileDialog提供的相同熟悉的统一界面,但它的工作原理非常容易编码。
#1
2
It amazes me how often people don't answer the question that was originally asked. I'll try not to do that here, if I can. I've done some homework on this problem since I'm having exactly the same issue. The short answer is that you can't use QFileDialog to reliably list and select nodes in "/dev". When you set the "QDIR::System" bit in the QFileDialog Filter using QFileDialog::setFilter(QDIR::System)
, you would expect that all the files in /dev would show up, but they do not. Admittedly, there are more entries than when it is not set, but most of the device nodes are still not displayed. It is clear that QFileDialog is doing some additional filtering behind the scenes, and that this filtering cannot be turned off in any obvious way.
令人惊讶的是,人们不经常回答最初提出的问题。如果可以的话,我会尽量不这样做。我已经完成了关于这个问题的一些功课,因为我遇到了完全相同的问题。简短的回答是您无法使用QFileDialog可靠地列出和选择“/ dev”中的节点。当您使用QFileDialog :: setFilter(QDIR :: System)在QFileDialog过滤器中设置“QDIR :: System”位时,您会希望/ dev中的所有文件都会显示,但它们不会显示。不可否认,条目比未设置时更多,但大多数设备节点仍未显示。显然,QFileDialog在幕后进行了一些额外的过滤,并且不能以任何明显的方式关闭此过滤。
On the other hand, if you use the QDir class with the QDir::System filter bit set, then in fact all of the /dev entries do appear in the entryList. For example, assuming that you already have a QComboBox named TTYDevices in your user interface, try something like this:
另一方面,如果您使用QDir类并设置了QDir :: System过滤器位,那么实际上所有/ dev条目都会出现在entryList中。例如,假设您的用户界面中已经有一个名为TTYDevices的QComboBox,请尝试以下操作:
DevDir=new QDir("/dev","tty*",QDir::Name,QDir::System);
ui->TTyDevices->addItems(DevDir->entryList());
Then use the standard signals from QComboBox to detect and act on selection of the desired device node. By the way, ui is the standard Qt pointer to the instance of your parent window class and should be set up in the Window's constructor. Just make sure that you don't reference it before the constructor calls ui->setupUi(this)
. If you do, the program will crash.
然后使用来自QComboBox的标准信号来检测并根据所需设备节点的选择进行操作。顺便说一句,ui是指向父窗口类实例的标准Qt指针,应该在Window的构造函数中设置。只需确保在构造函数调用ui-> setupUi(this)之前不引用它。如果你这样做,程序将崩溃。
This trick provides identical functionality to QFileDialog, with the additional features provided by accessing the QDir object directly. It does mean that you cannot easily have the same familiar, uniform interface provided QFileDialog, but it works and is remarkably easy to code.
此技巧为QFileDialog提供了相同的功能,并通过直接访问QDir对象提供了其他功能。它确实意味着您无法轻松拥有QFileDialog提供的相同熟悉的统一界面,但它的工作原理非常容易编码。