2017-08-19 71 views
0

也會在頁面加載之前調用C++函數。下面是問題所在,我創建了一個C++類,它加載一個本地文件,併爲每個新行發送一個信號。我想要做的是在我的QML文件中,我想將這些行打印到我已經完成的listview中,但現在的問題是即使在應用程序啓動之前C++函數也會加載,它會讀取文件並填充listview然後顯示該頁面。即使使用Component.onCompleted

這是我的代碼。

的main.cpp

#include <QtCore/QCoreApplication> 
#include <QtGui/QGuiApplication> 
#include <QtQuick/QQuickView> 
#include <QtQml> 


#include "boot.h" 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication::setApplicationName("xyz"); 
    QCoreApplication::setAttribute(Qt::AA_X11InitThreads); 

    qmlRegisterType<Boot>("xyx", 1, 0, "Boot"); 

    QGuiApplication app(argc, argv); 

    QQuickView quickView; 
    quickView.setSource(QUrl(QStringLiteral("qrc:/Boot.qml"))); 
    quickView.setResizeMode(QQuickView::SizeRootObjectToView); 
    quickView.showMaximized(); 

    return app.exec(); 
} 

Boot.qml

import QtQuick 2.0 
import "." 
import QtQuick.XmlListModel 2.0 
import xyz 1.0 

Item{ 

    Loader{ 
     id: bootPageLoader 
     anchors.fill:parent 
     sourceComponent: bootSystem 
     focus:true 
    } 

    Component{ 
     id:bootSystem 

     Rectangle{ 
      width: 640 
      height: 480 
      color:"black" 
      focus:true 

      Component.onCompleted: { 
       booting.load(); 
      } 

      Boot{ 
       id:booting 

       onErrorMsgChanged: { 
        console.log("New Boot Log Message: " + errorMsg); 
        //This is where I am adding to the listview every time I receive a signal 
        logModel.append({msg:errorMsg}); 
        log.positionViewAtEnd(); 


       } 
      } 

      ListView { 
       id:log 
       anchors.left: parent.left 
       anchors.leftMargin: 10 
       anchors.topMargin: 10 
       anchors.bottomMargin:10 
       anchors.top: parent.top 
       width:(parent.width*40)/100 
       height:parent.height-20 

       model: BootLogModel{ 
        id:logModel 
       } 
       delegate: Text { 
        text: msg 
        color: "green" 
        font.pixelSize: 15 
       } 
      } 

      Text{ 
       id: loading 
       anchors{ 
        horizontalCenter: parent.horizontalCenter 
        verticalCenter: parent.verticalCenter 
       } 

       text: "LOADING..." 
       color: "white" 
       font.pixelSize: Math.round(parent.height/20) 
       font.bold: true 
      } 
     } 
    } 
} 

BootLogModel.qml

import QtQuick 2.0 
ListModel { 

} 

這裏是C++碼S nippet

在boot.h

#ifndef BOOT_H 
#define BOOT_H 

#include <QObject> 
#include <QString> 
#include <string> 

class Boot : public QObject{ 
    Q_OBJECT 
    Q_PROPERTY(QString errorMsg READ errorMsg NOTIFY errorMsgChanged) 

    public: 
     explicit Boot(QObject *parent = 0); 
     Q_INVOKABLE void load(); 

     QString errorMsg(); 
     void setErrorMsg(const QString &errorMsg); 

    signals: 
     void errorMsgChanged(); 
    private: 
     QString m_errorMsg; 
}; 
#endif // BOOT_H 

在boot.cpp

Boot::Boot(QObject *parent) : QObject(parent) 
{ 
} 
QString Boot::errorMsg() 
{ 
    return m_errorMsg; 
} 

void Boot::setErrorMsg(const QString &errorMsg) 
{ 
    m_errorMsg = errorMsg; 

    emit errorMsgChanged(); 
} 

void Boot::load() 
{ 
    int i = 0; 
    while(i < 10000) 
    { 
     setErrorMsg("test: " + QString::number(i)); 
     i++; 
    } 

} 

我GUI之前首先看到這個 I first see this before the GUI

那麼這是GUI的存在顯示並已填充 Then this is the GUI being displayed and already populated

+0

如果你的問題是把它打印出來數據然後我有一個解決方案的具體情況,你也應該知道,循環不是GUI友好的。 – eyllanesc

+0

請提出可能的解決方案。這個問題不在循環中,我想要做的就是每當我調用「SetErrorMsg」時,我希望它將消息添加到列表視圖並更新gui。我想在任何其他功能中做到這一點。 –

+0

說我有一個叫做「play」的函數,我想要做的就是如果play有錯誤,那麼我想設置錯誤信息併發出一個信號,當它改變時它會更新gui。 –

回答

3

循環總是在GUI中的一個問題,它始終是更好地尋找到GUI一個友好的替代,在這種情況下,QTimer

boot.h

int counter = 0; 

bool.cpp

void Boot::load() 
{ 
    QTimer *timer = new QTimer(this); 

    connect(timer, &QTimer::timeout, [timer, this](){ 
     setErrorMsg("test: " + QString::number(counter)); 
     counter++; 
     if(counter > 10000){ 
      timer->stop(); 
      timer->deleteLater(); 
     } 
    }); 
    timer->start(0); //delay 
}