How to trigger slot from another header file - qt5

SettingDialog *s = new SettingDialog();
QObject::connect(SettingsLabelBtn,SettingsLabelBtn->clicked(),s,s->changeSettings());
I have mainwindow.h and settingdialog.h. I want to when click SettingLabelBtn(it is QPushButton object) this event trigger to slots from settingdialog.h.
mainwindow class derived from QMainWindow also it has Q_Object macro
settingdialog class derived from QDialog aslo it has Q_Object macro
My error is no matching member function for call to 'connect'

Your connect syntax is wrong -- you're calling the member functions rather than passing their respective addresses. Try...
QObject::connect(SettingsLabelBtn, &QPushButton::clicked, s, &SettingDialog::changeSettings);
Also see the documentation regarding the new signal/slot syntax.

Related

Unityscript : Instantinate TouchController?

I am currently creating a joystick and therefore I want to replace the Get Axis method.
Here is my code
function UpdateSmoothedMovementDirection () {
var h = TouchController.GetAxisRaw("Horizontal");
But Unity gives me this error :
An instance of type 'TouchController' is required to access non static member 'GetAxisRaw'.
How can I call an instance ?
Add public variable of type TouchController to your script. Then add game object with TouchController script to your scene. If your script is derived from MonoBehaviour, in Hierarchy tab in Unity Editor drag and drop game object with TouchController onto your variable.
If your script is not a MonoBehaviour script, than you'll have to set this variable manually from some other MonoBehaviour script.

groovy method scope when using a method reference

I have a groovy class that looks up a method reference and then invokes it. The method being invoked is a private method. When the actual class is an instance of the child class, it throws an error that it cannot find the private method, even though it is the public method in the parent that actually calls it.
In this case, I could obviously just call pMethod2() directly and that works, but I'm trying to understand why this doesn't work as written and if there's a way to correct it so it works.
class Parent {
def pMethod1() {
def m = this.&pMethod2
m() // this call fails if the calling class is of type Child
}
private def pMethod2() {}
public static void main(String[] args) {
new Child().pMethod1();
}
}
class Child extends Parent {}
It is a bit confusing, especially if you're used to C / C++. What you get when using the ".&" operator in Groovy is not an address, but an instance of MethodClosure.
The MethodClosure object contains an owner and a delegate object, which is used when resolving the method to call. In your example, the owner and delegate object will be "this", which is an instance of Child. The method to call is simply stored as a string.
So, the assignment
m = this.&pMethod2
is just a shorthand way of writing
m = new MethodClosure(this, "pMethod2")
When you invoke the m() closure, it will try to resolve (at runtime) the method by looking for methods named "pMethod2" in the owner and the delegate objects respectively. Since the owner and delegate is an instance of Child, it will not find private methods located in Parent.
To make your example work you must make sure that the method is visible to the owner and/or delegate of the closure.
This can be done several ways, for instance by changing the access modifier of pMethod2 to protected, or by creating the closure with an instance of Parent; something like this:
m = new Parent().&pMethod2
Note that is is irrelevant that you created the MethodClosure instance in a method where pMethod2 is actually visible. It is also irrelevant that you invoke the closure in a method where it is visible. The method is not visible to the owner or delegate of the MethodClosure, which is what is being used when resolving the method.

List model inherited from QAbstractListModel, list item properties won't update from QML

I'm having a difficult time explaining my problem, so I'm just going to make it as simple and hope it does the job. I'm using Qt5 with QtQuick 2.0.
I've created a MyListModel class that inherits from QAbstractListModel, and holds items of type MyListItem. I use it in a QML ListView as a model: myListModel, and the delegate displays a quantity property from MyListItem, in a lovely TextInput box. Everything works fine.
However, when I change the quantity value from the delegate, the items in the model aren't updated. I know they're not updated, because my setQuantity(long desired_quantity) function, a member of MyListItem, does not run. Long story short, I can't figure out how to actually call the setQuantity function from within the delegate. I can do it manually by adding Q_PROPERTY(long quantity READ quantity WRITE setQuantity) to MyListItem, and then using setContextProperty() to expose a MyListItem myTemp object to QML, and then calling myTemp.quantity = 10. But clearly, if the delegate can't write to the quantity property (it can only read from it), it's not doing the job.
Can somebody point me in the right direction? I feel like I've tried everything the Qt designers could have possibly expected, and I get nothing. And I can't find any documentation that clearly addresses my issue.
The TextInput box will not update your c++ model automatically, you have to do this by yourself. You can do this by adding a slot or Q_INVOKABLE method to you model:
//add a slot to you model
public slots:
setDataInModel(const int index, const QVariant &value);
//or add Q_INVOKABLE method:
public:
Q_INVOKABLE setData(const int index, const QVariant &value);
You have to implement one of these methods so that it changes the appropriate data row in your model. Do not forget to call the dataChanged method inside of the method after update. You then have to call these methods from QML delegate manually when the TextInput is updated:
onAccepted: {
model.setDataInModel(index, text)
}
Here index is a property that is defined in each delegate and text is the text from your TextInput.

Listbox.SelectedItems.Add not causing SelectionChanged event

I have a custom user control extending the Listbox class. Inside of it I am overriding OnSelectionChanged to add/remove Adorners to any selected/unselected items. This all works when I select an item using the mouse, but when I programmatically add items to the listbox using
myListBox.SelectedItems.Add(newItem) // newItem is already a member of myListBox.Items
It does not execute the OnSelectionChanged code.
Update: Unless I'm crazy (which is always possible) it seems there is a difference in behaviour between calling this from the parent object
myListBox.SelectedItems.Add(newItem)
and this method inside my extended listbox class
Public Sub AddSelectedItem(newItem as Object)
Me.SelectedItems.Add(newItem)
End Sub
For some reason the second option is triggering the event while the first one isn't.
you need to add this line of code first
myListBox.Items.Add(newItem)
The solution here is that calling SelectedItems.Add() from inside an extension of ListBox
public class MyListBox : ListBox
{
public void AddSelectedItems(object newSelectedItem)
{
// works
this.SelectedItems.Add(newSelectedItem);
}
}
will trigger the OnSelectionChanged event.
Calling it like this from the window will not trigger the event
private sub SomeWindowMethod()
{
// does not work
this.MyListBoxInstance.SelectedItems.Add(newSelectedItem);
}

RTTI ?? create multiple object at runtime wxwidgets?

hi
sorry for my stupid question
what are the right way to create multiple control object from a list of array of label of object ...?
thank
The function wxCreateDynamicObject can be used to construct a new object of a given type, by supplying a string name. If you have a pointer to the wxClassInfo object instead, then you can simply call wxClassInfo::CreateObject.
You must include the IMPLEMENT_DYNAMIC_CLASS macro in every class you want to be able to dynamically create objects. IMPLEMENT_DYNAMIC_CLASS is a macro that not only initialises the static wxClassInfo member, but defines a global function capable of creating a dynamic object of the class in question.
Example
In a header file:
class wxFrame : public wxWindow
{
DECLARE_DYNAMIC_CLASS(wxFrame)
private:
wxString m_title;
public:
...
};
In a C++ file:
IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
wxFrame::wxFrame()
{
...
}