I'm trying to use the QFileDialog widget in PyQT in order to get the name of a file to save results. I'm using the method QFileDialog.getSaveFileName() and, as is expected, if I select an existing file a warning box appears saying that the file exists and offering the possibility to overwrite it. But the problem is that if I select a directory instead of a regular file the same warning box as if it was a normal file appears. I don't like this behavior and I would like a warning box informing there is a directory and it can't be selected, or maybe the OK button disabled until a regular file is selected. Is possible to do? How can I do this, if it is possible?
Thanks
The static functions will open a native dialog, unless you set DontUseNativeDialog with the options parameter, in which case you will get the built-in Qt file-dialog:
path = QFileDialog.getSaveFileName(options=QFileDialog.DontUseNativeDialog)
The built-in dialog is often faster, and generally has more sensible behaviour than the native dialogs. In particular, if you single-click a directory, the Save button changes to an Open button, so a directory can never be selected.
If you absolutely must have an native dialog, you could try setting the DontConfirmOverwrite option. This will allow you to handle any potential overwriting yourself after the dialog has closed.
Related
I have a WebView node in my JavaFX and the page loaded in it has a <input type=file>. I want to set this input's value to a file on disk. I know this is not possible by injecting javascript into the web view. But I'm wondering if I can get access to JavaFX internals in how these input fields are handled and set the value through there. There appears to be no mention of file input controls' handling in the docs so I'm lost on this.
When I click on the file input. JavaFX gives me a native file selector. So, I'm expecting there's some form of handler that is invoked when clicking on a file input, that asks the user to select a file and then fills the file input with this value. That's what I want to do.
I tried just getting the element and setting it's value, but of course, it didn't work.
webEngine.getDocument().getElementById("FileInput")
.setNodeValue("C:\\attachment.pdf");
This piece of code does nothing. No error, no result either.
So, any ideas?
JavaFX internally uses WebKit for the WebView node, thus it have the same security restrictions. It's not possible to set the value of <input type="file"/> programmatically, neither over JavaScript or Java.
I suggest you to use FileChooser without using the WebView node. What the WebView node shows is a select button, which calls a FileChooser, and a label with the selected file name. This can be easily implemented in Java source code using JavaFX.
I have a graphical interface in jar file. There is a text box in there and a button in the window. I manually enter the text into it and then click the button to get the required output.
Now I want to make this thing automatic using a shell script. I wish to pass my input file using redirection. The shell script should open the jar file copy the text in the text box and click the button automatically so that I get the result instantaneously. Could anyone suggest something?
I don't think there's a out of the box solution for this, what I can think about is :
Create a simple java class which is the launcher of your GUI java app and to perform operations like typing and button click like
Using java Robot class. Refer this post or
Using a GUI testing utility like fest to do those user operations automatically
I added (via drag and drop) a QDockWidget to my GUI that I was editing in Designer...
Now I want to remove it.
Firstly, I selected the visible object and pressed the Delete key.
The Object disappeared.
However, the QDockWidget can be seen to still be present in the Object Inspector,
and when I try to save my .ui, I get the message:
The container extension of the widget MainWindow (QMainWindow)
returned a widget not managed by Designer dockWidget (QDockWidget)
when queried for page #2. Container pages should only be added by
specifying them in XML returned by the domXml() method of the custom
widget.
Clicking on the Widget in the Object Inspector and hitting the Delete key does nothing,
and the right-click context menu does not have the usual 'Remove' option.
How do I get rid of it?!
:|
Specs:
Windows 7 32 bit
PyQt4
I also had this problem - dockable windows in the object inspector which I couldn't see in the main window. Because I couldn't see them, I couldn't find a way to delete them. However, when I saved the file, I ignored the warning messages (the same you had). When I reloaded the saved ui file, all the erroneous dock windows in the object inspector had disappeared.
I've created a new dialog using Qt Creator (version 4.7.0) - one of the templated forms (with an OK and a Cancel button).
I want the user to enter some data on the form and then when they click OK, it saves that information. So I had a look, and saw that when the OK button is clicked, it sends and signal to the dialog's accept slot.
So I right clicked on the dialog in the design view, and selected "Go to slot...". I clicked on the "accepted" option, which dropped an on_Dialog_accepted() method into the dialogs class. However, when I run the program and open the dialog, I get an error in my console saying QMetaObject::connectSlotsByName: No matching signal for on_Dialog_accepted()
So what did I do wrong?
I've found documentation on the connectSlotsByName - but nothing about any obvious pratfalls that an inexperienced Qt-developer can get themselves into.
Right-clicking on the dialog in the design view prior to selecting "Go to slot..." made a connection from the dialog's signals to the dialog's slot, which doesn't work with QMetaObject::connectSlotsByName(), since that method searches for all child objects, but not for the object itself.
What you wanted to do actually is right-clicking on the OK button, then selecting "Go to slot..." from there. It will then create a slot with the name of your button widget, and the connection will be made correctly at run-time.
It makes no sense that QtDesigner lets you select "Go to slot..." from the Dialog. You might want to file a bug to Qt's devs for that.
It is basically just as Fred explained: you did not do anything wrong. This is a QtCreator bug. And a pretty old one for that matter. Unfortunately, even two years later nothing in that regard has changed.
The assignee of the aforementioned bug decided to sort of redirect it to this QtCore bug, which simply asks for QMetaObject::connectSlotsByName() to be changed in a way that it also processes the passed object and not only its children (thereby fixing the QtCreator issue).
I had a look at the source and submitted a trivial patch.
Update: The patch got accepted, meaning this bug is going to be fixed in Qt 5.1. Note: it is not relevant which version of QtCreator your are using but which version of Qt you link your code to.
I have started to play a little with Qt 4. And then I have come across a problem with the Qt Designer.
In the Signal/Slots editor I can only setup the connections that are listed there, and not all the slots are listed.
If I try to add it manualy in the .ui file, the connection would not work.
If I add it in the ui_*.h file it works fine, but then the connection is deleted when I change the design.
Does anyone have any good tips to how I can get around this bug? Or to ask another way:
How can I make the Qt Designer list all the available slots?
By default not all signals/slots are shown. You could try checking the "show signals and slots inheritied from ...." checkbox in the lower left hand corder of the "Configure Connection" dialog that comes up when you try to create a signal.
Beyond that, you can either do what Marcin said and use auto-connections, or manually write connect statements in the constructor of the object that uses the ui.
You might try to use uic's autoconnecting feature.
However you won't be able to see all available slots but if you use the same name in both Designer and code - they should automatically be connected.