source: src/UIElements/Views/Qt4/QDebugStream.hpp@ 9bdbc7

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 9bdbc7 was ec8471, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: QtLogBox is again included and working thread-safe.

  • QDebugStream is now appending text to QtLogBox in a thread-safe manner by using signal/slots. This code is taken from qt-forum.de.
  • Property mode set to 100644
File size: 2.0 KB
Line 
1/*
2 * QDebugStream.hpp
3 *
4 * Created on: Jun 19, 2014
5 * Author: heber
6 */
7
8#ifndef QDEBUGSTREAM_HPP_
9#define QDEBUGSTREAM_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include <QObject>
17#include <QPlainTextEdit>
18#include <QMutex>
19
20/**
21 * Taken from http://www.qtforum.de/forum/viewtopic.php?t=7970
22 */
23class QDebugStream : public QObject, std::basic_streambuf<char>{
24
25 Q_OBJECT
26
27signals:
28 void sendString(QString text);
29
30public:
31 QDebugStream(std::ostream &stream, QPlainTextEdit* text_edit) : m_stream(stream){
32 log_window = text_edit;
33 m_old_buf = stream.rdbuf();
34 stream.rdbuf(this);
35
36 connect(this, SIGNAL(sendString(QString)), text_edit, SLOT(appendPlainText (QString)));
37 }
38 ~QDebugStream(){
39 // output anything that is left
40 if (!m_string.empty())
41 emit sendString(m_string.c_str());
42 //log_window->appendPlainText(m_string.c_str());
43 m_stream.rdbuf(m_old_buf);
44 }
45
46protected:
47 virtual int_type overflow(int_type v){
48 mutex.lock();
49 if (v == '\n'){
50 emit sendString(m_string.c_str());
51 //log_window->appendPlainText(m_string.c_str());
52 m_string.erase(m_string.begin(), m_string.end());
53 }
54 else
55 m_string += v;
56
57 mutex.unlock();
58 return v;
59 }
60
61 virtual std::streamsize xsputn(const char *p, std::streamsize n){
62 mutex.lock();
63
64 m_string.append(p, p + n);
65 size_t pos = 0;
66 while (pos != std::string::npos){
67 pos = m_string.find('\n');
68 if (pos != std::string::npos){
69 std::string tmp(m_string.begin(), m_string.begin() + pos);
70 emit sendString(tmp.c_str());
71 //log_window->appendPlainText(tmp.c_str());
72 m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
73 }
74 }
75
76 mutex.unlock();
77 return n;
78 }
79
80private:
81 std::ostream &m_stream;
82 std::streambuf *m_old_buf;
83 std::string m_string;
84 QPlainTextEdit* log_window;
85 QMutex mutex;
86};
87
88
89#endif // QDEBUGSTREAM_HPP
Note: See TracBrowser for help on using the repository browser.