Log issues
-
In fact, nowhere in any of the code you've entered here do you ever actually instantiate a QTextBrowser. Where is your allocation statement (e.g.
textBrowser = new QTextBrowser;)?Thanks. Now it works but the same as it was without
static, when I useLog::writeToLog("Some data");to write toQTextBrowserin for exampleTestwindow and close it, then openLogwindow to check data,QTextBrowseris empty.To open
LogI use:Log *appLog = new Log(this); appLog->show(); -
How/where are you allocating the
QTextBrowser? -
How/where are you allocating the
QTextBrowser?I have initialized
textBrowser = new QTextBrowser();in constructor and in function below because constructor is not called when I callLog::writeToLog("Some data")inTest.cppand app crashes.void Log::writeToLog(QString logMessage) { textBrowser = new QTextBrowser(); textBrowser->append(logMessage); }But the result is the same (
QTextBrowseris empty), I think because of two initialization. -
Definitely do not make a new QTextBrowser every time you call writeToLog, that defeats the purpose of making it static. You need to instantiate it one time in any given program run. Start by initializing it to null (or Q_NULLPTR), then in writeToLog check to see if it exists already. If it does not, then allocate it and set its parameters as appropriate, so something like:
QTextBrowser *Log::textBrowser = null; ... void Log::writeToLog(QString logMessage) { if (!textBrowser) { textBrowser = new QTextBrowser; textBrowser->installEventFilter(this); connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser); } textBrowser->append(logMessage); } -
(Note that my example code won't actually work as written because writeToLog is static, so you don't have a "this" pointer... those two lines will need to be in your constructor, along with appropriate checks to create textBrowser there if needed.)
-
Definitely do not make a new QTextBrowser every time you call writeToLog, that defeats the purpose of making it static. You need to instantiate it one time in any given program run. Start by initializing it to null (or Q_NULLPTR), then in writeToLog check to see if it exists already. If it does not, then allocate it and set its parameters as appropriate, so something like:
QTextBrowser *Log::textBrowser = null; ... void Log::writeToLog(QString logMessage) { if (!textBrowser) { textBrowser = new QTextBrowser; textBrowser->installEventFilter(this); connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser); } textBrowser->append(logMessage); }Thanks for code example. It now works. One more question, since
Log::writeToLog(QString logMessage)is astaticfunction I can't usethis, I get error:log.cpp:116: error: C2355: 'this': can only be referenced inside non-static member functions or non-static data member initializers. So I need to get the instance of theLogclass. Any ideas? -
Yes, remove those two lines from the if statement in writeToLog, but keep them in an identical if statement in your constructor.
-
Yes, remove those two lines from the if statement in writeToLog, but keep them in an identical if statement in your constructor.
I have added also check in constructor, because it crashes when open
Logwithout adding anything to it.if (!textBrowser) { textBrowser = new QTextBrowser(); textBrowser->installEventFilter(this); connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser); } else { textBrowser->installEventFilter(this); connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser); }Also I have notices that
connect(textBrowser, &QTextBrowser::textChanged, this, &Log::checkLogTextBrowser);is not working. I have some buttons which is disabled and when new log data is added, those buttons should be enabled, but it still disabled.void Log::checkLogTextBrowser() { if (textBrowser->document()->isEmpty()) { saveLog->setEnabled(false); saveLogAs->setEnabled(false); openLog->setEnabled(false); clearLog->setEnabled(false); deleteLog->setEnabled(false); sendLog->setEnabled(false); } else { saveLog->setEnabled(true); saveLogAs->setEnabled(true); openLog->setEnabled(false); clearLog->setEnabled(true); deleteLog->setEnabled(false); sendLog->setEnabled(false); } }I can use some trigger as
bool checkButtonsStateto check when log is added and then set it true to enable buttons? But whyconnectis not working? -
Make sure to call checkLogTextBrowser() directly in your constructor as well, to handle anything that was logged before you opened it.
-
Make sure to call checkLogTextBrowser() directly in your constructor as well, to handle anything that was logged before you opened it.
Now everything works. Thank you.