Home > Mobile >  Help: Qt caused the access log output window
Help: Qt caused the access log output window

Time:10-10

The log output window of Qt, according to the online sample https://jingyan.baidu.com/article/a65957f4d2cce824e67f9b21.html code,
Caused the access at run time, add lock or other methods seems to be no,
According to the commissioning should be global pointer to invoke the Mainwindow class controls and the Mainwindow class itself signal slot call access conflicts caused by the same control,
Montana consult everybody how what better way to resolve conflict or access log content output to a window control

This is outside the Mainwindow class to access the UI, if nothing else, the code
The main. CPP

//define a global pointer to the main window
FMCanera_QT * myWin=NULL;

Void outputMessage (QtMsgType type, const QMessageLogContext & amp; The context, const QString & amp; MSG)
{
The static QMutex mutex.
The lock (); a mutex.
QString text;
The switch (type)
{
Case QtDebugMsg:
Text=QString (" & lt; Font color=\ "# 9 f5f9f " & gt; The Debug: ");
break;
Case QtWarningMsg:
Text=QString (" & lt; Font color=\ "# B5A642 " & gt; Warning: ");
break;
Case QtCriticalMsg:
//myWin - & gt; FM_Critical_State=true;
Text=QString (" & lt; Font color=\ "# FF0000 " & gt; Critical: ");
break;
Case QtFatalMsg:
Text=QString (" & lt; Font color=\ \ "# 800080" & gt; Fatal: ");
Case QtInfoMsg:
Text=QString (" & lt; Font color=\ \ "# 000000" & gt;" );
}

QString context_info=QString (" File (% 1) Line (% 2) "). Arg (QString (context. The File). Arg (context. The Line);

QString current_date_time=QDateTime: : currentDateTime (), toString (" MM - dd yyyy - hh: MM: ss DDD ");

QString current_date=QString (" (% 1) "). Arg (current_date_time);

QString message=QString (" % 1% 2% 3% 4 "). Arg (text). Arg (context_info). Arg (MSG). Arg (current_date);

QString messageWindows=QString (" % 1% 2% 3 "). Arg (text). Arg (current_date). Arg (MSG);
MyWin - & gt; ShowMsg (messageWindows + "& lt;/font>" );//here calls the Mainwindow class controls the output log information

//save the log file
QDateTime c_date_time=QDateTime: : currentDateTime ();
QString c_date1=c_date_time. ToString (" MM/yyyy - ");
QString c_date2=c_date_time. ToString (" yyyy - MM - dd ");
QString filePath="./Log/" + c_date1;

MkMutiDir (filePath);

QString filename=c_date2 filePath + + ". TXT ";

QFile file (filename);

The file open (QIODevice: : WriteOnly | QIODevice: : Append);

QTextStream text_stream (& amp; The file);

Text_stream & lt;
File. The flush ();

File. The close ();

Mutex. Unlock ();

}


Int main (int arg c, char * argv [])
{
QApplication a (arg c, argv);

FMCanera_QT w;

//is assigned to the main window of the pointer to the custom of global pointer
MyWin=& amp; W;

//registered MessageHandler
QInstallMessageHandler (outputMessage);

Baron how ();
Return a.e xec ();
}

Here is in the main window method calls textBrowser controls the output of log information
FMCanera_QT. CPP
//call log output in the main public methods
Void FMCanera_QT: : showMsg (QString MSG)
{
UI. TextBrowser_log - & gt; Append (MSG);//access conflict

}
//private slot, defines the trigger when textBrowser_ controls text changes, scroll to a pointer to the control at the bottom of the
Void FMCanera_QT: : setRecTextFouc ()
{
UI. TextBrowser_log - & gt; MoveCursor (QTextCursor: : End);//access conflict
}

Run error:

CodePudding user response:

Actually, I am quite curious how do you define the UI is, shouldn't be a pointer?
Second, you the myWin usage: do you think of?
MyWin - & gt; ShowMsg (messageWindows + "& lt;/font>" );//here calls the Mainwindow class controls the output log information
You in the function void outputMessage (QtMsgType type, const QMessageLogContext & amp; The context, const QString & amp; MSG)
Used in myWIn with this sentence, you will know where you are wrong,
If (null!={myWin)
.
}

That is to say, you have to place this:
 if (null!={myWin) 
MyWin - & gt; ShowMsg (messageWindows + "& lt;/font>" );//here calls the Mainwindow class controls the output log information
}

CodePudding user response:

The definition of the UI is qt for vs created by default mode, without using a pointer, accustomed to also can't change,

Do you mean
If (null!={myWin)
.
}
Is that a pointer is empty all did not call you,
Actually have a way to call inside the body, and in most of the time is running, only occasionally appear when testing for a long time I said access conflict, similar to the multithreaded access conflict
But this is registered in the main method and the main form in the Mainwindow class access conflict between private bath, I do not know much about these two representatives of qt window problem between the main thread or which thread, and added a thread lock is useless
As follows:
Void FMCanera_QT: : showMsg (QString MSG)
{
QMutexLocker locker (& amp; M_ShowLock);
UI. TextBrowser_log - & gt; Append (MSG);//access conflict
//if (FM_Critical_State)//access conflict
//m_IOCard_PCM27D24DI_Thread - & gt; SetData (0, 0, 3000);
//UI. TextBrowser_log - & gt; MoveCursor (QTextCursor: : End);
}

Void FMCanera_QT: : setRecTextFouc ()
{
QMutexLocker locker (& amp; M_ShowLock);
UI. TextBrowser_log - & gt; MoveCursor (QTextCursor: : End);
}

Void FMCanera_QT: : showMsg (QString MSG)
{
QMutexLocker locker (& amp; M_ShowLock);
UI. TextBrowser_log - & gt; Append (MSG);//access conflict
//if (FM_Critical_State)//access conflict
//m_IOCard_PCM27D24DI_Thread - & gt; SetData (0, 0, 3000);
//UI. TextBrowser_log - & gt; MoveCursor (QTextCursor: : End);
}

Void FMCanera_QT: : setRecTextFouc ()
{
QMutexLocker locker (& amp; M_ShowLock);
UI. TextBrowser_log - & gt; MoveCursor (QTextCursor: : End);
}

And call there will be conflicts in showMsg
As follows:
Void FMCanera_QT: : showMsg (QString MSG)
{
UI. TextBrowser_log - & gt; Append (MSG);
UI. TextBrowser_log - & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • Qt
  • Related