[SOLVED] QtPyVCP - save() Slot - QFSFileEngine::open: No file name specified

More
18 Dec 2019 13:49 - 18 Dec 2019 17:42 #152843 by Donb9261
From what I am gathering there is little info on this message. But, it appears using the slot as is:

    @Slot()
    def save(self):
        save_file = QFile(self.filename)

        result = save_file.open(QFile.WriteOnly)
        if result:
            save_stream = QTextStream(save_file)
            save_stream << self.text()

            save_file.close()

does not extract the file name and path of the currently open .ngc file loaded in the editor. I have tried the save button in probe_basic as well and see the same error when I edit the open gcode file and then click save. The debug message in the terminal drops: QFSFileEngine::open: No file name specified.

This means that save_file is returning empty.

Is it an issue with the def or an issue with usage of QFile?

Has anyone had this issue? If so, what is the fix?

Also, it appears that save_as() saves the file into the main vcp directory vice the nc_files directory. Does the path need to be appended in the gcodeeditor.py to include the full path to where one would want the files saved. If so, any ideas on how to make the change so that if I use save_as() it saves to say /home/me/linuxcnc/configs/myvcp/nc_files.

I would like both save() and save_as() to work but currently neither performs as expected.

Any help is appreciated.

EDIT

Solution

I had two problems:

1) Edit and Save the open gcode file without having the FileSystemWidget in my UI
2) SaveAs() saves to the parent directory not nc_files directory.

Solutions:

Edit Save

1) Add new slot named saveMyFile(self). This adds a slot to the signal/slot editor. Set a new slot as follows: Sender = Your Save Button Signal = clicked() Receiver = gcodeeditor Slot = saveMyFile()

2) Add the following code to your gcodeeditor.py file below the save() slot def.


    @Slot()
    def saveMyFile(self):
        save_file = QFile(STATUS.file())

        result = save_file.open(QFile.WriteOnly)
        if result:
            save_stream = QTextStream(save_file)
            save_stream << self.text()

            save_file.close()

Next, save all files and reload your vcp. Set your edit mode to true by whatever button you have. Mine is EDIT. Make any edits you need to the file in the GCodeEditor and then click SAVE button. The file will be saved over the current file permanently and then reload to the top ready to run.

Save As

This one was bit tricky. Had to figure out how to extract the directory path from the current loaded file path. So, here is my solution.

Create a new slot called saveMyFileAs() and set up the slot in the Signal/Slot Editor for my/your SAVE AS button. You will need to reload the vcp into designer so it picks up the new slot. If you cannt see the new slot that is the issue.

Add the following code below the save_as() slot def in your gcodeeditor.py file. Comments explain what is happening:)


    @Slot()
    def saveMyFileAs(self):
        # Filename that will be entered in the dialog (some_file.ngc) Extension required!
        file_name = self.save_as_dialog(self.filename)
        # Check to make sure a filename was entered. No crashes please!
        if file_name is False:
            return
        # Get the current file path from Linuxcnc STATUS
        original_file = STATUS.file()
        # Print for debug only. Else comment out. 
        print(original_file)
        # Use os.path.split method to break the path into 2 parts >>
        # Head is everything up to the last / 
        # Tail is the actual filename. 
        # os.path.split builds a list with the two items [head, tail]
        # Like so.... ('/home/don/linuxcnc/configs/vcp2/nc_files', 'qtpyvcp.ngc') 
        head_tail = os.path.split(original_file)
        # Print for Debug only
        print(head_tail)
        # Here is the magic. The list is index like all list starting from 0
        # Make path equal to the first index or the head part of the list
        path = head_tail[0]
        # Print for Debug only        
        print(path)
        # Now we need to take the path (head) and give it the new (tail) and join them
        # to make a complete path with the new file name you entered.
        new_absolute_path = os.path.join(path, file_name)
        # Print for Debug only
        print(new_absolute_path)
        # Tell QFile what the new file name and path are
        new_file = QFile(new_absolute_path)
        # Open a new file with the new name and set it to write mode
        result = new_file.open(QFile.WriteOnly)
        # Check to make sure opening the file was successful.
        if result:
            # Now we need to pull out the text from the old file
            save_stream = QTextStream(new_file)
            # Send the text to the new file
            save_stream << self.text()
            # All done now close the file
            new_file.close()

Now save all files and relaunch your vcp. Open a gcode file. Click your Save As button. Enter the new file name. Click save in the dialog. Now you can use the OPEN button to select your new file and verify it both exists and is the same as the original.

I hope this helps. ;)

Don
Last edit: 18 Dec 2019 17:42 by Donb9261. Reason: Solution Found

Please Log in or Create an account to join the conversation.

More
18 Dec 2019 13:55 #152844 by Donb9261
Attachments:

Please Log in or Create an account to join the conversation.

More
18 Dec 2019 14:00 #152845 by Leon82
Try it with an original program,

I don't think it will save the qtpy engrave program..

Im pretty sure mine works as expected, but I'll double check later

Please Log in or Create an account to join the conversation.

More
18 Dec 2019 14:16 #152847 by Donb9261
IT only works if the file is opened through the FileSytemTable widget. Issue for me is I may not want to use that.

Using the FileSystemTable widget, double clicking or using the load gcode presents the path to the file_absolute_path QLabel. What I am hoping is to find a way to get that absolute path stored in a variable when the file is loaded using the open button. I know there is a way but I just have not found it.

It appears to not be a bug but a functional flow concern on my part. The file system widgets take considerable screen real-estate and I want preserve that as much as possible to simplify the interface.
The following user(s) said Thank You: Leon82

Please Log in or Create an account to join the conversation.

More
18 Dec 2019 16:33 #152854 by Donb9261
Found a way! Works great.

In the gcodeeditor.py file I added a new slot called saveMyFile(self). Then added the following and it works. I call in the current file from Linuxcnc and then set that as the QFile path. Now you can have save without using the FileSystemTable. Valuable screen real estate loss. Also, falls in line with Fanuc as far as not having to manually reload from an external editor.

Adding the extra slot allows the original functionality to work with the FileSytemTable. No breaking changes. :) Now just need to get the default directory setup for save_as(). Currently saves to main vcp directory. :(

Set to EDIT_Mode>>Make Edits>>Click Save>>File saves and reloads at line 1 automatically.




    @Slot()
    def saveMyFile(self):
        save_file = QFile(STATUS.file())

        result = save_file.open(QFile.WriteOnly)
        if result:
            save_stream = QTextStream(save_file)
            save_stream << self.text()

            save_file.close()
Attachments:
The following user(s) said Thank You: Leon82

Please Log in or Create an account to join the conversation.

More
18 Dec 2019 17:00 #152856 by Leon82

Please Log in or Create an account to join the conversation.

Moderators: KCJLcvette
Time to create page: 0.133 seconds
Powered by Kunena Forum