2017-03-06 26 views
0

我正在爲我的3D打印機設計一個GUI。在主屏幕上,您可以選擇開始新的打印或更改設置(進給率,加速度等)。照我設置的例子如下this。但是在示例中unlinke,我的設置不是GUI的屬性(如x位置,寬度等),而是通過文本字段輸入的原始數據,對GUI不重要。這些設置將保存在GUI末尾的.txt文件中,該文件稍後將轉換爲打印機的代碼。Qt/QML:保存並中止設置

我的第一個問題是,我如何設計一個保存和中止按鈕?用戶應該能夠更改文本字段,但只有在按下保存按鈕時,更改才應該是終端。

我的第二個問題是,我是否總是讀出文本字段?設置通常不應該改變,所以有更有效的方法嗎? (閱讀被點擊保存按鈕只有當文本字段,否則採用以前的值

對於保存按鈕,我想這樣的事情,但我不工作:

Item { 
    TextField {id: testField} 
    Button {id: testButton; text: "Accept"; onClicked: settings.state = "inactive"} 
    Settings {id:settings; property string state: "active"} 
    state: settings.state 
    states: [ 
      State {name: "active"} 
      State {name:"inactive"; property alias test: testField.text} 
    ] 
} 

回答

0

文件操作必須是在C++級別上完成的。下面的代碼會呈現出開放的一個很好的例子,保存和編輯操作Qt。 它採用小部件,以便添加以下的.pro文件。

QT += widgets 

qmlfile.h

//qmlfile.h 
#ifndef QMLFILE_H 
#define QMLFILE_H 

#include <QObject> 

class QMLFile : public QObject 
{ 
    Q_OBJECT 

public: 
    explicit QMLFile(QObject *parent = 0); 

    Q_INVOKABLE QString getFileContents() const; 

    Q_INVOKABLE void saveFileContents(QString fileContents) const; 
}; 

#endif 

qmlfile.cpp

#include <QFileDialog> 
#include <QTextStream> 
#include <QDebug> 
#include "qmlfile.h" 

QMLFile::QMLFile(QObject *parent): QObject(parent) 
{ 

} 

QString QMLFile::getFileContents() const 
{ 
    QString fileName = QFileDialog::getOpenFileName(NULL, tr("Open File"), "/home", tr("Text Files (*.txt)")); 
    qDebug() << "fileName:" << fileName; 
    QFile file(fileName); 
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) 
     return ""; 

    QString content = file.readAll(); 
    file.close(); 
    return content; 
} 

void QMLFile::saveFileContents(QString fileContents) const 
{ 
    QString fileName = QFileDialog::getSaveFileName(NULL, tr("Save File"), "/home/ansh/data.txt", tr("Text Files (*.txt)")); 

    QFile file(fileName); 
    if(file.open(QIODevice::WriteOnly | QIODevice::Text)) 
    { 
     qDebug() << "created file:" << fileName; 
     QTextStream stream(&file); 
     stream << fileContents << endl; 
     file.close(); 
     return; 
    } 
    else 
    { 
     qDebug() << "could not create file:" << fileName; 
     return; 
    } 
} 

的main.cpp

#include <QGuiApplication> 
#include <QQuickView> 
#include <QQmlContext> 
#include <QApplication> 
#include "qmlfile.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 

    QQuickView view; 
    QMLFile qmlFile; 
    view.rootContext()->setContextProperty("QMLFile", &qmlFile); 
    QObject::connect((QObject*)view.engine(), SIGNAL(quit()), QApplication::instance(), SLOT(quit())); 
    view.setSource(QUrl(QLatin1String("qrc:/main.qml"))); 
    view.show(); 

    return app.exec(); 
} 

main.qml

import QtQuick 2.5 
import QtQuick.Controls 2.0 

Rectangle { 
    width: 360; height: 360 

    Rectangle{ 
     id:buttons 
     height: 50; width: parent.width; anchors.top: parent.top 

     Row { 
      Button { 
       text: "open" 
       onClicked: txt.text = QMLFile.getFileContents(); 
      } 

      Button { 
       text: "save" 
       onClicked: QMLFile.saveFileContents(txt.text); 
      } 
      Button { 
       text: "Abort" 
       onClicked: Qt.quit() 
      } 
      spacing: 5 
     } 
    } 

    Rectangle{ 
     id:textHandle 
     width: parent.width; height: parent.height - buttons.height; anchors.bottom: parent.bottom 

     TextEdit{ 
      id: txt; anchors.fill: parent 
     } 
    } 
} 

對於第二個問題,由於設置是動態的,因此必須每次都讀取。

+0

哇,非常感謝,我會嘗試它,當我回家! – MilloMille

+0

@MilloMille請注意或接受答案。 –