Performance text用于记录的区域速度较慢
我有一个Qt应用程序,我想展示一些日志。我使用Performance text用于记录的区域速度较慢,performance,qt,qml,raspbian,raspberry-pi2,Performance,Qt,Qml,Raspbian,Raspberry Pi2,我有一个Qt应用程序,我想展示一些日志。我使用文本区域。但是,如果日志太大或事件发生得太快,GUI无法足够快地绘制Textarea 我已经用Qt-Creator(QML-Profiler)分析了这个问题,如果日志很大,那么绘制GUI需要300毫秒。我在树莓Pi2上使用这个软件 有什么办法解决这个问题吗?我应该使用其他QML控件吗?谢谢 QML代码: TextArea { text: appHandler.rawCommunication readOnly: true
文本区域
。但是,如果日志太大或事件发生得太快,GUI无法足够快地绘制Textarea
我已经用Qt-Creator(QML-Profiler)分析了这个问题,如果日志很大,那么绘制GUI需要300毫秒。我在树莓Pi2上使用这个软件
有什么办法解决这个问题吗?我应该使用其他QML控件吗?谢谢
QML代码:
TextArea {
text: appHandler.rawCommunication
readOnly: true
}
C++代码:
Q_PROPERTY(QString rawCommunication READ rawCommunication WRITE setrawCommunication NOTIFY rawCommunicationChanged)
void setrawCommunication(QString val)
{
val.append("\n");
val.append(m_rawCommunication);
m_rawCommunication = val;
emit rawCommunicationChanged(m_rawCommunication);
}
用一个,比如。根据用户在列表中的位置,视图表示需要显示哪些数据。这意味着它们在显示大量数据方面比TextArea
之类的项目表现得更好,在您的情况下,这将在内存中保留大量不断增长的字符串
然后,您可以是一个TextArea
,这样每个日志行就有一个可编辑的文本块。然而,如果你不需要造型,我建议你选择更轻一点的,比如。更进一步:如果您不需要可编辑文本,请使用纯旧的文本。切换到这些方法可能没有多大区别,但如果您仍然看到速度缓慢(并且一次可以看到很多代理),则值得一试。尝试以下方法:
创建C++日志记录类
将所有日志追加到此类
并使用一些操作打印它们,例如单击按钮
这将解决您的性能问题
代码示例:
记录器.h
project.pro
使用这种方法,当您想要发布您的软件时,您可以通过一行更改停止所有日志但是,我已经包含了完整的代码,使用“QAbstractListModel”将大量数据记录到QML
listmodel.h
#ifndef LISTMODEL_H
#define LISTMODEL_H
#include <QAbstractListModel>
class ListModel: public QAbstractListModel
{
Q_OBJECT
public:
ListModel();
// Q_PROPERTY(QStringList logs READ name WRITE nameChanged)
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
Q_INVOKABLE QVariant activate(int i);
private:
QStringList m_list;
};
#endif // LISTMODEL_H
main.cpp
#include "listmodel.h"
#include <QFile>
#include <QHash>
ListModel::ListModel()
{
QFile file("/home/ashif/LogFile");
if(!file.open(QIODevice::ReadOnly))
{
qDebug( "Log file open failed" );
}
bool isContinue = true;
do
{
if(file.atEnd())
{
isContinue = false;
}
m_list.append(file.readLine());
}
while( isContinue);
}
int ListModel::rowCount(const QModelIndex & parent ) const
{
return m_list.count();
}
QVariant ListModel::data(const QModelIndex & index, int role ) const
{
if(!index.isValid()) {
return QVariant("temp");
}
return m_list.value(index.row());
}
QVariant ListModel::activate(int i)
{
return m_list[i];
}
#include <QGuiApplication>
#include <QQmlContext>
#include <QQmlApplicationEngine>
#include "logger.h"
#include "listmodel.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
Logger myLogger;
ListModel listModel;
engine.rootContext()->setContextProperty("mylistModel", &listModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
#包括
#包括
#包括
#包括“logger.h”
#包括“listmodel.h”
int main(int argc,char*argv[])
{
QGUI应用程序应用程序(argc、argv);
qqmlaplicationengine;
记录器myLogger;
ListModel ListModel;
engine.rootContext()->setContextProperty(“mylistModel”和&listModel);
engine.load(QUrl(QStringLiteral(“qrc:/main.qml”));
返回app.exec();
}
我尝试了列表视图
的建议,但它有几个缺点:
- 添加新输出时,没有简单的方法将视图定位在底部
- 没有跨行/代理的选择
因此,我最终使用了一个缓存的TextArea
,每秒更新一次:
TextArea {
id: outputArea_text
wrapMode: TextArea.Wrap
readOnly: true
font.family: "Ubuntu Mono, times"
function appendText(text){
logCache += text + "\n";
update_timer.start();
}
property string logCache: ""
Timer {
id: update_timer
// Update every second
interval: 1000
running: false
repeat: false
onTriggered: {
outputArea_text.append(outputArea_text.logCache);
outputArea_text.logCache = "";
}
}
Component.onCompleted: {
my_signal.connect(outputArea_text.appendText)
}
}
为什么不使用参数引用void setrawCommunication(QString&val)
您可能还需要只在TextArea中添加新文本,而不是每次都更改它。因此,在C++中,你只需发送“新文本”,而不是永久地添加到QStand。我已经用ListVIEW ListVIEW {模型:LogMeaveReave:Ttext {{Te:St:}} WOLFY82:解决你的代码更新版本来解决它。
#add this line
# comment it, run qmake and recompile to disable logs
DEFINES += ENABLElogs
#ifndef LISTMODEL_H
#define LISTMODEL_H
#include <QAbstractListModel>
class ListModel: public QAbstractListModel
{
Q_OBJECT
public:
ListModel();
// Q_PROPERTY(QStringList logs READ name WRITE nameChanged)
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
Q_INVOKABLE QVariant activate(int i);
private:
QStringList m_list;
};
#endif // LISTMODEL_H
#include "listmodel.h"
#include <QFile>
#include <QHash>
ListModel::ListModel()
{
QFile file("/home/ashif/LogFile");
if(!file.open(QIODevice::ReadOnly))
{
qDebug( "Log file open failed" );
}
bool isContinue = true;
do
{
if(file.atEnd())
{
isContinue = false;
}
m_list.append(file.readLine());
}
while( isContinue);
}
int ListModel::rowCount(const QModelIndex & parent ) const
{
return m_list.count();
}
QVariant ListModel::data(const QModelIndex & index, int role ) const
{
if(!index.isValid()) {
return QVariant("temp");
}
return m_list.value(index.row());
}
QVariant ListModel::activate(int i)
{
return m_list[i];
}
import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
Window {
visible: true
ListView
{
width: 200; height: 250
anchors.centerIn: parent
model:mylistModel
delegate: Text
{
text:mylistModel.activate(index)
}
}
}
#include <QGuiApplication>
#include <QQmlContext>
#include <QQmlApplicationEngine>
#include "logger.h"
#include "listmodel.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
Logger myLogger;
ListModel listModel;
engine.rootContext()->setContextProperty("mylistModel", &listModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
TextArea {
id: outputArea_text
wrapMode: TextArea.Wrap
readOnly: true
font.family: "Ubuntu Mono, times"
function appendText(text){
logCache += text + "\n";
update_timer.start();
}
property string logCache: ""
Timer {
id: update_timer
// Update every second
interval: 1000
running: false
repeat: false
onTriggered: {
outputArea_text.append(outputArea_text.logCache);
outputArea_text.logCache = "";
}
}
Component.onCompleted: {
my_signal.connect(outputArea_text.appendText)
}
}