java - Connect QPushButton Jambi -
i'm pretty new java programming. wrote application listed below, can't connect button function. idea 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 call function sumnum(5,3);
works perfectly, when call button, don't work. i'm using new_action.released.connect("tata()");
i've looked qt, it's giving me
void com.trolltech.qt.qsignalemitter.abstractsignal.connect(object receiver, string method)
object receiver?
i give object receiver, new_action.released.connect(new_action,"tata()");
but, nope, didn't work either.
any idea?
edit: here same application in python:
import sys 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 didn't ask object receiver, , it's run it, in java seems different.
as smar said, needed run in loop, extend qwidget
, initial settings in application()
, setup layout in initui()
, run 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 extended qwidget
, need set layout qhboxlayout main_layout = new qhboxlayout(this);
notice this
connecting button new_action.released.connect(this,"tata()");
object receiver or in word current qwidget
it's same python, python use self
, java use this
.
that's worked me, hope having same problem.
Comments
Post a Comment