delegates

How to create delegate for QTreeWidget?

邮差的信 提交于 2019-12-17 23:08:18
问题 Here is what I'm trying to do (all parents and children must have a close button on the right, in the future, only the hovered item will be able to show the **close ** button): My delegate code: class CloseButton : public QItemDelegate { Q_OBJECT public: CloseButton( QObject* parent = 0 ) : QItemDelegate( parent ) {}; QWidget* createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const { if ( index.column() == 1 ) { QToolButton* button = new QToolButton

What would I lose by abandoning the standard EventHandler pattern in .NET?

こ雲淡風輕ζ 提交于 2019-12-17 22:48:06
问题 There's a standard pattern for events in .NET - they use a delegate type that takes a plain object called sender and then the actual "payload" in a second parameter, which should be derived from EventArgs . The rationale for the second parameter being derived from EventArgs seems pretty clear (see the .NET Framework Standard Library Annotated Reference). It is intended to ensure binary compatibility between event sinks and sources as the software evolves. For every event, even if it only has

How to create a delegate from a MethodInfo when method signature cannot be known beforehand?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 21:49:20
问题 I need a method that takes a MethodInfo instance representing a non-generic static method with arbitrary signature and returns a delegate bound to that method that could later be invoked using Delegate.DynamicInvoke method. My first naïve try looked like this: using System; using System.Reflection; class Program { static void Main() { var method = CreateDelegate(typeof (Console).GetMethod("WriteLine", new[] {typeof (string)})); method.DynamicInvoke("Hello world"); } static Delegate

Proper naming convention for a .NET Delegate type?

纵然是瞬间 提交于 2019-12-17 21:27:24
问题 By convention classes are often named like nouns, methods like verbs and interfaces like adjectives. What is the common naming convention for a delegate? Or what's a good way to differentiate its name when delegates are listed among types and other things? My immediate assumption is to name a delegate more likely an adjective because a single method interface can often be replaced with a delegate. Some thoughts: delegate object ValueExtracting(object container); delegate object ValueExtractor

Storing a method as a member variable of a class

混江龙づ霸主 提交于 2019-12-17 21:01:50
问题 I have this as one of my members of the class 'KeyEvent': private delegate void eventmethod(); And the constructor: public KeyEvent(eventmethod D) { D(); } What I want to do is instead of calling D() there, I want to store that method (D) as a member variable of KeyEvent, so something like: stored_method = D(); And then later in another method of KeyEvent, do something like: stored_method(); How can I do this? 回答1: You pretty much have the code already. Just create a member field of the right

Multicast delegate weird behavior in C#?

非 Y 不嫁゛ 提交于 2019-12-17 19:56:15
问题 I have this simple event : public class ClassA { public event Func<string, int> Ev; public int Do(string l) { return Ev(l); } } And 2 Methods : static int Display(string k) { return k.Length; } static int Display_2(string k) { return k.Length*10; } Im registering this event : ClassA a = new ClassA(); a.Ev += Display; a.Ev += Display_2; Now , I'm executing : Console.WriteLine(a.Do("aaa")); the output : What ??? he has in invocation list 2 methods ! it did run them , but why does it shows only

Java equivalent of C# Delegates (queues methods of various classes to be executed)

孤街醉人 提交于 2019-12-17 19:27:43
问题 TLDR: Is there a Java equivalent of C#'s delegates that will allow me to queue up methods of various classes and add them to the queue dynamically? Language constructs instead of the code for it. Context: I have used Unity 3D before and I love things like the Update method of scripts. Just declaring the method adds it to the list of methods executed each frame. I want to create something like that in my LWJGL game. For this, I would want to use delegates (or something equivalent to it). Is

Objective-C equivalent to Java's anonymous classes in class methods

China☆狼群 提交于 2019-12-17 18:53:14
问题 I want to set the delegate of an object inside a class method in Objective-C. Pseudo-code: + (ClassWithDelegate*) myStaticMethod { if (myObject == nil) { myObject = [[ClassWithDelegate alloc] init]; // myObject.delegate = ? } return myObject; } In Java I would simply create an anonymous class that implemented the delegate protocol. How can I do something similar in Objective-C? Basically I would like to avoid creating a separate class (and files) to implement a simple delegate protocol. 回答1:

what is Delegate in iPhone?

限于喜欢 提交于 2019-12-17 18:36:10
问题 what is the exact meaning of delegate in iphone?how it is implemented in UIViewController? 回答1: A delegate is an object that usually reacts to some event in another object and/or can affect how another object behaves. The objects work together for the greater good of completing a task. Typically a delegate object will be shared by many other objects that have a more specific task to carry out. The delegate itself will be more abstract and should be very reusable for different tasks. The

Delegate for an Action< ref T1, T2>

自闭症网瘾萝莉.ら 提交于 2019-12-17 18:22:10
问题 I'm trying to create a delegate of a static method which takes a ref argument. Please don't ask why I'm doing such a cockamamie thing. It's all part of learning how .Net, C#, and reflection work and how to optimize it. My code is: public struct DataRow { private double t; static public void Cram_T(ref DataRow dr, double a_t) { dr.t = a_t; } } '''' Type myType = typeof(DataRow); MethodInfo my_Cram_T_Method = myType.GetMethod("Cram_T"); var myCram_T_Delegate = Delegate.CreateDelegate(typeof