Textarea slow for logging

后端 未结 4 579
北海茫月
北海茫月 2020-12-21 14:35

I have a Qt application and I\'d like to show some log. I use a TextArea. However, if the log is large or the events come too fast the GUI can\'t draw Te

4条回答
  •  粉色の甜心
    2020-12-21 14:54

    However, I have included complete code, using "QAbstractListModel" for a Logging heavy data to QML

    listmodel.h

    #ifndef LISTMODEL_H
    #define LISTMODEL_H
    #include 
    
    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
    

    listmodel.cpp

    #include "listmodel.h"
    #include 
    #include 
    
    
    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];
    }
    

    main.qml

    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)
            }
        }
    }
    

    main.cpp

    #include 
    #include 
    #include 
    #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();
    }
    

提交回复
热议问题