Giter Site home page Giter Site logo

Comments (5)

therecipe avatar therecipe commented on May 12, 2024

Hey

Unfortunately, it's currently not possible to add your own signals or slots to existing classes.
The binding currently just allows you to use/override the existing signals or virtual functions.

The problem is, that if you would want to add new signals/slots to your own subclass, you would need to manually create the *.cpp, *.h and the *.go file and also run the http://doc.qt.io/qt-5/moc.html tool.

This could be automated (probably integrated into qtdeploy), with a function that parses the *.go files from your project and creates the missing files on the fly. But I haven't really looked into that yet.

However, if you just want to be able to call Go functions from Qml or Qml functions from Go, there is a dirty workaround :)

I added a new example, which demonstrates how you can do it. https://github.com/therecipe/qt/tree/master/internal/examples/quick/bridge (this example currently doesn't work on android because of the use of QQuickWidget, but it works with qt 5.6)

Calling Go functions from Qml, works by using a QWidget as a proxy to receive Json encoded messages from Qml.

Calling Qml functions from Go, works by setting a "onPropertyChanged" method to receive Json encoded messages from Go. (the QMetaObject::invokeMethod, is currently not usable)

If you got any questions or run into problems, feel free to ask.

from qt.

longlongh4 avatar longlongh4 commented on May 12, 2024

Hi
I have tried both, the second way seems confused when project grows larger. So I think the first way is better (even though we must make our hand dirty with C++, but who cares ^_^ ), I have sent a pull request to add an example about how to mix QObject signals&slots with golang .

How about implement a go generator to auto implement bridge c++ class to do this, like code below:

//go:generate goQt blahblah...
type SignalHandler interface {
    ReceiveValueFromQml(value string) `qSignal`
    SendValueToQml(value string) `qSlot`
}

from qt.

therecipe avatar therecipe commented on May 12, 2024

Hey

Thanks for the pull request :)
I started working on a way to auto implement new signals and slots.
The qtdeploy tool will now execute a new tool called qtmoc, which parses the local files and creates the needed files on the fly.

The syntax will look like this:

//go:generate qtmoc
type SignalHandler struct {
    core.QObject

    _ func(value string) `signal:sendToQml`
    _ func(value string) `slot:callbackFromQml`
}

You will also be able to subclass these classes, so something like this will work also:

//go:generate qtmoc
type TopSignalHandler struct {
    SignalHandler

    _ func(value string) `signal:sendToQml2`
    _ func(value string) `slot:callbackFromQml2`
}

from qt.

therecipe avatar therecipe commented on May 12, 2024

Okay, I pushed the changes.

You can now execute qtmoc to generate the needed files.
I also updated the example quick/bridge and quick/bridge2.

from qt.

longlongh4 avatar longlongh4 commented on May 12, 2024

That's great, thank you!

from qt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.