Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QColor member results in error. #26

Closed
Pfeil opened this issue Feb 15, 2019 · 7 comments
Closed

QColor member results in error. #26

Pfeil opened this issue Feb 15, 2019 · 7 comments

Comments

@Pfeil
Copy link

Pfeil commented Feb 15, 2019

I tried to figure out the problem, but somehow I don't get it.
This works: (Minimal example)

#[derive(QObject, Default)]
struct DataStructure {
    base: qt_base_class!(trait QObject),
    name: qt_property!(QString;),
}

But this does not:

#[derive(QObject, Default)]
struct DataStructure {
    base: qt_base_class!(trait QObject),
    color: qt_property!(QColor;),
}

I do not see that/where QString implements QGadget or QObject, so why does it work with QString, but not with QColor? What would I need to change?

Error Message:

> cargo run
   Compiling qmldemo v0.1.0 (/home/user/Git/qmldemo)
error[E0277]: the trait bound `qmetaobject::QColor: qmetaobject::QGadget` is not satisfied
 --> src/piechart.rs:8:19
  |
8 | #[derive(Default, QObject)]
  |                   ^^^^^^^ the trait `qmetaobject::QGadget` is not implemented for `qmetaobject::QColor`
  |
  = note: required because of the requirements on the impl of `qmetaobject::QMetaType` for `qmetaobject::QColor`
  = note: required because of the requirements on the impl of `qmetaobject::PropertyType` for `qmetaobject::QColor`
  = note: required by `qmetaobject::PropertyType::pass_to_qt`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `qmldemo`.
@ogoffart
Copy link
Member

QColor need to iimplement QMetaType.
The error message is a bit confusing. QGadget do not need to be re-implemented. It could be improved if #[do_not_recommend] was stabilized: rust-lang/rfcs#2397

@ogoffart
Copy link
Member

I added the missing QMetaType declaration in commit ec11955

@Pfeil
Copy link
Author

Pfeil commented Feb 17, 2019

I see, thanks for that. Now, I have an additional question to that: I have a similar problem with a custom type, I'm not sure whether to open another issue for that. I am currently trying to replicate chapter 4 of this c++ example where they create a class PieSlice which inherits from QQuickPaintedItem. They use it as a Q_PROPERTY/qt_property in the class PieChart. Since the base member (QObjectCPPWrapper) does/must not implement Clone, I can not derive or simply implement QMetaType for PieSlice (as far as I know).

I assume I can somehow work around that and render everything using the PieChart class itself (Slices could be QGadgets or so), but that would not really be what they to in the tutorial.

So the question is: Is there an intended way to solve this problem for custom types with a base class in qmetaobject? I am not sure whether I have overseen something in the examples, but I think this is a relatively common case. I am not sure whether I could implement it more similar to QColor or so. Maybe it would work somehow, but a more standard/intended/tutorial-like way would be nice.

@ogoffart
Copy link
Member

Well, the trait to use a property is, in fact, PropertyType. It is implemented for QMetaType and RefCell<T: QObject>
So what you must do then is to have the property of type RefCell<PieChart>
See struct ObjectWithObject in tests.rs
Yes, I know, this is not well documented! :-(

@Pfeil
Copy link
Author

Pfeil commented Feb 18, 2019

That makes sense, thanks. Well, now it is at least documented here, and I'll look into documenting things as soon I've gotten further. I have to say that most time I need for this demo is to get the cpp! macros working. I did what you explained, and it works, but for QQuickPaintedItems I need to use the setParentItem(...) function, but I can't get it to work. I do not think that this is related to this issue (it compiles without this cpp macro), but it may be.

fn set_pieslice(&mut self, mut pieslice: RefCell<PieSlice>) {
        {
            let slice_obj = pieslice.get_mut().get_cpp_object();
            let self_obj = self.get_cpp_object();
            // TODO pieslice.set_parent_item(self);
            unsafe {
                cpp!( unsafe [self_obj as "QQuickPaintedItem*", slice_obj as "QQuickPaintedItem*"] {
                    slice_obj->setParentItem(self_obj);
                });
            }
        }
        self.pieslice = pieslice;
    }

I did not manage to implement this in a trait in an easy way, so I decided to just do it in my example crate. The error message says nothing useful.

   Compiling qmldemo v0.1.0 (/home/verpfeilt/Git/qmldemo)
error: proc-macro derive panicked
  --> src/piechart.rs:37:17
   |
37 | /                 cpp!( unsafe [self_obj as "QQuickPaintedItem*", slice_obj as "QQuickPaintedItem*"] {
38 | |                     slice_obj->setParentItem(self_obj);
39 | |                 });
   | |___________________^
   |
   = help: message: 
           -- rust-cpp fatal error --
           
           The OUT_DIR environment variable was not set.
           NOTE: rustc must be run by Cargo.: NotPresent
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

@ogoffart
Copy link
Member

I have to say that most time I need for this demo is to get the cpp! macros working.

I'm interrested in what could have been done to make this easier. Was it the lack of documentation? Or bad error message?
You can open issues on the rust_cpp bug tracker.

@ogoffart
Copy link
Member

The OUT_DIR environment variable was not set.
NOTE: rustc must be run by Cargo.: NotPresent

That's a really strange error... normaly, cargo is setting this variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants