Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why QFileDialog::getExistingDirectory returned non native directory?(pcap directory below with '/', not '\'))
Forum Updated to NodeBB v4.3 + New Features

Why QFileDialog::getExistingDirectory returned non native directory?(pcap directory below with '/', not '\'))

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 787 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jsulmJ jsulm

    @angelyouyou What exact folder did you select for pcap? Is it on network drive?

    A Offline
    A Offline
    angelyouyou
    wrote on last edited by angelyouyou
    #3

    @jsulm
    sorry. I have updated my picture to compare the different.
    7b93d981-5a2d-4e3f-a2bb-cd79b50e1c47-image.png
    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);
    }
    
    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • A angelyouyou

      @jsulm
      sorry. I have updated my picture to compare the different.
      7b93d981-5a2d-4e3f-a2bb-cd79b50e1c47-image.png
      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);
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #4

      @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).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A angelyouyou

        @jsulm
        sorry. I have updated my picture to compare the different.
        7b93d981-5a2d-4e3f-a2bb-cd79b50e1c47-image.png
        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);
        }
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #5

        @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());
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        A 1 Reply Last reply
        2
        • J.HilkJ J.Hilk

          @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());
          
          A Offline
          A Offline
          angelyouyou
          wrote on last edited by
          #6

          @J-Hilk
          Thanks for your help.
          I have used getExistingDirectoryUrl and toLocalFile, but the same case.
          c1ece0d5-805a-42be-9117-8a22fe13148c-image.png

          
          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);
          }
          
          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #7

            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));
            }
            
            J.HilkJ 1 Reply Last reply
            4
            • Chris KawaC Chris Kawa

              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));
              }
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #8

              @Chris-Kawa oh my goodness, QDir::toNativeSeparators is static 🤦‍♂️
              I never realised!


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              JonBJ A 2 Replies Last reply
              0
              • J.HilkJ J.Hilk

                @Chris-Kawa oh my goodness, QDir::toNativeSeparators is static 🤦‍♂️
                I never realised!

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #9

                @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 in QDir for acting on a specified path :)

                J.HilkJ A 2 Replies Last reply
                0
                • JonBJ JonB

                  @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 in QDir for acting on a specified path :)

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #10

                  @JonB The first time I used QDir, I was so disappointed with it, after I noticed that exists and mkdir/mkpath aren't static , that I never bothered to check what else actually is static. 😉


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  1
                  • J.HilkJ J.Hilk

                    @Chris-Kawa oh my goodness, QDir::toNativeSeparators is static 🤦‍♂️
                    I never realised!

                    A Offline
                    A Offline
                    angelyouyou
                    wrote on last edited by angelyouyou
                    #11

                    @J-Hilk said in Why QFileDialog::getExistingDirectory returned non native directory?(pcap directory below with '/', not '\')):

                    toNativeSeparators

                    Thanks.That is OK.
                    a7839649-6128-426c-9d86-9f9aeb478baa-image.png

                    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);
                    }
                    
                    1 Reply Last reply
                    1
                    • JonBJ JonB

                      @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 in QDir for acting on a specified path :)

                      A Offline
                      A Offline
                      angelyouyou
                      wrote on last edited by angelyouyou
                      #12
                      This post is deleted!
                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved