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. Assign default path to QFileDialog
Forum Updated to NodeBB v4.3 + New Features

Assign default path to QFileDialog

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.8k Views
  • 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.
  • H Offline
    H Offline
    HenkCoder
    wrote on last edited by
    #1

    Hello everyone,
    I'm making a notepad and I'm currently working on the open and save button.
    How do I set the default path of those to the Desktop?

    fileName = QFileDialog::getSaveFileName(this, tr("Save a file"), "C:/Users/<USER>/Desktop/", tr("Text File (*.txt);; RichTextFormat File (*.rtf);; All Files (*)"));
        }
    

    This doesn't work. How do I solve this?

    1 Reply Last reply
    0
    • H HenkCoder

      @Christian-Ehrlicher
      Can you just tell me how to make it?
      I wrote that code like yesterday and I don't remember what I did.
      Thank you for answering

      Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by Cobra91151
      #6

      @HenkCoder

      Hello!

      Check out my example:

       QString desktopPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
       qDebug() << "Desktop path: " << desktopPath;
       QString fileName = QFileDialog::getSaveFileName(this, QObject::tr("Save a file"), desktopPath, QObject::tr("Text File (*.txt);; RichTextFormat File (*.rtf);; All Files (*)"));
       qDebug() << fileName;
      
        if (!fileName.isEmpty()) {
            QFile *saveFile = new QFile(fileName);
            saveFile->open(QIODevice::WriteOnly);
            QTextStream saveStream(saveFile);
            saveStream << "This is a text example...";
            saveFile->close();
            saveFile->deleteLater();
        }
      

      My code will save the file to the Desktop location. Keep in mind, to save the actual file, you must use QFile with QTextStream. Happy coding!

      H 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        See QStandardPaths

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        H 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          See QStandardPaths

          H Offline
          H Offline
          HenkCoder
          wrote on last edited by
          #3

          @Christian-Ehrlicher
          I already tried it but it doesn't work

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @HenkCoder said in Assign default path to QFileDialog:

            but it doesn't work

            This is no error description... it works perfectly fine for me. Please show us your code.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            H 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @HenkCoder said in Assign default path to QFileDialog:

              but it doesn't work

              This is no error description... it works perfectly fine for me. Please show us your code.

              H Offline
              H Offline
              HenkCoder
              wrote on last edited by
              #5

              @Christian-Ehrlicher
              Can you just tell me how to make it?
              I wrote that code like yesterday and I don't remember what I did.
              Thank you for answering

              Cobra91151C 1 Reply Last reply
              0
              • H HenkCoder

                @Christian-Ehrlicher
                Can you just tell me how to make it?
                I wrote that code like yesterday and I don't remember what I did.
                Thank you for answering

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by Cobra91151
                #6

                @HenkCoder

                Hello!

                Check out my example:

                 QString desktopPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
                 qDebug() << "Desktop path: " << desktopPath;
                 QString fileName = QFileDialog::getSaveFileName(this, QObject::tr("Save a file"), desktopPath, QObject::tr("Text File (*.txt);; RichTextFormat File (*.rtf);; All Files (*)"));
                 qDebug() << fileName;
                
                  if (!fileName.isEmpty()) {
                      QFile *saveFile = new QFile(fileName);
                      saveFile->open(QIODevice::WriteOnly);
                      QTextStream saveStream(saveFile);
                      saveStream << "This is a text example...";
                      saveFile->close();
                      saveFile->deleteLater();
                  }
                

                My code will save the file to the Desktop location. Keep in mind, to save the actual file, you must use QFile with QTextStream. Happy coding!

                H 1 Reply Last reply
                1
                • Cobra91151C Cobra91151

                  @HenkCoder

                  Hello!

                  Check out my example:

                   QString desktopPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
                   qDebug() << "Desktop path: " << desktopPath;
                   QString fileName = QFileDialog::getSaveFileName(this, QObject::tr("Save a file"), desktopPath, QObject::tr("Text File (*.txt);; RichTextFormat File (*.rtf);; All Files (*)"));
                   qDebug() << fileName;
                  
                    if (!fileName.isEmpty()) {
                        QFile *saveFile = new QFile(fileName);
                        saveFile->open(QIODevice::WriteOnly);
                        QTextStream saveStream(saveFile);
                        saveStream << "This is a text example...";
                        saveFile->close();
                        saveFile->deleteLater();
                    }
                  

                  My code will save the file to the Desktop location. Keep in mind, to save the actual file, you must use QFile with QTextStream. Happy coding!

                  H Offline
                  H Offline
                  HenkCoder
                  wrote on last edited by
                  #7

                  @Cobra91151 Hey! Sorry for the late answer, I was busy lately. Alright, I'm going to try it, thank you for answering and happy coding to you too!

                  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