ich habe mir ein Buch zur Erklärung von QT4 gekauft.
Nun ist da aber eine Sache die ich nicht verstehe.
/widget.h
Code: Alles auswählen
#include <QtGui/QWidget>
class Widget : public QObject
{
Q_OBJECT
private :
int val;
public://kon,-Destruktor
Widget();
~Widget();
//get methode
int value()const;
public slots :
void setValue(int);
signals :
void valueChanged(int);
};
Code: Alles auswählen
#include "widget.h"
//kon,-Destruktor
Widget::Widget()
{
val = 0;
}
Widget::~Widget()
{
}
//slots
void Widget::setValue(int v)
{
if(v != val){
val = v;
}
emit valueChanged(v);
}
//methode
int Widget::value()const
{
return val;
}
/main.cpp
Code: Alles auswählen
#include <QApplication>
#include "widget.h"
#include <QMessageBox>
#include <QString>
//simple nachrichten-box
void MyMessageBox(Widget& a, Widget& b, QString title){
QString qstr, qval;
//String zusammenbasteln
qstr.append(title);
qstr.append("\na :");
qval.setNum(a.value());
qstr.append(qval);
qstr.append("\nb :");
qval.setNum(b.value());
qstr.append(qval);
QMessageBox::information(NULL, "Widget Information", qstr, QMessageBox::Ok);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget *a = new Widget;
Widget *b = new Widget;
QObject::connect(a, SIGNAL(valueChanged(int)),
b, SLOT(setValue(int)));
//b.val bekommt den wert 100
b->setValue(100);
MyMessageBox(*a,*b,"b->setValue()");
a->setValue(99);
MyMessageBox(*a,*b,"a->setValue()");
return 0;
}