di icon indicating copy to clipboard operation
di copied to clipboard

boost di QObject support.

Open mostafajalal20 opened this issue 4 years ago • 5 comments

Expected Behavior

we try to use boost di with QObject from another .so inside our main program using di::extension::runtime_injector.

Actual Behavior

we get error that not mentioned in boost di documentations.

Steps to Reproduce the Problem

https://github.com/mostafajalal20/Di_sample.git this is our example to showing the problem.

mostafajalal20 avatar Apr 24 '21 10:04 mostafajalal20

Any changes about this issue?

decamel avatar Nov 11 '22 13:11 decamel

Im having the same issues.

shalabala avatar Jul 16 '23 19:07 shalabala

I understand that this problem is due to having default value of QObject in constructor.

FooClass(int a, QObject parent = nullptr)

The class shown above causes problems in using boost. the below code shows correct form of above class.

FooClass(int a, QObject parent)

mostafajalal20 avatar Jul 17 '23 07:07 mostafajalal20

Thank you, this was one of the problems, but i did try to mend it as you described. But I also was able to realize another problem, which ended up being the solution for me.

QWidgets have their copy-, and move constructor deleted, meaning they cannot be copied or returned form functions either. So for example, the following code would throw an error:

class MyWidget : public QWidget {
//...
};
MyWidget createWidget(){
    MyWidget widget;
    return  widget
    //alternatively return std::move(widget);
}

int main(){
    //...
    MyWidget widget = createWidget();
    widget.show();
    //...
}

And i was trying to instantiate MyWidget the following way:

int main(){
    //...
    auto injector = di::make_injector()
    injector.create<MyWidget>().show();
}  

But this uses the move constructor, which is deleted in QWidget, so it kept throwing an error. But once i change it to some sort of pointer type (simplest example would be injector.create<MyWidget*>()->show()), it works perfectly.

This might be somewhat of a noob error, but i thought i post it maybe someone in the future will face the same.

shalabala avatar Jul 17 '23 17:07 shalabala

Yes I used all arguments of constructors as std::shared_ptr because the boost::di work without any problem with raw or smart pointers.

mostafajalal20 avatar Jul 17 '23 21:07 mostafajalal20