è¯ã人ã
ã®ãããã§ããã®äžé£ã®èšäºã®èè
ã¯æåŸ
ãåãããã以éã®ãã¹ãŠã®èšäºã¯åœŒã«ãã£ãŠå
¬éãããã®ã§ããã®äœåãç§ã«å²ãåœãŠãªãã§ãã ããã ç§ã¯åœŒã®åéã§ãã ;ïŒ
éšåçªå·2
ååPyQt4ãã©ã®ããã«æ©èœããããç¥ããæåã®ã¢ããªã±ãŒã·ã§ã³ãäœæããŸããïŒ
ããã¹ãã©ãã«ã¯ãæžåŒãèšå®ãããŠããŠãåªããŠããŸããããã£ãšè€éã§ããããããŠãŒã¶ãŒã®æäœãå¿
èŠãšãããã®ãèšè¿°ããå¿
èŠããããšæããŸãã Qt4ã«ã¯ããããæäŸããå€ãã®ã¯ã©ã¹ãå«ãŸããŠããŸãã ãŸãããŠãŒã¶ãŒã®ã¢ã¯ã·ã§ã³ã«å¯Ÿããããã°ã©ã ã®åå¿ãèšå®ããããšãã§ããŸãããããŠããšããã§ãéåžžã«ãšã¬ã¬ã³ããªæ¹æ³ã§ã ããããèšèããè¡åã«ç§»ããç°¡åãªããã°ã©ã ãæžããŸãããã ååãããå®è¡ããæ¹æ³ãèšãããã®ã§ãããã«ãã ããããšã¯ãããŸããã
1ïŒïŒ/ Usr / bin / python
PyQt4 import Qtãã2
3ã€ã³ããŒãã·ã¹ãã
4 __name__ == "__main__"ã®å ŽåïŒ
5 app = Qt.QApplicationïŒsys.argvïŒ
6ãã¿ã³= Qt.QPushButtonïŒãçµäºãïŒ
7ãã¿ã³ã®ãµã€ãºå€æŽïŒ200ã70ïŒ
8 button.showïŒïŒ
9 app.exec_ïŒïŒ
ååãšåæ§ã«ãã»ãŒåãã¢ã¯ã·ã§ã³ãå®è¡ããŸãããQApplicationã¯ã©ã¹ããã¢ããªãªããžã§ã¯ããäœæãããŠã£ãžã§ãããäœæããæ§æãã衚瀺ããã¢ããªã±ãŒã·ã§ã³ãèµ·åããŸããã åŸ
ã£ãŠãã»ããã¢ããããŸããïŒ ããããååã¯ããã§ã¯ãããŸããã§ããïŒ ããã ãã èŠããŠãããªããååããã¹ãŠã®QtãŠã£ãžã§ãããé衚瀺ã§äœæãããããããæ§æããŠãã衚瀺ã§ããããã«ãããšèšããŸããã ä»ã§ã¯äŸ¿å©ã§ãã 6è¡ç®ã§ã¯ãããã¹ããExitããå«ããã¿ã³ãäœæãã8è¡ç®ã§è¡šç€ºããŸããã ãããã7è¡ç®ã§èª¿æŽãè¡ãããµã€ãºãå€æŽããŸããã Qt4ãŠã£ãžã§ããã«ã¯ããŸããŸãªèšå®ããããæè»ãªã€ã³ã¿ãŒãã§ã€ã¹ãäœæã§ããŸãã ãã®å Žåããã®ããããã£-ãµã€ãºã䜿çšããŠã200x70ãã¯ã»ã«ã«å€æŽããŸããã
çµæã«æºè¶³ãããããããŸãããã...ãã¿ã³ã«ã¯ãçµäºããšè¡šç€ºãããŸãããã¯ãªãã¯ããŠãäœãèµ·ãããŸããã ããã¯ããã¿ã³ã®ã¯ãªãã¯å¿çãèšå®ããªãã£ãããã§ãã 1è¡è¿œå ããŠããããå®è¡ããŠã¿ãŸãããã
1ïŒïŒ/ Usr / bin / python
PyQt4 import Qtãã2
3ã€ã³ããŒãã·ã¹ãã
4 __name__ == "__main__"ã®å ŽåïŒ
5 app = Qt.QApplicationïŒsys.argvïŒ
6ãã¿ã³= Qt.QPushButtonïŒãçµäºãïŒ
7ãã¿ã³ã®ãµã€ãºå€æŽïŒ200ã70ïŒ
8 Qt.QObject.connectïŒãã¿ã³ãQt.SIGNALïŒãã¯ãªãã¯ïŒïŒãïŒãsys.exitïŒ
9 button.showïŒïŒ
10 app.exec_ïŒïŒ
èµ·åãããšãã¢ããªã±ãŒã·ã§ã³ããŸã£ããå€æŽãããŠããªãããšãããããŸãã ãã ããä»ãããã¿ã³ãã¯ãªãã¯ããŠã¿ãŠãã ãã...ã¢ããªã±ãŒã·ã§ã³ãçµäºããŸããïŒ ãŠãŒã¶ãŒã®ã¢ã¯ã·ã§ã³ã«å¯Ÿããåå¿ã次ã«ç€ºããŸããããã¯8è¡ç®ã§æäŸãããŠããŸããå°ãå€ãã£ãŠããŸãããå®éã«ã¯ãã¹ãŠãã·ã³ãã«ã§ãã éçé¢æ°connectïŒïŒãåŒã³åºããã·ã°ãã«ãéä¿¡ãããªããžã§ã¯ããã·ã°ãã«èªäœããã®ã·ã°ãã«ãå°çãããšãã«å®è¡ãããé¢æ°ããã©ã¡ãŒã¿ãŒãšããŠç€ºããŸãã ã·ã°ãã«ãšã¯äœã§ããïŒ ããã§ãå°ãäœè«ãããŠããSignals and Slotsãã®æŠå¿µã«ã€ããŠè©±ãå¿
èŠããããŸãã
QtãŠã£ãžã§ããã¯ãç¹å®ã®ã€ãã³ãã®ã·ã°ãã«ãçæããŸãïŒã¡ãªã¿ã«ãUNIXã·ã°ãã«ãšã¯ãŸã£ããé¢ä¿ãããŸããïŒã ããšãã°ãQPushButtonã¯ããã¿ã³ãã¯ãªãã¯ããããšclickedïŒïŒã·ã°ãã«ãçæããŸãã Qtã䜿çšãããšããŠã£ãžã§ããããåä¿¡ããã·ã°ãã«ã«å¯Ÿããåå¿ãèšå®ããããããã¹ããããå®è¡ã§ããŸãããããã¯ãã·ã°ãã«ãå°çãããšãã«åŒã³åºãå¿
èŠãããé¢æ°ã§ãã
ãä¿¡å·ãšã¹ããããã¯éåžžã«éèŠãªåœ¹å²ãæãããäºãã«äœãç¥ããªãããŸããŸãªãªããžã§ã¯ããæ¥ç¶ã§ããŸãã ã·ã°ãã«ãã¹ãããã«æ¥ç¶ããã«ã¯ãQObjectã¯ã©ã¹ã®connectïŒïŒé¢æ°ã䜿çšãããŸãïŒå®éãã»ãšãã©ãã¹ãŠã®Qtãªããžã§ã¯ãã¯äœããã®æ¹æ³ã§QObjectã®åå«ã§ããããããã®é¢æ°ã¡ãœãããå«ãŸããŠããŸãïŒã åŸæ¥ã®Qt for C ++ã§ã¯ãconnectïŒïŒãåŒã³åºãããã®æ§æã¯æ¬¡ã®ãšããã§ãã
æ¥ç¶ïŒéä¿¡è
ãã·ã°ãã«ïŒä¿¡å·ïŒïŒïŒãåä¿¡è
ãã¹ãããïŒã¹ãããïŒïŒïŒïŒ
ããã¯ç§ãã¡ã䜿çšãããã®ãšã¯å°ãç°ãªããŸãããæåã®ãã®ãæåã§ãã ãéä¿¡è
ããšã¯ãä¿¡å·ãçæãããªããžã§ã¯ãã§ããããã®äžã§ã€ãã³ããçºçããŸããã ãSignalïŒïŒãã¯ãçæãããä¿¡å·ã®ååã§ãã ãåä¿¡è
ããããã¯ä¿¡å·ã«å¿çããå¿
èŠããããªããžã§ã¯ãã§ãä¿¡å·ãå°çãããšãã«å®è¡ããããã¹ãããïŒïŒãæ©èœãåããŠããŸãã C ++ã§ã¯ãSIGNALïŒïŒããã³SLOTïŒïŒãã¯ãã¯æ§æã®äžéšã§ãããPythonã¯ãããã«ç°ãªãåŒã³åºãæ§æã䜿çšããŸãã åŒã³åºããããšãã«é¢æ°ãå±ãããªããžã§ã¯ããPythonã§åžžã«ç€ºãå¿
èŠããããããéçºè
ã¯PyQt4ã®çããã©ãŒã ãäœæããŸããïŒäœ¿çšããããšããå§ãããŸãããC ++ãã©ãŒã ã䜿çšã§ããŸãïŒã
æ¥ç¶ïŒéä¿¡è
ãSIGNALïŒ "signalïŒïŒ"ïŒãreceiver_slotïŒ
åä¿¡æ©ã¹ãããã¯ãä¿¡å·ã«å¿çããæ©èœã§ãã sys.exitïŒïŒãªã©ãã©ã®ã¯ã©ã¹ã«ãå±ããªãå ŽåããããŸãã ããã¯ãããæè»ãªæ§æã§ãã æ¬è³ªã¯åãã§ããä¿¡å·ãå°çãããšãã¹ãããæ©èœãå®è¡ãããŸãã ãã®æ§æã§ã¯ãã¹ãããã次ã®ããã«èšè¿°ããå¿
èŠããããŸãïŒé¢æ°ãç¹å®ã®ã¯ã©ã¹ã®ã¡ã³ããŒã§ããå ŽåïŒïŒã¬ã·ãŒããŒæ¬åŒ§ãšãã©ã¡ãŒã¿ãŒã®ãªãé¢æ°ã ãã ãããŸã ããã€ãã®å¯èœæ§ããããŸãã
ä¿¡å·ã¯å¥ã®ä¿¡å·ã«æ¥ç¶ã§ããŸãïŒC ++ãšåæ§ã®èšé²åœ¢åŒã䜿çšïŒããã®å Žåã1ã€ã®ä¿¡å·ãçæãããšã2çªç®ã®ä¿¡å·ãçæãããŸãã
æ¥ç¶ïŒéä¿¡è
ãSIGNALïŒ "signal1ïŒïŒ"ïŒãåä¿¡è
ãSIGNALïŒ "signal2ïŒïŒ"ïŒïŒ
1ã€ã®ä¿¡å·ã¯è€æ°ã®ã¹ãããã«æ¥ç¶ã§ãããããã¯ã©ã³ãã ãªé åºã§å®è¡ãããè€æ°ã®ä¿¡å·ã¯1ã€ã®ã¹ãããã«æ¥ç¶ãããŸãã æåŸã«ãæ¥ç¶ããã£ã³ã»ã«ã§ããŸãïŒPyQt4ã®çã圢åŒã䜿çšã§ããŸãïŒã
åæïŒéä¿¡è
ãSIGNALïŒãã·ã°ãã«ïŒïŒãïŒãåä¿¡è
ãSLOTïŒãã¹ãããïŒïŒãïŒïŒ
ãã ããå¯èœæ§ã¯ããã«éå®ãããŸããã ã·ã°ãã«ã¯å€æ°ã®ç¹å®ã®å€ãéã¶ããšãã§ããã¹ãããã¯ããããåãåãããšãã§ããŸããäŸãã°ïŒ
Qt.QObject.connectïŒã¹ã©ã€ããŒãQt.SIGNALïŒ "valueChangedïŒintïŒ"ïŒãspinbox.setValueïŒ
ãŸãã¯ãC ++ã®ãã©ãŒã ã§ãåãã§ãïŒPythonã®ãã©ãŒã ã«æ
£ããã®ã«äœ¿çšããŸãïŒã
Qt.QObject.connectïŒã¹ã©ã€ããŒãQt.SIGNALïŒ "valueChangedïŒintïŒ"ïŒãã¹ãã³ããã¯ã¹ãQt.SLOTïŒ "setValueïŒintïŒ"ïŒïŒ
ã芧ã®ãšãããå€æ°ã¯æ¬åŒ§å
ã®ã¿ã€ãã®ãªã¹ãã§èª¬æãããŠããŸãã ã·ã°ãã«ãçæããããšãå¿
èŠãªãã¹ãŠã®å€æ°ãäžããããã¹ãããã«ã¯ãã©ã¡ãŒã¿ãŒããããã·ã°ãã«ãéã¶å€ãååŸããŸãã ãã©ã¡ãŒã¿ãŒã¯åãé åºã§æå®ããåãã¿ã€ãã«ããå¿
èŠããããŸãã ãã ããä¿¡å·ã¯ã¹ããããããå€ãã®ãã©ã¡ãŒã¿ãæã€ããšãã§ãããäœåãªããã©ã¡ãŒã¿ã¯åã«ç¡èŠãããŸãã ãã©ã¡ãŒã¿ã®ã¿ã€ããç°ãªãå Žåãããã°ã©ã ã®å®è¡äžã«å¯Ÿå¿ããèŠåãçºè¡ãããŸãã
ããŠãä¿¡å·ãšã¹ãããã®ç¥èã䜿çšããŠãèå³æ·±ããã®ãæžããŸãããã ããã¯ãã¹ã©ã€ããŒãŸãã¯ãã€ã€ã«ã«ãŠã³ã¿ãŒã䜿çšããŠ0ã100ã®æ°åãå
¥åã§ããã¢ããªã±ãŒã·ã§ã³ã§ãã ããã«ããŠã£ãžã§ããã®1ã€ã§æ°ãå€æŽããããšã察å¿ããå€æŽãä»ã®ãŠã£ãžã§ããã§çºçããŸãããã®ããããã£ã¯ãã·ã°ãã«ãšã¹ããããä»ããŠå®è£
ãããŸãã
1ïŒïŒ/ Usr / bin / python
PyQt4 import Qtãã2
3ã€ã³ããŒãã·ã¹ãã
4 __name__ == "__main__"ã®å ŽåïŒ
5 app = Qt.QApplicationïŒsys.argvïŒ
6
7ãŠã£ã³ããŠ= Qt.QWidgetïŒïŒ
8 window.setWindowTitleïŒãæ°åãå
¥åããŠãã ãããïŒ
9
10ã¬ã€ã¢ãŠã= Qt.QHBoxLayoutïŒïŒ
11 window.setLayoutïŒã¬ã€ã¢ãŠãïŒ
12
13ã¹ã©ã€ããŒ= Qt.QSliderïŒQt.Qt. HorizoâântalïŒ
14 slide.setRangeïŒ0ã100ïŒ
15 layout.addWidgetïŒã¹ã©ã€ããŒïŒ
16
17ã¹ãã³ããã¯ã¹= Qt.QSpinBoxïŒïŒ
18 spinbox.setRangeïŒ0ã100ïŒ
19 layout.addWidgetïŒã¹ãã³ããã¯ã¹ïŒ
20
21 Qt.QObject.connectïŒã¹ã©ã€ããŒãQt.SIGNALïŒ "valueChangedïŒintïŒ"ïŒãspinbox.setValueïŒ
22 Qt.QObject.connectïŒspinboxãQt.SIGNALïŒ "valueChangedïŒintïŒ"ïŒãslider.setValueïŒ
23
24 slide.setValueïŒ50ïŒ
25
26 window.showïŒïŒ
27
28 app.exec_ïŒïŒ
ç§ãã¡ã®ã¢ããªã±ãŒã·ã§ã³ã¯ããã«å€ããªããŸããã ãããç解ããŸãããã
è¡7ã8ã§ã¯ããŠã£ãžã§ãããŠã£ã³ããŠãäœæããã³æ§æãããŸãã ãŠã£ãžã§ããã¯ãŠã£ã³ããŠã«ã§ããããšã¯æ¢ã«è¿°ã¹ãŸããããããã§ã¯æœè±¡QWidgetã¯ã©ã¹ã䜿çšããŸããïŒãšããã§ããã¹ãŠã®QtãŠã£ãžã§ãããç¶æ¿ããŠããŸãïŒã è¡8ã¯ããŠã£ã³ããŠã®ã¿ã€ãã«ãèšå®ããŸãã
10ã11è¡ç®ã«ãæ°ãããã®ãç»å ŽããŸããã ããã¯ã¬ã€ã¢ãŠããããŒãžã£ãŒã¯ã©ã¹ã§ãã Qt4ã«ã¯ããŠã£ãžã§ããã¬ã€ã¢ãŠããæ
åœãã3ã€ã®äž»èŠãªã¯ã©ã¹ããããŸãã
QHBoxLayout-ãŠã£ãžã§ãããæ°Žå¹³æ¹åã«ã°ã«ãŒãåããŸãã
QVBoxLayout-ãŠã£ãžã§ãããåçŽã«ã°ã«ãŒãåããŸãã
QGridLayout-ã°ãªããã»ã«ããšã«ãŠã£ãžã§ãããã°ã«ãŒãåããŸãã
ãããã®ãããŒãžã£ãŒã䜿çšãããšããŠã£ãžã§ããã®ããããçš®é¡ã®èŠèŠçãªçµã¿åãããäœæã§ããŸãããŠã£ãžã§ãããã¬ã€ã¢ãŠãã«é
眮ã§ããã ãã§ãªããä»ã®ã¬ã€ã¢ãŠãã«ãé
眮ã§ããŸãã
ãã®å Žåãæ°Žå¹³ã¬ã€ã¢ãŠããããŒãžã£ãŒã䜿çšããããããŠã£ã³ããŠã®ã¡ã€ã³ãããŒãžã£ãŒïŒwindow.setLayoutïŒïŒïŒãšããŠèšå®ããŸããã åŸã§ããŠã£ãžã§ãããäœæããåŸããããŒãžã£ãŒå
ã«é
眮ã§ããŸãããããŒãžã£ãŒã¯ããããé
åžããæé©ãªãµã€ãºãæäŸããŸãã è¡13ã14ã§ã¯ãæ°Žå¹³ã¹ã©ã€ããŒãäœæããã³æ§æããïŒèš±å®¹å€ã®ç¯å²ãèšå®ãããŸãïŒãè¡15ã§ã¯ãã¬ã€ã¢ãŠããããŒãžã£ãŒå
ã«é
眮ãããŸãã è¡17ã19ãåæ§ã§ãã
è¡20ã21ã¯ã2ã€ã®ãŠã£ãžã§ãããäºãã«æ¥ç¶ããŸãã ã©ã¡ãã«ãããŠãŒã¶ãŒãå€ãå€æŽãããšãã«çæãããã·ã°ãã«ãvalueChangedïŒintïŒããšãæ°ããå€ãèšå®ããã¹ããããsetValueïŒintïŒãããããŸãã ãããŠã£ãžã§ããã®å€å€æŽä¿¡å·ã¯å¥ã®ãŠã£ãžã§ããã®ã¹ãããã«æ¥ç¶ãããæ°ããå€ãéä¿¡ããŸãã
24è¡ç®ã§ã¯ãã¹ã©ã€ããŒã50ã«èšå®ãã26è¡ç®ã§ãŠã£ã³ããŠããã®äžã«å«ãŸãããã¹ãŠã®ãŠã£ãžã§ãããšãšãã«è¡šç€ºãã28è¡ç®ã§ã¢ããªã±ãŒã·ã§ã³ããèµ·åãããŠããŸãã
24è¡ç®ãå®è¡ããããšãã«äœãèµ·ãããã詳现ã«èª¬æãã䟡å€ããããŸãã¹ã©ã€ããŒãªããžã§ã¯ãã®å€ãå€åããä¿¡å·ãvalueChangedïŒintïŒããçæããŸãã ä¿¡å·ã¯ã¹ãã³ããã¯ã¹ãªããžã§ã¯ãã«éãããsetValueïŒintïŒã¹ãããé¢æ°ã䜿çšããŠåãå€ãèšå®ããŸãããã®é¢æ°ã¯1ã€ã®åŒæ°ïŒæ°ããå€ïŒãåããŸãã 次ã«ãã¹ãã³ããã¯ã¹ã®å€ãå€æŽããããããã¹ã©ã€ããŒãªããžã§ã¯ãã«å¯Ÿå¿ããä¿¡å·ãçæããŸãã ãã ããã¹ã©ã€ããŒã«ã¯ã¹ãã³ããã¯ã¹ãéä¿¡ããå€ãæ¢ã«å«ãŸããŠããããïŒæ°ããå€ãå ±åããã®ã¯æåã§ããïŒãã¹ãããããã ãã§ãæ°ããä¿¡å·ã«åå¿ããããšã¯ãããŸããã ããã«ãããããã°ã©ã ãç¡éã®ãµã€ã¯ã«ã«å
¥ãããšã¯ãããŸããïŒãŠã£ãžã§ããã§ããã®ãããªä¿¡å·ãšã¹ããããäœæããå Žåããã®ãããªç¶æ³ãäºæž¬ãããããã«å¯Ÿããä¿è·ãäœæããå¿
èŠããããŸãïŒã 24è¡ç®ããspinbox.setValueïŒ50ïŒãã«çœ®ãæãããšããŸã£ããåãçµæã«ãªããäž¡æ¹ã®ãŠã£ãžã§ããã®å€ãåãã«ãªããŸãããå€ã®å€æŽãå ±åããã®ã¯spinboxãæåã«ãªããŸãã ãŠãŒã¶ãŒããããã®ãŠã£ãžã§ãããæäœãããšãã«ãåãããšãèµ·ãããŸãããã®å Žåãåãä¿¡å·ãçæããããã¹ãŠãåãã¢ã«ãŽãªãºã ã«åŸã£ãŠçºçããŸãã
ã芧ã®ãšããããŠãŒã¶ãŒãšã®å¯Ÿè©±ãšããã°ã©ã ã®å€èŠ³ã®ã«ã¹ã¿ãã€ãºã¯Qtã«ãã£ãŠç°¡åã«æäŸãããŸãã äžè¬çãªã¢ãããŒãã¯æ¬¡ã®ãšããã§ãïŒãŠã£ã³ããŠãäœæããã¬ã€ã¢ãŠããããŒãžã£ãŒãäœæããŠé
眮ãããŠã£ãžã§ãããäœæããŠããã«æ§æããã¬ã€ã¢ãŠããããŒãžã£ãŒå
ã«é
眮ããå¿
èŠãªä¿¡å·ãšã¹ãããæ¥ç¶ãäœæããããã«ããä»ã®ãŠã£ãžã§ããã«åœ±é¿ãäžãããŠã£ãžã§ããã«ããã©ã«ãå€ãäžããŸãå€ïŒ28è¡ç®ãšããŠïŒã ãã®åŸããŠã£ã³ããŠã衚瀺ããããããè¯ãæéãŸã§æ®ãããšãã§ããŸããããã°ã©ã ã«å€ãã®ãŠã£ã³ããŠãããå Žåã¯ãäºåã«ãã¹ãŠã®ãŠã£ã³ããŠãäœæããå¿
èŠã«å¿ããŠéãããšãã§ããŸãã ãã®ã¢ãããŒãã¯ãQtã䜿çšããå Žåã«æãæ£ãããšèŠãªãããŸãã
ããã§ãã¹ãŠã§ããããŒã3ããåŸ
ã¡ãã ããããæž
èŽããããšãããããŸããã