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.
as what Smar said, it needed to run in a loop, so i extend from
QWidget
, initial the settings inApplication()
, setup the layout ininitUI()
and run it inmain()
.since i extended from
QWidget
, i only need to set the layout like soQHBoxLayout main_layout = new QHBoxLayout(this);
Notice the thisas well as connecting the button
new_action.released.connect(this,"Tata()");
Object receiver is this or in another word currentQWidget
it's exactly same as Python, just python use
self
and Java usethis
.that's what worked for me, hope that help however having the same problem.