Textarea slow for logging

后端 未结 4 591
北海茫月
北海茫月 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:57

    I tried the ListView suggestion but it has several drawbacks:

    • No easy way to keep the view positioned at the bottom when new output is added
    • No selection across lines/delegates

    So I ended up using a cached TextArea, updating once every second:

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

提交回复
热议问题