3

I'm pretty new to Java programming. I wrote the application listed below, but I can't connect the button to my function. Any idea what I'm doing wrong?

package com.teat;
import com.trolltech.qt.gui.*;

public class Application 
{

    public static void main(String[] args) 
    {
        QApplication.initialize(args);
        QWidget mainWidget = new QWidget();
        mainWidget.setWindowTitle("Simple Example");
        QHBoxLayout main_layout = new QHBoxLayout();
        mainWidget.setLayout(main_layout);

        QPushButton new_action = new QPushButton("Working");
        new_action.released.connect("Tata()");
        main_layout.addWidget(new_action);

        SumNum(5,3);
        mainWidget.show();
        QApplication.execStatic();
        QApplication.shutdown();
    }

    private static int SumNum(int num1,int num2)
    {
        int sum = num1 + num2;
        System.out.println(sum);
        return sum;

    }
    private static void Tata(){
        System.out.println("Yes, it's Working");
    }

}

When I call the function like SumNum(5,3); it works perfectly, but when I call it from button, It don't work. I'm using new_action.released.connect("Tata()");

I've looked into Qt, it's giving me
void com.trolltech.qt.QSignalEmitter.AbstractSignal.connect(Object receiver, String method) but what is Object receiver?

I even give itself as object receiver, new_action.released.connect(new_action,"Tata()"); but, nope, it didn't work either.

Any idea?

Edit: here the same application in python:

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.setWindowTitle('Simple Example')

        main_layout = QtGui.QHBoxLayout()
        self.setLayout(main_layout)

        new_action = QtGui.QPushButton("Working")
        new_action.released.connect(self.Tata)
        main_layout.addWidget(new_action)

        self.show()

    def SumNum(self, num1, num2):
        print num1+num2
        return num1+num2

    def Tata(self):
        print "Yes, it's Working"


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

in Python it didn't ask for Object receiver, and it's just run it, but in java it seems like completely different.

Bear
  • 550
  • 9
  • 25
  • 1
    Qt is meant to be used inside the event loop, so that may be the problem. By event loop, I mean the loop that is started by `execStatic()` here. Easy way to do this is to subclass QWidget and use that as `mainWidget`. So there will still be `mainWidget.show()` and `QApplication.execStatic()`, but all other application code is inside the application. – Smar Apr 19 '16 at 08:24
  • @Smar i edited my question, added python example, as you said, i have to make separate class and execute it from main class, just like how it is in python example, right? also what about object receiver? what is that for? – Bear Apr 19 '16 at 18:38
  • I see you managed to solve your problem :) Is there any open questions still? If not, please just accept your answer after you’re able to :) – Smar Apr 20 '16 at 07:02
  • @Smar yeah, thanks for the help, i'm sure i will have more question later on. :D (also i have to wait 21 hour then i can accept my answer) – Bear Apr 20 '16 at 07:18

1 Answers1

1

as what Smar said, it needed to run in a loop, so i extend from QWidget, initial the settings in Application(), setup the layout in initUI() and run it in main().

import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;

public class Application extends QWidget {

    public Application() {

        setWindowTitle("Simple Example");
        setMinimumHeight(100);
        setMinimumWidth(100);
        setGeometry(250, 250, 350, 100);

        initUI();

        show();
    }

    private void initUI() {

        QHBoxLayout main_layout = new QHBoxLayout(this);

        QPushButton new_action = new QPushButton("Working");
        new_action.released.connect(this,"Tata()");
        main_layout.addWidget(new_action);

    }

    private void Tata(){
        System.out.println("Yes, it's Working");
    }

    public static void main(String[] args) {
        QApplication.initialize(args);
        new Application();
        QApplication.execStatic();
        QApplication.shutdown();
    }
}

since i extended from QWidget, i only need to set the layout like so QHBoxLayout main_layout = new QHBoxLayout(this); Notice the this
as well as connecting the button new_action.released.connect(this,"Tata()"); Object receiver is this or in another word current QWidget

it's exactly same as Python, just python use self and Java use this.

that's what worked for me, hope that help however having the same problem.

Bear
  • 550
  • 9
  • 25