Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance text用于记录的区域速度较慢_Performance_Qt_Qml_Raspbian_Raspberry Pi2 - Fatal编程技术网

Performance text用于记录的区域速度较慢

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

我有一个Qt应用程序,我想展示一些日志。我使用
文本区域
。但是,如果日志太大或事件发生得太快,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
,这样每个日志行就有一个可编辑的文本块。然而,如果你不需要造型,我建议你选择更轻一点的,比如。更进一步:如果您不需要可编辑文本,请使用纯旧的
文本。切换到这些可能没有多大区别,但是如果您仍然看到缓慢(并且一次可以看到很多代理),那么值得一试。根据用户在列表中的位置,视图表示需要显示哪些数据。这意味着它们在显示大量数据方面比
TextArea
之类的项目表现得更好,在您的情况下,这将在内存中保留大量不断增长的字符串

然后,您可以是一个
TextArea
,这样每个日志行就有一个可编辑的文本块。然而,如果你不需要造型,我建议你选择更轻一点的,比如。更进一步:如果您不需要可编辑文本,请使用纯旧的
文本。切换到这些方法可能没有多大区别,但如果您仍然看到速度缓慢(并且一次可以看到很多代理),则值得一试。

尝试以下方法: 创建C++日志记录类 将所有日志追加到此类 并使用一些操作打印它们,例如单击按钮 这将解决您的性能问题

代码示例: 记录器.h project.pro 使用此方法,当您想发布软件时,可以通过一行更改停止所有日志。请尝试以下方法: 创建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();
}

然而,我已经包含了完整的代码,使用“QabStretcListModel”将大量数据记录到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)
            }
        }

我尝试了
ListView
建议,但它有几个缺点:

  • 添加新输出时,没有简单的方法将视图定位在底部
  • 没有跨行/代理的选择
因此,我最终使用了一个缓存的
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。为什么不使用参数引用?code>void setrawCommunication(QString&val)
您可能还需要只在TextArea中添加新文本,而不是每次都更改它。因此,在C++中,您只需发送“新文本”,而不是永久地添加到QStand。我已经用ListVIEW ListVIEW {模型:LogMeaveDeave:Ttext { text:显示}}} WOLFY82:解决了用代码的更新版本来详细说明的问题吗?我已经用listview ListView解决了它。{model:logModel delegate:Text{Text:display;}}@Wolfy82:想用代码的更新版本详细说明一下吗?
#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)
            }
        }