Jun 17, 2016 06:51
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Counter(QObject):
def __init__(self, parent=None):
QObject.__init__(self, parent)
self.m_nValue=0
def slotInc(self):
self.m_nValue+=1
self.emit(SIGNAL('counterChanged(int)'),self.m_nValue)
if (self.m_nValue == 5) :
self.emit(SIGNAL('goodbye()'))
app=QApplication(sys.argv)
lbl=QLabel("0");
cmd=QPushButton("ADD");
counter=Counter();
lbl.move(0,0)
cmd.move(150,0)
lbl.show();
cmd.show();
QObject.connect(cmd, SIGNAL('clicked()'),counter.slotInc)
QObject.connect(counter, SIGNAL('counterChanged(int)'),lbl, SLOT('setNum(int)'))
QObject.connect(counter, SIGNAL('goodbye()'),app, SLOT('quit()'))
sys.exit(app.exec_())
"""
//Listing 2.8
//======================================================================
//main.cpp
#include //Qt4
//Qt5: #include
#include "Counter.h"
int main (int argc, char** argv)
{
QApplication app(argc, argv);
QLabel lbl("0");
QPushButton cmd("ADD");
Counter counter;
lbl.show();
cmd.show();
QObject::connect(&cmd, SIGNAL(clicked()),
&counter, SLOT(slotInc())
);
QObject::connect(&counter, SIGNAL(counterChanged(int)),
&lbl, SLOT(setNum(int))
);
QObject::connect(&counter, SIGNAL(goodbye()),
&app, SLOT(quit())
);
return app.exec();
}
//======================================================================
//Counter.h
#ifndef _Counter_h_
#define _Counter_h_
#include
class Counter : public QObject {
Q_OBJECT
private:
int m_nValue;
public:
Counter();
public slots:
void slotInc();
signals:
void goodbye ( );
void counterChanged(int);
};
#endif //_Counter_h_
//======================================================================
//Counter.cpp
#include "Counter.h"
Counter::Counter() : QObject()
, m_nValue(0)
{ }
void Counter::slotInc()
{
emit counterChanged(++m_nValue);
if (m_nValue == 5) {
emit goodbye();
}
}
"""
P.S.
В калькуляторе (далее) у него, кажется, ошибка. Ну и книга "Программирование GUI на C++. Бланшет, Саммерфильд" - очень хороша и стоит ее тоже почитать.
питон,
qt