QFileDialog opens twice when checking for improper closing - pyqt5

I am trying to make a PyQt5 application, I am trying to get that filename out of a QFileDialog. However, when trying to actually get the filename after the dialog was accepted, another QFileDialog pops out. I do not know how to solve this, here is the code snippet that is blocking me.
from PyQt5 import QtWidgets as qtw
loadfile = qtw.QFileDialog()
if loadfile.exec_() != 1:
pass
else:
path = loadfile.getOpenFileName(self, 'Open a file', '', 'All Files (*.*)')
As far as I am aware, I am just checking if the user pressed the cancel or the close button here. If not, I call the getOpenFileName in the loadfile object. But this opens the QFileDialog again.

Related

How to start a QDialog in a separate process in PyQt5? [duplicate]

This question already has answers here:
How to create an independent non-modal dialog
(1 answer)
PyQt window closes immediately after opening
(3 answers)
Closed 10 months ago.
I have QDialog designed by PyQt5 designer, I basically need to execute this dialog box from QMainWindow app in a separate process. Although I don't get any errors with below code but the Dialog box never show up. Can anyone tell me what am I doing wrong?
Main Window App on button click:
def alertWindow(self, alertInfo):
p = Process(name="alertwindow", target=alertWindowCustom, args=(alertInfo,))
Dialog Box Class:
class alertWindowCustom(QDialog, Ui_alertwindow):
def __init__(self, alertwindowData):
QDialog.__init__(self)
self.setupUi(self)
self.alertwindowData = alertwindowData
self.run()
self.exec_()
def run(self):
print("brah", self.alertwindowData)
If I just call alertWindowCustom class as a = alertWindowCustom(alertInfo) without a process, the dialog box is created but the MainWindow become unresponsive.
If using QThread is a better option to use over multiprocessing, I would rather use that.
Credit to #musicamante and #ekhumoro.
You don't have to use a thread or a process to create a dialog box that does not block main thread.
To keep your MainWindow responsive while dialog box is open, avoid using exec_() but rather use show(). This will create a non-modeled dialog box and store the dialog box in memory which you will always have to keep a reference of. To do that make a reference of it in your MainWindow as below:
self.alertwindowclass = alertWindowCustom(alertInfo)
Make sure to use self, otherwise as soon as the function is done, dialog box data will be moved to python recycle bin which will destroy you dialog box.
My new full code is below:
class mainWindow(QMainWindow):
# Below function is called to create the dialog box
def alertWindow(self, alertInfo):
self.alertwindowclass = alertWindowCustom(alertInfo)
class alertWindowCustom(QDialog, Ui_alertwindow):
def __init__(self, alertwindowData):
QDialog.__init__(self)
self.setupUi(self)
self.alertwindowData = alertwindowData
self.run()
self.show()
def run(self):
print("brah", self.alertwindowData)
I had to read many forms to gather this information. I wrote an article here if you want to learn more about it. Hope that help people in the future.

Send keys in VBA on appearance of dialog box

Back in this question, I was looking for a way of disabling a particularly troublesome dialog from Outlook that warned me the attachments I was sending might be unsafe. Having concluded that the only security-setting options were unavailable because of administrator privileges, I've instead been trying to look for a workaround to have a script active within Outlook that does something like the following (pseudo VBA):
sub PollForDialog()
if dialogbox = open and dialogbox.name = "This outgoing message may..." then
Sendkeys arrowleft + enter
I've tried exhaustively to search for how to have scripts execute upon a messagebox popping up, and I haven't had any luck. Plenty of stuff about dialog boxes opening when a script is run, but nothing I can find on how to trigger on a dialog opening. Anyone able to help?
Thanks muchly.

Check for "save changes to 'filename' prompt w/ VBA?

First off, this is not about the saveasfilename dialog box.
Is there a way to have a flag set that checks to see if the "want to save your changes to 'filename.xsls'? dialog box appears during a VBA sub ?
Basically I have a macro that copies some data into another file, displays a MsgBox then closes the file and Excel SHOULD prompt the user to confirm that the file is saved. However, I have a legacy program that locks that file sometimes, causing the prompt to not appear, and it looks like the file was saved but actually wasn't. To 'fix' it I have to close all instances of Excel and the program and start over. It doesn't happen often, but you can miss it if you aren't paying attention or someone less experienced in the process doesn't know to check to make SURE they are prompted to save.
What I'd like to know is if there's a way to have some sort of check / flag value, 1/0, true/false, etc to make sure that dialog box appears. If it doesn't then warn the user that they need to restart Excel and the other program. Basically I'm trying to catch an error that should never happen, so this might be unsolvable.
This is the dialog box I'm referring to:
Sometimes it doesn't appear because the file is locked and the VBA sub just continues on.
It would probably better to solve the other problem, but since you don't have posted it, we don't know it.
Anyway, everything you want is described here: https://support.microsoft.com/en-us/kb/213428
You can bascally do this:
Sub Auto_Close()
If ThisWorkbook.Saved = False Then
'ThisWorkbook.Save this would autosave
Application.GetSaveAsFilename 'this displays the save as dialog
End If
End Sub
I ended up doing this as a work around and it's not truly a solution to my question although I suspect there's got to be a way to do it through window classes checking or something. Thank you to everyone who took their time to chime in and offer their brainpower :)
I declared two variables for the file size before and after the sub runs. If the file sizes are equal, then a msgbox appears and says the file may have not been written correctly, or you are overwriting the existing file.
Dim ExportFileByte As Long
Dim ExportBytePostFile As Long
ExportFileByte = FileLen(ExportSourcePath)
' (sub runs here and exports a copy of some data to an .XLS file)
ExportBytePostFile = FileLen(ExportSourcePath)
If ExportFileByte = ExportBytePostFile Then MsgBox ("Error: File may have not have saved, or file is being overwritten without changes (OK)")

workbook.save doesn't work properly

I'm writing a code to save the changes made to excel file using the button click option.
The code for that is very simple:
workbook.Save()
workbook.Close()
App.close() - this is to close the Excel.Application
However, when I click the button I need to wait a bit and this image appears
I would show the image but due to not having 10 reputation it doesn't let me, so I will explain.
The image form has the title File Now Available and below that in the description it said " 'test4.xlsx is not available for editing. Choose Read-Write to open it for editing "
When I click the cross top right of image the excel file opens and then I have to close that. Once done then I can read that file again.
Is there a way I can write a code whereby that image message doesn't appear and it closes the file properly, as I believe this code doesn't fully close it.
Any help is appreciated
Thank You

Eclipse plugin-Control closing of editor

What I am cunrrently doing is while closing the editor in doSave method I check for a condition If it is satisfied I allow super.doSave() be called else I display a dialog box displaying the error message.
Now I want to instead ask user again if he want to save it again and if he says yes he could save the wrong file or say no to edit it but in either case editor should not close.
However currently after the error it closes.
If your editor part implements ISaveablePart2 Eclipse will call the method:
public int promptToSaveOnClose();
to prompt for saving when closing. You can respond with ISaveablePart2.CANCEL to cancel the close, ISaveablePart2.YES to continue the save (by calling doSave), ISaveablePart2.NO to skip the save, or ISaveablePart2.DEFAULT to use the normal behavior.