Why QFileDialog::getExistingDirectory returned non native directory?(pcap directory below with '/', not '\'))
-
void Text2PcapWidget::on_toolButton_BrowserCapStoragePath_clicked() { QString pcapStoragePath = QFileDialog::getExistingDirectory(this, UI_RES_CONTROL_TEXT2WIDGET_PCAP_DIAG_TITLE, DIAG_DEFAULT_DIR); if (pcapStoragePath.isEmpty()) { return; } ui->lineEdit_CapStoragePath->setText(pcapStoragePath); }
-
@angelyouyou What exact folder did you select for pcap? Is it on network drive?
-
@jsulm
sorry. I have updated my picture to compare the different.
The wireshark path is obtained with the below code:void Text2PcapWidget::on_toolButton_BrowserWiresharkPath_clicked() { QString wireshakSelectedPath = QFileDialog::getOpenFileName(this, UI_RES_CONTROL_TEXT2WIDGET_WS_DIAG_TITLE, DIAG_DEFAULT_DIR); if (wireshakSelectedPath.isEmpty()) { return; } QMessageBox::information(this, UI_RES_CONTROL_MESSAGEBOX_INFO, wireshakSelectedPath); ui->lineEdit_WiresharkPath->setText(wireshakSelectedPath); }
-
@angelyouyou Qt internally uses / instead of \. So, you can use paths with / with Qt, even on Windows. Else, you can replace / with \ (https://doc.qt.io/qt-5/qstring.html#replace-3).
-
@angelyouyou thats normal, Qt as cross platform framework, uses internally / as path separators and will also always return those to you, until otherwise specified. I would suggest using
getExistingDirectoryUrl
:QUrl pcapStoragePath = QFileDialog::getExistingDirectoryUrl(this, UI_RES_CONTROL_TEXT2WIDGET_PCAP_DIAG_TITLE, DIAG_DEFAULT_DIR); if (pcapStoragePath.isEmpty()) { return; } ui->lineEdit_CapStoragePath->setText(pcapStoragePath.toLocalFile());
-
@J-Hilk
Thanks for your help.
I have used getExistingDirectoryUrl and toLocalFile, but the same case.
void Text2PcapWidget::on_toolButton_BrowserCapStoragePath_clicked() { QUrl pcapStorageUrl = QFileDialog::getExistingDirectoryUrl(this, UI_RES_CONTROL_TEXT2WIDGET_PCAP_DIAG_TITLE, QUrl(DIAG_DEFAULT_DIR)); QString pcapStoragePath = pcapStorageUrl.toLocalFile(); if (pcapStoragePath.isEmpty()) { return; } QDir::fromNativeSeparators(pcapStoragePath); ui->lineEdit_CapStoragePath->setText(pcapStoragePath); }
-
As mentioned above Qt uses / for all of its path handling. Use QDir::toNativeSeparators to convert the path for displaying in native format.
void Text2PcapWidget::on_toolButton_BrowserCapStoragePath_clicked() { QUrl pcapStorageUrl = QFileDialog::getExistingDirectoryUrl(this, UI_RES_CONTROL_TEXT2WIDGET_PCAP_DIAG_TITLE, QUrl(DIAG_DEFAULT_DIR)); QString pcapStoragePath = pcapStorageUrl.toLocalFile(); if (pcapStoragePath.isEmpty()) { return; } ui->lineEdit_CapStoragePath->setText(QDir::toNativeSeparators(pcapStoragePath)); }
-
@Chris-Kawa oh my goodness,
QDir::toNativeSeparators
is static 🤦♂️
I never realised! -
@J-Hilk
Since the method takes a parameter of the path to convert,QString QDir::toNativeSeparators(const QString &pathName)
, you can see it's a static rather than an instance method! Along with a whole bunch of "utility" methods inQDir
for acting on a specified path :) -
@J-Hilk said in Why QFileDialog::getExistingDirectory returned non native directory?(pcap directory below with '/', not '\')):
toNativeSeparators
Thanks.That is OK.
void Text2PcapWidget::on_toolButton_BrowserCapStoragePath_clicked() { QString pcapStoragePath = QFileDialog::getExistingDirectory(this, UI_RES_CONTROL_TEXT2WIDGET_PCAP_DIAG_TITLE, DIAG_DEFAULT_DIR); pcapStoragePath = QDir::toNativeSeparators(pcapStoragePath); if (pcapStoragePath.isEmpty()) { return; } ui->lineEdit_CapStoragePath->setText(pcapStoragePath); }
-
This post is deleted!