Seite 1 von 1

[solved]Singleton, Signal emit Problem

Verfasst: 11. Mai 2007 09:33
von Halcom
Hallo,

ich arbeite mit XCode und qt 4.2.3.

Ich habe mir einen Singleton erstellt. Dieser hat eine Methode die ein Signal emitten soll. Die methode Singleton wird von einer anderen Klasse aufgerufen( Sie wird auch korrekt aufgerufen ) . In einer dritten Klasse wurde das signal des Singleton verbunden. Jedoch wird der entsprechende slot nicht aufgerufen.
Ein kurzer Testcode dazu:

main.cpp

Code: Alles auswählen

#include <QApplication>
#include <mainwindow.h>
int main(int argc, char * argv[])
{	
	QApplication app(argc,argv);
	MainWindow mainWin;
       mainWin.show();	
	return app.exec();
}
Code vom Singleton.h:

Code: Alles auswählen

#include <QObject>
class Singleton : public QObject
{ 
  Q_OBJECT; 
public: 
  // Methods to handle singleton instance. 
  static Singleton* getInstance(); ///< Get singleton instance. Create instance if necessary. 
  static bool hasInstance();            ///< Check if the singleton has an instance. 
  void destroyInstance();               ///< Destroy the instance of the singleton 
  void MethodToCall(); 
private: 
  Singleton();
  ~Singleton();
  /// Member to hold the singleton instance. 
  static Singleton * _instance; 
signals: 
  void emittedSignal(); 
};
Code von Singelton.cpp

Code: Alles auswählen

#include "Singleton.h" 
Singleton * Singleton::_instance = 0; 
Singleton:: Singleton() { 
} 
Singleton::~ Singleton() { 
} 
Singleton * Singleton::getInstance() { 
  if(_instance==0) 
  { 
    _instance = new Singleton; 
  } 
  return _instance; 
}
bool Singleton::hasInstance() { 
  return _instance!=0; 
} 
void Singleton::destroyInstance(){ 
 delete _instance; 
 _instance=NULL; 
} 
void Singleton::MethodToCall() { 
  emit this->emittedSignal(); 
} 
mainwindow.h

Code: Alles auswählen

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class Singleton;
class MainWindow : public QMainWindow
{
	Q_OBJECT
	
	public:
		MainWindow();
		~MainWindow();
		
	private:
				
    Singleton * m_singleton;      	
                          			
	private slots:
    void emitSignal();	
};
#endif
mainwindow.cpp

Code: Alles auswählen

#include <QTGui>
#include <QPushButton>
#include "mainwindow.h"
#include "Singleton.h"
#include "Reciever.h"

MainWindow::MainWindow(){
	setWindowTitle(tr("SINGLETON EMIT TEST"));
  m_singleton = Singleton::getInstance();
  QWidget *w =new QWidget(this);
  setCentralWidget(w);
  QPushButton * testButton = new QPushButton("Emit Signal" ,this);
  QVBoxLayout * mainLayout = new QVBoxLayout(this);
  mainLayout->addWidget(testButton);
  
  connect(testButton, SIGNAL(clicked()), this, SLOT(emitSignal()));

  Reciever * rec = new Reciever(this);
  mainLayout->addWidget(rec);
  w->setLayout(mainLayout);
}

MainWindow::~MainWindow(){
	m_singleton->destroyInstance();
}

void MainWindow::emitSignal(){
  m_singleton->MethodToCall();
}
reciever.h

Code: Alles auswählen

#ifndef RECIEVER_H
#define RECIEVER_H
#include <QWidget>
class Singleton; 
class QLabel;
class Reciever : public QWidget 
{ Q_OBJECT
  public: 
    Reciever(QWidget *i_parent); 
    ~Reciever(); 
  private: 
    Singleton * m_singleton;    
    QLabel * m_msgLabel;      
  public slots: 
    void recieverSlot(); 
}; 
#endif
reciever.cpp

Code: Alles auswählen

#include <QTGui>
#include "Singleton.h" 
#include "Reciever.h" 

Reciever::Reciever(QWidget *i_parent)
: QWidget(i_parent),
  m_singleton(NULL),
  m_msgLabel(NULL)
{ 
   m_singleton = Singleton::getInstance(); 
   connect(m_singleton, SIGNAL(emittedSignal()),this,SLOT(recieverSlot())); 
   m_msgLabel = new QLabel("wait",this);
} 

Reciever::~Reciever() { 
} 

void Reciever::recieverSlot() { 
  m_msgLabel->setText("CONNECTED");
} 
Wer kann mir hierbei weiterhelfen?

Danke im vorraus.

gruß Mike

EDIT: lauffähiges testprog

Verfasst: 11. Mai 2007 09:52
von Christian81
Q_OBJECT vergessen, sagt Dir auch Qt zur Laufzeit in der Konsole.
Ausserdem sollte hinter Q_OBJECT kein ';' ;)

Verfasst: 11. Mai 2007 10:03
von Halcom
Das Q_Object habe ich nur hier im Beispielcode vergessen . In meiner richtigen anwendung ist es da. =)

Verfasst: 11. Mai 2007 10:07
von Christian81
Dann schau mal was die Konsole so sagt wenn der Slot ausgeführt werden soll. Unter win32 in die pro-Datei 'CONFIG += console' einfügen (siehe auch qmake-Doku)

Verfasst: 11. Mai 2007 10:18
von Halcom
Bin leider unter MAC OS 10.4. Habe ich eingefügt die Konsole sagt gar nichts. ( Ich hoffe ich habs richtig gemacht, aber viel falsch machen kann man eigentlichnicht ;) )

Verfasst: 11. Mai 2007 10:21
von Christian81
So auf die Schnelle sehe ich keine weiteren Problem. Kannst Du evtl. ein Testcase bauen?

Ausserdem (weiss nicht ob es ggf. moc in irgend einer Art und Weise verärgert):
emit this->emittedSignal();
-->
emit emittedSignal();

Verfasst: 11. Mai 2007 10:28
von Halcom
auf die Idee bin ich auch schon gekommen. Macht aber keinen Unterschied ob ich this->emit oder emit schreibe. Habe es auch schon mit emit->emittedSignal ( void) versucht geht aber auch nicht. Der Code ist quasi mein TestCode.

Verfasst: 11. Mai 2007 10:33
von Christian81
Ja, wäre nur schön wenn ich ihn bei mir auch mal kompilieren könnte damit ich durchsteppen kann. Abschreiben werde ich ihn auf alle Fälle nicht :)

Verfasst: 11. Mai 2007 10:37
von Halcom
Mache ich gleich =) gib mir mal ein paar min.

Verfasst: 11. Mai 2007 12:04
von Halcom
So hier das Testprog. Es funktioniert :shock: . Nun muss ich nur noch rausfinden was am anderen code anders ist. Danke für die hilfe =)

Verfasst: 11. Mai 2007 16:44
von Burgpflanze
Halcom hat geschrieben:So hier das Testprog. Es funktioniert :shock: . Nun muss ich nur noch rausfinden was am anderen code anders ist. Danke für die hilfe =)
Wo denn nur? :wink: