Can somebody please explain why do I create in qt public slots but not public signals?
Slots are normal functions, and can be public, private or protected.
Signals are always protected when eventually generated by the 'moc' program.
But note this (from some old Qt 4.7 docs):
Since slots are normal member functions, they follow the normal C++ rules when called directly. However, as slots, they can be invoked by any component, regardless of its access level, via a signal-slot connection. This means that a signal emitted from an instance of an arbitrary class can cause a private slot to be invoked in an instance of an unrelated class.
SLOTS are functions which can be public, private or protected.Functions are called from any where i.e with in the class or outside the class.But SIGNALSare like events and it should be emitted within the class or from the inherited class so SIGNALSare always protected.
Related
A friend asked me this and not sure how to understand. Prolly a simple answer.
He has the following
Public Class TestClass
Public Sub Setup()
MsgBox ("Hello")
End Sub
End Class
Based on that example, what type of member is Setup, in relation to the TestClass class?
I think it it might be an instance member. Because a class is just a collection of instances (methods, properties, etc) within the class.
Correct?
This would be an instance method as opposed to a class method (static methods).
When a field, method, property, event, indexer, constructor, or destructor declaration does not include a static modifier, it declares an instance member.
More information here.
Initially my answer said that a member is the same as a field. According to the MSDN link above this was not entirely correct so I adjusted it. You'll also notice that they use the term static member instead of instance member.
Terminology is a very tricky subject and you'll notice people use many different descriptions for the same subject. This is further amplified when you take other languages in consideration and the terminology there.
It is an instance method, but not because a class is a collection of instances.
It is an instance method because TestClass is not shared (static), and must be instantiated. That is, there must be a instance of TestClass available to use its method Setup(). Conversely, with a Shared class, you do not need an instance of TestClass to use Setup(), it would be a Shared method and not an instance method.
That is academic, however, since VB does not support static classes (Shared Classes), but does support shared methods, the effective difference is that declaring Setup() as Public makes it an instance method, or declaring it as Shared would make it a static method.
how should the member of a QWidget-Class normally designed private or public?
class MyWidget : public QWidget {
public:
MyWidget( QWidget *parent = 0);
QLineEdit *myLine;
}
or
class MyWidget : public QWidget {
public:
MyWidget( QWidget *parent = 0);
private:
QLineEdit *myLine;
}
I prefer the second definition with the private member, but this version is bad to test because I have no access to the member with the findChild( QString ).
Other part is should a QWidget class nested other widgets? Problem is there is no direct access to the nested widgets and this isn't really usefull for automated gui tests where the mouse click should mabyee push a button or something else..
The second choice is generally regarded as the "best programming practice", and if you need to access the private members via code from somewhere else (including your test suite), then you need to implement getters and setters.
Generally, test frameworks should also be using APIs to access objects rather than directly accessing members, for it is that API that you typically want to test within unit tests. If you really can't give up the notion that the test framework should directly access the private members, then you can look into using C++ friend classes. But you didn't hear that from me.
If you need to access something generically (like by a string name), that's exactly what Qt Properties was designed to do for you. So declare your items as a property and list the getters/setters/signals/etc that way too.
Aside: Unfortunately, implementing getters and setters is somewhat boring work, which is why I ended up implementing a QtCreator plugin tool that implements getters, setters and creates signal definitions and emits the signal. If you ever need to actually do something more complex in the future, then simply remove the and replace the auto-getter/setter with your more complex definitions.
As the title stands I get this error within content of this snippet:
class NewTaskDialog : public QDialog
It was working just fine earlier, but error started showing up when I added method:
void MainWindow::saveButtonClicked(NewTaskDialog dialogWindow)
Your syntax for saveButtonClicked creates a copy of the NewTaskDialog that's passed to it. You can't copy QWidgets unless you create a cloning function that explicitly provides the exact functionality you seek. QWidget's constructor is private.
You must pass a pointer
void MainWindow::saveButtonClicked(NewTaskDialog* dialogWindow)
or a reference. Using the pointer is the standard Qt way.
Use a pointer to the QDialog instead. The QDialog class has the copy constructor defined as private to try prevent you from passing a QDialog by value since you should never do that.
What's the use of the private copy constructor in c++
When i read the doc, both seems to be the same. Then what is the difference between these two? Please explain the difference between the two with an example like in which scenario which to be used.
Regards,
The first one is a generic listener to the session. It's called whenever an attribute of any kind is being added or removed to/from a session. It's used when you want to be informed of any session attribute addition/removal.
The second one is a callback interface that can be implemented by a specific class. The callback method is called on an object implementing this interface when this object is being bound/unbound to/from the session. It's used when you want an object to be informed of its own addition/removal to/from the session.
HttpSessionBindingListener:
If an object implements HttpSessionBindingListener, it is notified when it is bound to or unbound from a session. For example,
MyObject implements HttpSessionBindingListener
{
// class definition
}
If I call
session.setAttribute ("Object", MyObject)
methods valueBound and/or valueUnbound (defined in HttpSessionBindingListener, implemented in MyObject are called)
Implementing HttpSessionBindingListener works only for the object that implements it
HttpSessionAttributeListener:
When any class implements HttpSessionAttributeListener interface it is notified when any change in the attribute list of session occurs. For example
MyClass implements HttpSessionAttributeListener
{
// implementations of methods
}
session.setAttribute ("anything", AnyObjectNotOnlyMyClass);
indicates change in the list of attributes.Implementing HttpSessionAttributeListener listens to any attribute added, removed or replaced.
I have a class called CommunicationManager which is responsible for communication with server.
It includes methods login() and onLoginResponse(). In case of user login the method login() has to be called and when the server responds the method onLoginResponse() is executed.
What I want to do is to bind actions with user interface. In the GUI class I created an instance of CommunicationManager called mCommunicationManager. From GUI class the login() method is simply called by the line
mCommunicationManager.login();
What I don't know how to do is binding the method from GUI class to onLoginResponse(). For example if the GUI class includes the method notifyUser() which displays the message received from theserver.
I would really appreciate if anyone could show how to bind methods in order to execute the method from GUI class (ex. GUI.notifyUser()) when the instance of the class mCommunicationManager receives the message from the server and the method CommunicationManager.onLoginResponse() is executed.
Thanks!
There's two patterns here I can see you using. One is the publish/subscribe or observer pattern mentioned by Pete. I think this is probably what you want, but seeing as the question mentions binding a method for later execution, I thought I should mention the Command pattern.
The Command pattern is basically a work-around for the fact that java does not treat methods (functions) as first class objects and it's thus impossible to pass them around. Instead, you create an interface that can be passed around and that encapsulates the necessary information about how to call the original method.
So for your example:
interface Command {
public void execute();
}
and you then pass in an instance of this command when you execute the login() function (untested, I always forget how to get anonymous classes right):
final GUI target = this;
command = new Command() {
#Override
public void execute() {
target.notifyUser();
}
};
mCommunicationManager.login(command);
And in the login() function (manager saves reference to command):
public void login() {
command.execute();
}
edit:
I should probably mention that, while this is the general explanation of how it works, in Java there is already some plumbing for this purpose, namely the ActionListener and related classes (actionPerformed() is basically the execute() in Command). These are mostly intended to be used with the AWT and/or Swing classes though, and thus have features specific to that use case.
The idiom used in Java to achieve callback behaviour is Listeners. Construct an interface with methods for the events you want, have a mechanism for registering listener object with the source of the events. When an event occurs, call the corresponding method on each registered listener. This is a common pattern for AWT and Swing events; for a randomly chosen example see FocusListener and the corresponding FocusEvent object.
Note that all the events in Java AWT and Swing inherit ultimately from EventObject, and the convention is to call the listener SomethingListener and the event SomethingEvent. Although you can get away with naming your code whatever you like, it's easier to maintain code which sticks with the conventions of the platform.
As far as I know Java does not support method binding or delegates like C# does.
You may have to implement this via Interfaces (e.g. like Command listener.).
Maybe this website will be helpful:
http://www.javaworld.com/javaworld/javatips/jw-javatip10.html
You can look at the swt-snippets (look at the listeners)
http://www.eclipse.org/swt/snippets/
or you use the runnable class , by overwritting the run method with your 'callback'-code when you create an instance