How to connect a toolbar button to a script to perform some python action - qgis

I have created a toolbar and a button for the toolbar in pyqgis using the following script:
from PyQt5 import QtCore, QtGui, QtWidgets
toolbar = iface.addToolBar(u'My toolbar')
iface.toolButton = QToolButton()
iface.toolButton.setIcon(QIcon("myIcon.png"))
toolbar.addWidget(iface.toolButton)
In addition, I have a simple python script that perform a simple action, for example:
def my_action()
perform_action
How do I connect the "def my_action()" script to the button above. So that it calls the my_action function when the user click the button?

Related

Drag a file from my GTK app to another app (not the other way around)

I have searched for examples, but all the examples were the opposite direction (my app is getting file drag-and-drop from another application). But this must be possible because I can drag a file from Files (Nautilus) to another app, Text Editor (gedit).
Could you show me a very simple example of a GTK Window with one widget on it, and when I drag from the widget to Text Editor, it passes a text file on the system (such as /home/user/.profile) to the Text Editor so that it will open the text file?
In order to make it so that your application can receive files, you need to use uri. In the function you bind to drag-data-received, you can use data.get_uris() to get a list of the files that were dropped. Make sure that you call drag_dest_add_uri_targets(), so that the widget can receive URIs.
This code example has one button that drags a file, and another button that can receive it. You can also drag the file and drop it into any file-receiving app, such as gedit (Text Editor) or VSCode.
import gi
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
window = Gtk.Window()
window.connect("delete-event", Gtk.main_quit)
box = Gtk.HBox()
window.add(box)
# Called when the Drag button is dragged on
def on_drag_data_get(widget, drag_context, data, info, time):
data.set_uris(["file:///home/user/test.html"])
# Called when the Drop button is dropped on
def on_drag_data_received(widget, drag_context, x, y, data, info, time):
print("Received uris: %s" % data.get_uris())
# The button from which the file is dragged
drag_button = Gtk.Button(label="Drag")
drag_button.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, [], Gdk.DragAction.LINK)
drag_button.drag_source_add_uri_targets() # This makes sure that the buttons are using URIs, not text
drag_button.connect("drag-data-get", on_drag_data_get)
box.add(drag_button)
# The button into which the file can be dropped
drop_button = Gtk.Button(label="Drop")
drop_button.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.LINK)
drop_button.drag_dest_add_uri_targets() # This makes sure that the buttons are using URIs, not text
drop_button.connect("drag-data-received", on_drag_data_received)
box.add(drop_button)
window.show_all()
Gtk.main()
Mr Kruin's answer was for Python's GTK. The language I actually wanted was C#. Using his code as a hint, I modified the default GtkApplication project like so, and it worked on Linux.
private MainWindow(Builder builder) : base(builder.GetRawOwnedObject("MainWindow"))
{
builder.Autoconnect(this);
DeleteEvent += Window_DeleteEvent;
_button1.DragDataGet += (o, args) =>
{
args.SelectionData.SetUris(new string[]{fullFilePath});
};
Gtk.Drag.SourceSet(_button1, Gdk.ModifierType.Button1Mask,
new TargetEntry[0],
Gdk.DragAction.Link);
Gtk.Drag.SourceAddUriTargets(_button1);
}
On Windows, however, it did not work. I have tried both string fullFilePath = "file:///c:/windows/win.ini" and string fullFilePath = "c:\\windows\\win.ini" and none of them seemed to work.

Unable to render something with pvpython (paraview)

I'm new on paraview, and I want to make a script that render a vtk file, using only pvpython. Therefore, I wrote this script
from paraview.simple import *
from paraview.vtk.vtkFiltersSources import vtkSphereSource
paraview.simple._DisableFirstRenderCameraReset()
renderView1 = GetActiveViewOrCreate('RenderView')
renderView1.ViewSize = [1080, 860]
reader = OpenDataFile([nameFile...])
Interact()
Render()
But when I launch the script with pvpython using the command .\pvpython.exe .\myscript.py, the visualisation shows an empty window, without my mesh
Does someone have an idea, of why my mesh isn't rendered in the view ?
In order to add your reader output in the view, you have to call Show() before Interact().
Show() will add the current active object in the view. To explicitly add your reader output in the view, you may use Show(reader)
Calling Render() is also unneeded just after Interact()
edit Here is a minimal script to load a file and display it centered in a default 3d view:
from paraview.simple import *
reader = OpenDataFile("/path/to/your/file")
Show()
Interact()

Is it possible to use CMD + CLICK Element A + CLICK Element B

I cannot use the Katalon recorder to click on the Element with CMD Key. Is it possible to click on two different items while the CMD key is pressed?
I tried to do that with sendKeys, controlKeyDown, keyDown Commands and then call Click Command on first element and then call Click Command on second element. But nothing happens.
Also, if we can simulate with CLICK Element A then CMD + CLICK Element B it can be ok for me.
In Katalon Studio (not the recorder) switch to script mode:
Now you can write scripts in Groovy!
Next, try using Actions class, like this:
WebUI.openBrowser("url of the page you are testing")
WebDriver driver = DriverFactory.getWebDriver()
WebElement targetA = driver.findElement(By.cssSelector("element-a-selector"))
WebElement targetB = driver.findElement(By.cssSelector("element-b-selector"))
targetA.click()
Actions actions = new Actions(driver)
actions.keyDown(Keys.CONTROL).click(targetB).keyUp(Keys.CONTROL).perform()
Note that this is your second scenario, first clicking element A (you will need to add the CSS selector or any other way of identifying it) and then holding CTRL and clicking element B.
The first one (press CTRL, click element a, click element b, release CTRL) is similar, just change the last line to:
actions.keyDown(Keys.CONTROL).click(targetA).click(targetB).keyUp(Keys.CONTROL).perform()
and remove targetA.click().
You will need to import the following Selenium helper classes:
import org.openqa.selenium.By
import org.openqa.selenium.Keys
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement as WebElement
import org.openqa.selenium.interactions.Actions

Visual Studio Code: Is it possible to make a decorations hoverMessage clickable

Hi I am developing an extension for VSCode. I am decorating the text editor and hovering some items. Is it possible to make clickable items at hoverMessage and modify the range according to it.
The extension is at:
https://marketplace.visualstudio.com/items?itemName=serayuzgur.crates
You can see the hoverMessage from the GIF
Yes, using markdown you can then create a command link that will execute a command when a user clicks on it:
import * as vscode from 'vscode';
const myContent = new vscode.MarkdownString('[link](command:myCommand?arg1)');
// Command Uris are disabled by default for security reasons.
// If you set this flag, make sure your content is not constructed
// using untrusted/unsanitized text.
myContent.isTrusted = true;
const myHover = new Hover(myContent);
This command can perform whatever action you want

Right click in selenium webdriver problems

I've found this code here: https://stackoverflow.com/a/22376627/4165083
Actions oAction = new Actions(driver);
oAction.moveToElement(Webelement);
oAction.contextClick(Webelement).build().perform(); /* this will perform right click */
WebElement elementOpen = driver.findElement(By.linkText("Open")); /*This will select menu after right click */
elementOpen.click();
But I'm having problems with driver: "Cannot resolve symbol driver". I can't import anything. What should I do to get it working in my tests in Scala?
I believe you are referencing to wrong element. You have to reference to the element you are trying to right click on;
WebElement elementToRightClickOn = driver.findElement(By.id("something"));
Actions oAction = new Actions(driver);
oAction.moveToElement(elementToRightClickOn);
oAction.contextClick(elementToRightClickOn).build().perform(); /* this will perform right click */
WebElement elementOpen = driver.findElement(By.linkText("Open")); /*This will select menu after right click */
elementOpen.click();
"Cannot resolve symbol driver" - Check have you created the instance of the WebDriver and is accessible.
For Actions class - import org.openqa.selenium.interactions.Actions;
Provide complete code to assist.