I'm using c# wpf. What i want to do is,
A button: When the user clicks the button, rectangular selection is enabled. Then user can draw rectangle on panel.
Mouse down event: When the user clicks the mouse button , it gets the x axis of the current position on panel.
Mouse move event: After clickling , if the user moves the mouse to the left my code1 is going to run. If the user moves the mouse to the right my code2 is going to run.
Code1:
When the user moves the mouse to the right after clicking on panel user is going to draw green rectangle and when user releases the mouse button that rectangle disappears and selects the objects in the rectangle.
Code2:
When the user moves the mouse to the left after clicking on panel user is going to draw red rectangle and when user relases the mouse button the rectangle disappears and selecte the objects in the rectangle and the objects that rectangle intersects.
Here is what i got:
public static System.Drawing.Point mouseLocation2;
public static System.Drawing.Point posmousedoWN;
Button Click Event:
private void btnSelectBox_OnClick(object sender, RoutedEventArgs e)
{
if (btnSelectBox.IsChecked.Value)
{
if (mypartialclass.mouseLocation2.X > mypartialclass.posmousedoWN.X)
{
//code1
}
if (mypartialclass.mouseLocation2.X < mypartialclass.posmousedoWN.X)
{
//code2
}
}
}
Mouse Down event: (This event is in mypartial class)
protected override void OnMouseDown(MouseButtonEventArgs e)
{
posmousedoWN = RenderContextUtility.ConvertPoint(e.GetPosition(this));
}
Mouse Move event: (This event is in mypartial class)
protected override void OnMouseMove(MouseEventArgs e)
{
mouseLocation2 = RenderContextUtility.ConvertPoint(e.GetPosition(this));
}
This code is not working. When i click the button first code1 runs. Then always code2 runs. What am i missing?
Related
All
I am using QToolTip::showText inside a QMousePressEvent handler;
void PmCellsBasedMap::mousePressEvent(QMouseEvent *mouseEvent)
{
if (mouseEvent->button() == Qt::LeftButton)
{
// mouse left button pressed
QString str;
if (getTip(mouseEvent->pos(), str))
{
QToolTip::showText(mouseEvent->globalPos(),str);
}
}
}
However, the QToolTip shows the text too quickly, I need to keep the mouse pressed state for a relative long time, it shows the text later.
My intention: when operator clicks the left mouse button, the QToolTip shows the text all the way and be updated when the next mouse button click action happens.
Can anyone help me on this?
Thanks in advance ...
I am wondering how i can distinguish wheel click event from mouse press event. Because i want to do different handling for these two events in pyside. Currently, every time I click the wheel button, the event is catched by mousepressevent. Can anyone explain ?
Edit: I want to implement this in a subclass of
qglwidget class
From its name, the mousePressEvent is responsible for mouse clicks while the wheelEvent is for scrolling solely. The wheelEvent will not catch the wheel button click. It is how the Qt's API is designed when it comes to mouse events processing.
In order to separate which mouse button is pressed (right, wheel or left), use button property of QMouseEvent.
This is how the code would look like using C++ (I imagine it is easy to translate it to pyside)
void GLWidget::mousePressEvent(QMouseEvent *event) // redefine the mouse event
{
switch( event->button() ) {
case Qt::LeftButton:
// do stuff for left button pressed
break;
case Qt::MiddleButton:
// do stuff for wheel button pressed
break;
// ...
}
}
So, for pyside, you only need to compare the button property of event in mousePressEvent and see whether it is Qt.LeftButton , Qt.RightButton or Qt.MidButton. Hope that helps.
I am using scenebuilder to make the UI. I want to make the color of the buttons change when mouse is pressed or when the button is touched. Can I set the same method for both mouse pressed and screen touched events and also set the same events for multiple buttons? Like there are 3 buttons and I want to change their color in mouse pressed and screen touched events and use only one method for all.
Thank you
Lets say, you have three buttons
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
Create a method saying
private void handleButtonAction(ActionEvent event) {
// Button was clicked, change color
((Button)event.getTarget).setStyle("-fx-background-color:PINK");
}
All buttons have a setOnAction() which is fired on both mouse pressed and screen touched events.
JavaDoc says
The button's action, which is invoked whenever the button is fired.
This may be due to the user clicking on the button with the mouse, or
by a touch event, or by a key press, or if the developer
programmatically invokes the fire() method.
Use it :
button1.setOnAction(this::handleButtonAction);
button2.setOnAction(this::handleButtonAction);
button3.setOnAction(this::handleButtonAction);
If you are using FXML
You can define one action for all buttons :
<Button id="button1" onAction="#handleButtonAction"/>
<Button id="button2" onAction="#handleButtonAction"/>
<Button id="button3" onAction="#handleButtonAction"/>
Inside the controller :
#FXML
private void handleButtonAction(ActionEvent event) {
// Button was clicked, change color
((Button)event.getTarget).setStyle("-fx-background-color:PINK");
}
Can I get key pressed event setOnKeyPress on a popup?
My parent root is opening one popup box and this popup box contains "ok" button.
I want to generate an event on pressing the enter key on keyboard and it should "submit" (trigger "ok" button of) that popup dialog box.
I have used the following code:
NicheSuitePOSController.getApplication().getScene().getRoot().getScene().getRoot().getScene()
.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent ke) {
System.out.println("$$$$$$$$ 131 enter pressed" + ke.getCode());
if (ke.getCode().equals(KeyCode.ENTER)) {
System.out.println("$$$$$$$$ 133 enter pressed");
}
}
});
Try to use
Button.defaultButtonProperty() without attaching any key event handlers to the scene or dialogbox.
I'm trying to create a popup menu, where I can detect the mouse button that was pressed for a given item. I've created a custom QAction already to build my QMenu, but the triggered signal when the menu item is pressed doesn't provide a QMouseEvent for me to query the button pressed.
Also, I'm setting the a status tip for each QAction, which appears in the status bar when I mouse over it, but it stays even after I close the QMenu. Is this normal behavior?
I'm not sure if I understood what do you want; but if you want to show a popup menu on right mouse click, you should at first in header file of your widget (or window class) override function related to mouse event and declare some function that will show your popup menu. So, the header file should contain these declarations:
...
void Popup(const QPoint& pt);
void mousePressEvent(QMouseEvent *event);
...
And in cpp file definitions of functions:
void testQt::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
this ->Popup(event ->pos());
event->accept();
}
}
void testQt::Popup(const QPoint& pt)
{
QPoint global = this ->mapToGlobal(pt);
QMenu* pPopup = new QMenu(this);
QAction* pAction1 = new QAction("Item 1", this);
QAction* pAction2 = new QAction("Item 2", this);
pPopup ->addAction(pAction1);
pPopup ->addAction(pAction2);
QAction* pItem = pPopup ->exec(global);
if(pItem == pAction1)
{
}
else if(pItem == pAction2)
{
}
}
Now, when you press right mouse button, a popup menu will appear at cursor's position.
I hope this helps.
NOTE: If you want to detect which of mouse buttons is pressed when an action is chosen, you should inherit your own class from QMenu. QMenu class contains protected function mousePressEvent(QMouseEvent *event) which should be overriden and you'll be able to detect if left or right mouse button is pressed when an item is chosen in your menu.
I know this is a very old post. But if you want to know what button you have clicked in a popup menu/ context menu.
Lets say you press button Save, that's connected with signals and slots etc. In the slot call a method called sender();. This returns a QObject which you can cast into your QAction* and get the data etc from it .
void MyClass::showMenu()
{
auto action(new QAction*("Blah", ui->my_toolbar));
QObject::connect(action, &QAction::triggered, this, &MyClass::mySlot);
}
void MyClass::mySlot()
{
auto myAction(static_cast<QAction*>(sender()));
myAction->doAwesomeStuff();
}