Overloading event on QMainWindow blocks all other events - qmainwindow

I want to handle the QEvent::WindowDeactivate and for this I overload this function on my QMainWindow derieved class.
class MainWindow : public QMainWindow
....
bool event(QEvent * e);
....
bool MainWindow::event(QEvent *e)
{
if(e->type() == QEvent::WindowDeactivate){
//do smth
}
}
And after that, this event function starts to intercept all other events in my program. All buttons and widgets in this window stop reacting on mouse clicks and keyboard events are also intercepted. Is there a way to fix this?

I solved the problem: had to add
return QWidget::event(e);
at the end of event implementation. And after that - everything works fine.

Related

How to update the content of a label in one usercontrol from another usercontrol?

I have 2 usercontrols. OperationPanel and OutPutPanel.These are added into the MainWindow.xaml. How can I update the label in OutPanel when an "Execute" button in OperationPanel clicked.
There are various ways to solve this, depending on whether you are using MVVM, how proficient you are with OO design (e.g. the "mediator pattern"), and so on.
For a simple solution (off the top of my head), add a static method to your MainWindow.xaml.cs that you call from your "execute" button click. This method would raise an event (again static) that your second user control subscribes to. The code might look something like this:-
public static event EventHandler Executed;
public static void RaiseExecutedEvent()
{
if (Executed != null)
{
Executed(this, EventArgs.Empty);
}
}
You could also move this code into its own static class for re-usability.

the name of event for add or remove the children of inkCanvas using wpf

I have an Inkcanvas in my project (myPaint)
What is the name of event for add or remove the children (UiElement) from InkCanvas. for example I want handle this event : myInkCanvas.Children.remove(myRectangle) or this example :
myInkCanvas.Children.Add(myRectangle)
There isn't an event you can listen to that is fired when elements are added to or removed from the Children collection. There is a virtual protected method that is called, which you could leverage, called OnVisualChildrenChanged.
This isn't directly tied to the Children collection, as elements can add/remove visuals separate from that. But for InkCanvas, it would probably be safe.
So you'd use something like:
public class MyInkCanvas : InkCanvas {
protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved) {
// TODO: Raise event or do something
base.OnVisualChildrenChanged(visualAdded, visualRemoved);
}
}
What exactly do you need this for? Can you use the StrokeCollected event instead?

WPF usercontrol showdialog problem

There is a MainWindow, a usercontrol which is located in my MainWindow and a OtherForm which I am going to show from usercontrol. I sent OtherForm as parameter from MainWindow to usercontrol. And in usercontrol I am calling OtherForm.showdialog. When I show it the second time, I am getting "Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed" problem.
Code
In MainWindow class
void Example()
{
usercontrol.Load(new Otherform{ variable= 1 });
}
In Usercontrol class
private Window _form;
public void Load(window form)
{
_form=form;
}
void ExampleInUSerControl
{
_form.VerifyAccess();
_form.Activate();
_form.ShowActivated = true;
_form.ShowDialog();
}
The error message in this case is pretty accurate: once a Window is closed, it's closed for good. Since ShowDialog() always closes the window, you need to create a new instance of the window every time you call ShowDialog().
One fairly simple way to accomplish this in your example is to have the Load event take an argument of type Func<Window>:
In the MainWindow:
private Window MakeWindow()
{
return new MyWindow();
}
private void Example()
{
usercontrol.Load(MakeWindow);
}
In the user control:
public void Load(Func<T> makeWindow)
{
_form = makeWindow();
...
}
Note, by the way, that there should be no reason to call Activate or set ShowActivated - ShowDialog will do all that. And I don't know why you'd call VerifyAccess either.

WPF - Detect when UserControl is not visible anymore and prompt user

So, I have a class, which goes as follows:
public class EditorUserControl : UserControl
{
public EditorUserControl()
: base()
{
this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(
EditorUserControl_IsVisibleChanged);
}
void EditorUserControl_IsVisibleChanged(
object sender,
DependencyPropertyChangedEventArgs e)
{
if (IsEditing && !((bool)e.NewValue))
{
PressedButton pressedButton = PromptUser(new Buttons[] {
"Save changes to the object you just edited?",
Buttons.Yes,
Buttons.No,
Buttons.Cancel });
if(pressedButton == Buttons.Cancel)
{
CANCELTHETHING();
}
}
}
}
In words - this class is a base for all entity editing controls and when it goes invisible (e.g. window is closed, tab changed etc.) I need to check if the user has made changes and prompt the user whether to save/discard/cancel. The save/discard are easy. The problem is with the third option (and it must be there) - I cannot figure out a way how could I cancel the source event that caused the visibility to change (as there is no way to get to that actual event). Is there a better way to do this functionality (that would not require signing up for all of the possible sources of events)?
I don't think it is possible to cancel the source (event) as you want to.
Consider this line of code - EditorUserControl.Visibility = Visisibility.Hidden;
This will also cause the IsVisibleChanged event to fire, but there is no way to cancel a single line of code.
Your only option is to move the logic inside the IsVisibleChanged event handler to a method that will be called as appropriate by the application. For instance if you close the window then in the window_closing event handler you call the method and if the result is Button.Cancel then you cancel the closing event. If you change tabs then you handle a SelectionChanged event and again call the method and if you need to cancel then you set the selected tab index back to the previous value etc.

OnClick event on compound UserControls

I want to create a UserControl with several controls inside. These controls should behave simmilar to the radio buttons, i. e., the status of one of them affects the status of the rest. To do that I would like to be able to handle the OnClick event on each of the controls from the parent.
One solution could be to call a method of the parent, to perform the global UserControl change, from the child controls' OnClick method. Something like:
class Child : UserControl
{
...
protected override void OnClick(EventArgs z_args)
{
// do own stuff
((PartentType)Parent).ChangeStatus(this);
}
...
}
This is a solution, but I wonder if there is a more standard an elegant way to solve this issue. Thanks!
No, this is very bad, a control should never depend on having a specific parent. Do it the same way any Windows Forms control does it: if something interesting happens that a parent might be interested in then raise an event:
public event EventHandler StatusChanged;
public int Status {
get { ... }
}
protected override void OnClick(EventArgs z_args) {
// do own stuff
//...
var handler = StatusChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
Consider putting the event raising code in a private setter for the Status property.

Resources