Results 1 to 13 of 13

Thread: QFileDialog how to save???

  1. #1
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QFileDialog how to save???

    Hi,

    i'm trying to implement QFileDialog saving function.i use Mac OS QT 4.6

    What i fo is:

    void Nopad::savefile()
    {
    QFileDialog::getSaveFileName(this,
    tr("Save file"), "/VOLUMES/", tr("Image Files (*.txt )"));

    }

    The question is: how to set the text from textEdit to be saved in .txt file?textEdit is generated in ui_nopad.h

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFileDialog how to save???

    You need a QTextStream. See the sample code in the docs.

  3. #3
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: QFileDialog how to save???

    Here try this
    Step 1. Let user save .txt file using QFileDialog::getSaveFileName
    Step 2. above function will return file name of saved file
    Step 3. Open it in using QTextStream or QFile and dump all your data in it.

  4. #4
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QFileDialog how to save???

    Thanks a lot

  5. #5
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QFileDialog how to save???

    Quote Originally Posted by vishwajeet.dusane View Post
    Here try this
    Step 1. Let user save .txt file using QFileDialog::getSaveFileName
    Step 2. above function will return file name of saved file
    Step 3. Open it in using QTextStream or QFile and dump all your data in it.
    Could you give me an example with source....

  6. #6
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFileDialog how to save???

    First let the user select a filename.
    Qt Code:
    1. /******************************************************************************
    2. saveImage
    3. Opens a dialog where the user can select a path and filename where to save
    4. the screen image.
    5. On entry, we stop the timer to hold the actual screen.
    6. On exit, we start the timer again.
    7. ******************************************************************************/
    8. void MainWindow::saveImage()
    9. {
    10. refreshTimer->stop();
    11.  
    12. QString fileName =
    13. QFileDialog::getSaveFileName(this, tr("Save Image"),
    14. "untitled.png", tr("Images (*.png)"));
    15.  
    16. // you may forget this:
    17. if (fileName != "")
    18. ui.scopeWindow->saveImage(fileName);
    19. // and insert the following code her
    20.  
    21.  
    22. refreshTimer->start();
    23. }
    To copy to clipboard, switch view to plain text mode 
    In this piece of code I'm saving an image, so skip that part and the timer-parts. Only use the getting the filename.


    Then save something to a textfile:
    Qt Code:
    1. QFile data(fileName);
    2. if (data.open(QFile::WriteOnly | QFile::Truncate)) {
    3. QTextStream out(&data);
    4. out << "some_text";
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by boudie; 9th February 2010 at 15:21.

  7. #7
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QFileDialog how to save???

    Yes it creates file but i try to insert the text from text Edit doing the following:

    void Nopad::savefile()
    {

    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
    "/VOLUMES/",
    tr("Text (*.txt)"));

    QFile data(fileName);
    if (data.open(QFile::WriteOnly | QFile::Truncate)) {
    QTextStream out(&data);
    out << ui->textEdit->setText();
    }


    }

    It gives me an error

  8. #8
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFileDialog how to save???

    You made a little mistake.

    Change:
    out << ui->textEdit->setText();

    into:
    out << ui->textEdit->text();

  9. #9
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QFileDialog how to save???

    Quote Originally Posted by boudie View Post
    You made a little mistake.

    Change:
    out << ui->textEdit->setText();

    into:
    out << ui->textEdit->text();

    The following error appears: class QtextEdit has no member function text() .....

  10. #10
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QFileDialog how to save???

    any suggestions about what function to use???

  11. #11
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFileDialog how to save???

    out << ui->textEdit->toPlainText();

  12. #12
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QFileDialog how to save???

    SUPERB finally it works , now the problem is why the saved file has no extension .txt?How to make it with .txt extension?

  13. #13
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFileDialog how to save???

    I'm a novice too but does this help its from the textedit demo example code?
    Qt Code:
    1. bool TextEdit::fileSaveAs()
    2. {
    3. QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
    4. QString(), tr("ODF files (*.odt);;HTML-Files (*.htm *.html);;All Files (*)"));
    5. if (fn.isEmpty())
    6. return false;
    7. if (! (fn.endsWith(".odt", Qt::CaseInsensitive) || fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)) )
    8. fn += ".odt"; // default
    9. setCurrentFileName(fn);
    10. return fileSave();
    11. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to save file with QFileDialog
    By pnikolov in forum Qt Programming
    Replies: 11
    Last Post: 1st June 2012, 10:23
  2. qt save
    By doss in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2009, 07:53
  3. QFileDialog for choosing filename to save data
    By araglin in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2009, 08:46
  4. How set default file to save in QFileDialog
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2009, 12:09
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.