Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to create a custom Save File Window



  • Hello, im working on a app that requires saving files and i want to create a window that takes cares itself of saving the file i want.

    I'll explain : i am using the getSaveFileName() method in order to save my file, but this method opens a Desktop window, and this is what i dont want. i would like that my window takes cares itself of selecting the path, and the name of the file, and then when i click on "save" there is no desktop window popping up.

    Does anyone knows a way to do this ?



  • @aftalib
    So if you really do not want to use QFileDialog, don't use it! Then it's up to you to write a suitable replacement yourself. But that means that if, say, you still want the user to browse to a directory for saving you must construct something suitable. If all you want is the user to type a full path, or type a directory path plus a filename, you could use your own QLineEdits.

    QFileDialog does not do any opening or saving. It only allows the user to pick a path and returns that. It's up to your caller what it chooses to do with that.

    What you might want instead is your own dialog/window with whatever of your own it, plus a QLineEdit and an associated button. Clicking the button invokes the QFileDialog. Selecting a file there and pressing OK copies the path back into your invoking dialog. Your invoking dialog has all the code for actually doing the save or load.


  • Lifetime Qt Champion

    Hi
    do note that with QFileDialog
    you can set up both filename and path before showing it to the user.
    Then the user can just press save to save in the default location the app told it.

    Also, many users including my self do not like custom save dialogs.
    its just annoying as they often have missing features, cant browse shares or is limited in many other ways.

    but if you actually mean just to allow the app to save to the same location and the user don't even have to specify the filename
    then it's another story.



  • @JonB the thing is, i would like do to something like this : 689b46ff-0beb-4a53-a597-3970a855f8fe-image.png

    (this window is not made using Qt)

    so as you can see, the user chooses a directory among those available (thomson, etc..), types the name of its file, clicks on save and that's it.

    I think that im seeing what you're explaining but i dont get the part with QLineEdit, i dont think i ever worked with those. Do you think you would have an example of code using it with a similar case ?

    Thank you



  • @mrjj Yes i know that i can choose the filename and the path with QFileDialog but i really need a window for this..

    I personnaly would prefer the classic window but i'm not doing this for myself, someone told me specifically to do it like this ahah


  • Moderators

    @aftalib Qt has a Dir (Tree)View example

    https://doc.qt.io/qt-5/qtwidgets-itemviews-dirview-example.html

    that should give you a really good starting point to implement/create what you want



  • @J-Hilk oh yeah thanks this is exactly what i'm looking for.

    However i am trying to get DirView into a QGraphicsScene, like this : ```
    QGraphicsScene* scene = new QGraphicsScene(QRect(100, 100, 400, 200));

      treeSave->setHeaderHidden(true);
      QFileSystemModel model;
      model.setRootPath(QDir::currentPath());
      treeSave->setModel(&model);
    
    
      // Demonstrating look and feel features
      treeSave->setAnimated(false);
      treeSave->setIndentation(20);
      treeSave->setSortingEnabled(true);
      const QSize availableSize = treeSave->screen()->availableGeometry().size();
      treeSave->resize(availableSize / 2);
      treeSave->setColumnWidth(0, treeSave->width() / 3);
    
    
    
      treeSave->setWindowTitle(QObject::tr("Dir View"));
    

    treeSave->setGeometry(100,100,400,200);

    QVBoxLayout *lay = new QVBoxLayout;
    lay->addWidget(treeSave);
    view = new QGraphicsView();
    view->setLayout(lay);
    
    view->setScene(scene);
    view->setWindowTitle("Save your file");
    view->setGeometry(QRect(520, 520, 400, 200));
    view->setContentsMargins(0,0,0,0);
    view->show();
    
    
    but this just gives me a blank window..

  • Moderators

    @aftalib said in How to create a custom Save File Window:

    QGraphicsView

    I have no experience myself with this. Can you even add a simple QWidget to a QGraphicsScene ? I thought it has to be derived from QGraphicsItem

    If you can not simply add QWdigets to a Scene then your approach will not work.


  • Lifetime Qt Champion

    @J-Hilk said in How to create a custom Save File Window:

    QGraphicsScene

    Hi
    you do it via
    QGraphicsProxyWidget
    https://doc.qt.io/qt-5/qgraphicsscene.html#addWidget

    addWidgets constructs one for you.

    @aftalib

    you have QFileSystemModel model; as a local variable

    so when i see
    treeSave->setModel(&model);

    i fear it will run out of scope.


Log in to reply