I wrote a Qt plugin for QGIS, and inside it I have a QPushButton browse_btn to browse for an output folder.
I use the QFileDialog.getExistingDirectory function and it all works fine. However if I close the plugin and open it again, then click the pushbutton to add a path, the opened window for choosing the path will re-open after I choose a path or close it. If I close the plugin a open it again the 'choose path' window will reopen 3 time and so on...
The similar problem is still open here and in comments here and probably solved here, but since there is no code provided I cannot apply it to solve my problem.
The exact code is following:
self.dlg.browse_btn.clicked.connect(self.set_output)
def set_output(self):
folder_path = str(QFileDialog.getExistingDirectory(self.dlg, u"Pick a folder"))
if not folder_path:
return None
I think this happens because each time I open-close the plugin the separate connection is created with self.dlg.browse_btn.clicked.connect(self.set_output), and it only disconnects after I relaunch QGIS
Try this - Add a boolean flag isConnected to your class (or dlg) and init this flag to false.
Then change your connection line to this
if (!self.isConnected):
self.dlg.browse_btn.clicked.connect(self.set_output)
self.isConnected = true
Related
I've been working with RSelenium all day and still hitting road blocks here and there. My current issue is using the code WebElemReports$clickElement() which clicks a link and a new window opens. I tried to adjust Firefox settings in "about:config" so that it will not open a new window. It doesn't open a window in normal use, but using RSelenium, it opens a new window still. I also looked at this approach but couldn't follow the logic of how it worked:
How to clickElement() and open the link in the same tab
My next thought process was to use the switchToWindow() function along with getWindowHandles(). The code I wrote is as follows:
remDr$closeWindow()
windHand <- remDr$getWindowHandles()
remDr$switchToWindow(windHand)
My thinking is that I will close the current window so that there will only be one handle to reference and pass that handle to the switchToWindow function. I can't find much switchToWindow documentation for R. I receive the following error with using the code above:
Error: Summary: UnknownError
Detail: An unknown server-side error occurred while processing the command.
class: org.openqa.selenium.WebDriverException
Any help on this would be much appreciated--I tried to research this as much as possible so this won't get marked as a duplicate question like my last post. Many Thanks.
Actually you can't close main window, you can switch to child window as below :-
# get main window and store to switch back
currWindow <- remDr$getCurrentWindowHandle()
#gel all windows
windows <- remDr$getWindowHandles()
#loop through switching child window
for (window in windows[[1]]) {
if (window != currWindow[[1]])
remDr$switchToWindow(window)
}
#now do your stuff with child window
#now close your child window after doing all stuff
remDr$closeWindow()
#now switch back to main window for further stuff
remDr$switchToWindow(currWindow[[1]])
I have a script that:
creates a new folder
scans an InDesign document for images
formats the images and copies them to the new folder
When the script is done doing all of this I want it to bring to focus the new folder directory (in Windows).
As of now I am displaying the folder-path in an alert window, but I would rather it open the directory (if it isn't already) so the user can see the new files.
I wish I could just call one of these:
myNewFolder.bringToFront() : works only on program focus, i.e. -- BridgeTalk.bringToFront("photoshop")
myNewFolder.open() : seems to apply only to file I/O operations
myNewFolder.show() : seems to apply only to the Window object
...but none of these work.
EDIT: new ActiveXObject("Scripting.FileSystemObject") does not work either...
You have to use the execute method.
myFolder.execute();
In Qt,
QFileDialog *dlg = new QFileDialog();
QDir dir = dlg->getExistingDirectory(this, tr("Choose folder"), qgetenv("HOME"));
opens a folder choose dialog. Once I select a folder (press choose button) the folder is not closing automatically. So I tried:
if(dlg->close() == true) delete(dlg);
When I debug the dlg->close() returns true and the code delete(dlg) is hit. Still the Folder chooser dialog box is not closing.
I am using Ubuntu 11.10 64 bit OS. Using Qt libraries from the repository.
My ultimate aim is just to show a folder chooser dialog and once the folder is chosen the dialog should close. After that processing should continue. How to do this?
Thanks in advance.
Even if QFileDialog::getExistingDirectory is static and doesn't need a QFileDialog object to work, it should close the dialog window when a directory is finally chosen.
By default that function tries to open a native file dialog window, which seems to cause some problems on some platforms.
You should try forcing a non-native dialog by adding the option DontUseNativeDialog:
QString dir = QFileDialog::getExistingDirectory(
this,
tr("Choose folder"),
QDesktopServices::storageLocation(QDesktopServices::HomeLocation),
QFileDialog::ShowDirsOnly | QFileDialog::DontUseNativeDialog);
And remove the two other lines (with new QFileDialog and if(dlg->close()) ...).
getExistingDirectory(...) is a static function.
To add to cmannett85's answer:
You should not make an instance of QDialog. If you do, it's up to you to hide it. Modify your code to read
const QString home = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
const QDir dir = QFileDialog:getExistingDirectory(this, tr("Choose folder"), home);
This code should be relatively portable. qgetenv("HOME") is Unix-specific. You should not introduce gratuituous platform-specific code in Qt-based projects -- it sort of defeats the purpose of using Qt in the first place.
I wish to simulate a right click on a file. This is done by opening a Windows Explorer window and then right clicking on it.
The main issue is finding the location of the file in Windows Explorer. I am currently using Autoit v3.3.8.1.
My code 's first line:
RunWait (EXPLORER.EXE /n,/e,/select,<filepath>)
The next step is the problem. Finding the coordinates of the file.
After that, right clicking at that coordinates (it seems to me at this time) is not a problem....
Some background:
OS: Windows 7 64-bit
Software Languages: C#, Autoit (for scripting)
The Autoit script is called by a code similar to that below:
Process p = new Process();
p.StartInfo.FileName = "AutoItScript.exe";
p.StartInfo.UseShellExecute = false;
p.Start();
The code is compiled into a console class file which is run at startup. The autoit script runs as the explorer window opens up.
It seems as though you are taking the wrong approach to the problem, so I'll answer what you are asking and what you should be asking.
First up though, that line of code is not valid, and is not what you want either. You want to automate the explorer window, and RunWait waits for the program to finish. Furthermore you want those items to be strings, that code would never work.
Finding the item in explorer
The explorer window is just a listview, and so you can use normal listview messages to find the coordinates of an item. This is done most simply by AutoIt's GUIListView library:
#include<GUIListView.au3>
Local $filepath = "D:\test.txt"
Local $iPid = Run("explorer.exe /n,/e,/select," & $filepath)
ProcessWait($iPid)
Sleep(1000)
Local $hList = ControlGetHandle("[CLASS:CabinetWClass]", "", "[CLASS:SysListView32; INSTANCE:1]")
Local $aClient = WinGetPos($hList)
Local $aPos = _GUICtrlListView_GetItemPosition($hList, _GUICtrlListView_GetSelectedIndices($hList))
MouseClick("Right", $aClient[0] + $aPos[0] + 4, $aClient[1] + $aPos[1] + 4)
As has already been mentioned, sending the menu key is definitely a better way than having to move the mouse.
Executing a subitem directly
This is how it should be done. Ideally you should never need an explorer window open at all, and everything can be automated in the background. This should always be what you aim to achieve, as AutoIt is more than capable in most cases. It all depends on what item you want to click. If it is one of the first few items for opening the file in various programs, then it is as simple as either:
Using ShellExecute, setting the verb parameter to whatever it is you want to do.
Checking the registry to find the exact command line used by the program. For this you will need to look under HKCR\.ext where ext is the file extension, the default value will be the name of another key in HKCR which has the actions and icon associated with the filetype. This is pretty well documented online, so google it.
If the action is not one of the program actions (so is built into explorer) then it is a little more complex. Usually the best way will be to look at task manager when you start the program and see what it runs. Other things can be found online, for example (un)zipping. Actions like copy, delete, rename, create shortcut, send to... They can all be done directly from AutoIt with the various File* functions.
With more information, it would be possible to give you more specific help.
First, you might want to look at the Microsoft Active Accessibility SDK. In particular look at this interface...
http://msdn.microsoft.com/en-us/library/accessibility.iaccessible.aspx
You can use this to walk the items in the control and find the one with the file name you are looking for and its screen location.
From there, maybe try something like this for simulating the right click.
How can I use automation to right-click with a mouse in Windows 7?
Once you have done the right click, use accessibility again to find the right option on the context menu.
Maybe there's an easier way, you should be able to cobble something together like this if you don't find one. Good luck!
Suppose I have a file named test.txt on D drive. It needs to right click for opening Context Menu. To do this, the following code should work:
Local $filepath = "D:\test.txt"
Local $iPid = Run("explorer.exe /n,/e,/select," & $filepath)
ProcessWait($iPid)
Sleep(1000)
Send('+{F10}')
I am using an AutoIt script to automate my application. Following is the command which I am running:
FileOpenDialog ("File Upload", "C:\Documents and Settings\abhishek.kumar\Desktop\Quadrillion work", "Images (*.jpg;*.bmp)", "","WESTF12433.jpg","" )
Send("{ENTER}")
The first command works as it opens up the file open dialog with WESTF12433.jpg file as selected. Now I want to click on open button. How can I do it?
Send("{ENTER}") is not working.
Send is not a good method as it requires the window to be focused, which you can't guarantee. From what you have posted, I would say the best method would be this:
ControlClick("File Upload", "", "Button1")
Edit in response to comments:
Your problem: The fileOpenDialog is blocking execution. You need to think of it as though AutoIt Reads a line, Runs it, then reads the next.
In this case: AutoIt Reads line 1. It creates a FileOpenDialog and WAITS for you to close it. Once it as been closed it reads the next line, and runs that.
Try the following: Create two au3 files, and put the first line in 1.au3 and the second in 2.au3. Run them in that order and see what happens. Send will struggle as the dialog doesn't have focus, but the ControlClick versions should work.
You're not using FileOpenDialog() properly. Its purpose is to interact with the user. If you don't need that, there's no need for it in the first place.
All it does is return the name(s) of the selected files, which you defined already. Can't you just assign the file path to the variable from the start? As per Documentation - Function Reference - FileOpenDialog() :
Success: Returns the full path of the file(s) chosen. Results for
multiple selections are "Directory|file1|file2|..."
This will never work, because AutoIt is not multi-threaded.
Once you open the dialog, it pauses script execution until the user clicks ok, so a Send() function on the next line won't do anything until after.
What you can do is make another script, compile it, and run it just before you open the dialog.
Run("clickOpen.exe")
FileOpenDialog ("File Upload", "C:\Documents and Settings\abhishek.kumar\Desktop\Quadrillion work", "Images (*.jpg;*.bmp)", "","WESTF12433.jpg","" )
This is what would get executed:
WinWaitActive("File Upload")
Send("{ENTER}")
Simple as that! Hope it helps.