OmixVisualization / qtjambi

QtJambi is a wrapper for using Qt in Java.
http://www.qtjambi.io
Other
365 stars 43 forks source link

Get sender() #167

Closed glowiak closed 1 year ago

glowiak commented 1 year ago

How to get sender (like the sender() C++ function) in a connected function?

Or any other way to pass arguments when connecting. I have searched for this for a long time and haven't found any solution.

Thanks in advance.

omix commented 1 year ago

sender() is protected function of QObject. If you are in a QObject slot just use it. https://doc.qtjambi.io/6/qtjambi/io/qt/core/QObject.html#sender() What are you missing? Can you give me a code example?

glowiak commented 1 year ago
for (int i = 0; i < 9; i++)
        {
            this.numberB[i] = new QPushButton("" + (i + 1), this.win);
            this.numberB[i].setGeometry(bx, by, bw, bh);

            this.numberB[i].clicked.connect(this, "numb_clicked()");

            if (i == 2 || i == 5)
            {
                bx = bxb;
                by += bh;
            } else
                bx += bw;
        }

[...]

public void numb_clicked()
    {
        System.out.println("clicked button"); // placeholder
    }

The class that invokes it all is not descend of QWidget, but it has a private QMainWindow win variable.

omix commented 1 year ago

I recommend using QButtonGroup instead. It provides signals sending the used button. There is no way to get the current sender without using QObject slots. That's similar to C++.

glowiak commented 1 year ago

@omix How to use the slots? I have never seen these in Java before.

glowiak commented 1 year ago

is there any documentation about them or something like that (for Java)?

glowiak commented 1 year ago

-_-

omix commented 1 year ago

@omix How to use the slots? I have never seen these in Java before.

All methods in a QObject subclass can be used as slots, as you can read in the QtJambi how-to pages. There, you can access the protected member method QObject.signal(). Nevertheless, using QButtonGroup is the better way to do what you try to do.

glowiak commented 1 year ago

Can you provide an example of the use of slots?

omix commented 1 year ago

Can you provide an example of the use of slots?

https://github.com/OmixVisualization/qtjambi/blob/master/www/Characteristics-of-QtJambi.md#qobject-and-qmetaobject

glowiak commented 1 year ago

Thank you