ich möchte ein Logwindow erstellen, dass mit Hilfe von static Methoden beschrieben werden soll. Die Klasse Logwindow ist mit Hilfe eines Singletons implementiert, sodass ich einfach Logwin::info("infomessage"); aufrufen kann, ohne mich vorher um eine explizierte Instanziierung kümmern muss.
Im meinem Fall wird das Logwin zuerst von Thread A aufgerufen. Dort klappen alle Aufrufe.
Später ruft Thread B Logwin::debug("text") auf. In der Konsole erscheint folgendes:
QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)
Anscheinend liegt das Problem darin, dass das textField im Thread A erzeugt wurde und später von Thread beschrieben wird.
Ich habe dann versucht innerhalb der Klasse mit Signal/Slots zu arbeiten, was allerdings durch die nötigen static SIGNALS nicht geklappt hat (debug(QString) müsste ein static emit aufrufen).
Kennt wer eine Lösung?
Code: Alles auswählen
#ifndef LOGWINDOW_H
#define LOGWINDOW_H
#include <QtGui>
#include <QTime>
#include <QColor>
#include <iostream>
class Logwindow : public QWidget
{
Q_OBJECT
public:
static void info(QString str);
static void warning(QString str);
static void error(QString str);
/// appends new Line to Log output
/// \param str Log text
void appendText(QString str, QColor c);
private:
QTextEdit *textField;
static Logwindow* exemplar;
Logwindow (QWidget*);
};
#endif
Code: Alles auswählen
#include "Logwindow.h"
Logwindow* Logwindow::exemplar = 0;
Logwindow::Logwindow(QWidget *parent=0)
{
textField= new QTextEdit();
textField->setReadOnly(TRUE);
//layout krams
}
void Logwindow::error(QString str)
{
if(exemplar == 0)
exemplar = new Logwindow();
exemplar->appendText("[ERROR] " + str, Qt::red);
}
void Logwindow::warning(QString str)
{
if(exemplar == 0)
exemplar = new Logwindow();
exemplar->appendText("[WARNING] " + str, Qt::blue);
}
void Logwindow::info(QString str)
{
if(exemplar == 0)
exemplar = new Logwindow();
exemplar->appendText("[INFO] " + str, Qt::black);
}
void Logwindow::appendText(QString str, QColor c)
{
textField->setTextColor(c);
textField->append(QTime::currentTime().toString("hh:mm:ss")+" "+str);
}