2017-03-06 4 views
0

3DプリンタのGUIを設計しています。ホーム画面では、新しい印刷を開始するか、設定(送り速度、加速など)を変更するかを選択できます。私が設定した設定には、thisの例が続きます。しかし、この例のように、私の設定はGUIのプロパティ(x postion、widthなど)ではなく、テキストフィールドを介して入力される未処理のデータであり、GUIにとって重要ではありません。設定はGUIの最後に.txtファイルとして保存され、後でプリンタのコードに変換されます。Qt/QML:設定を保存して中止する

私の最初の質問は、どのように私は保存ボタンと中止ボタンを設計するのですか?ユーザーはテキストフィールドを変更できるはずですが、変更は保存ボタンが押されたときにのみターミナルにする必要があります。

私の2番目の質問は、私はいつもテキストフィールドを読むべきですか?設定は通常変更するべきではないので、より効率的な方法がありますか? ?(私はこのような何かをしようとしていた保存ボタンのそれ以外の場合は、前の値を採用し、

を保存ボタンをクリックしただけでテキストフィールドを読んで、しかし、私は仕事をdoesntの:

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

第2の質問では、設定は本質的に動的なので、毎回読む必要があります。

+0

うわーありがとう私は家に帰るときにそれを試してみよう! – MilloMille

+0

@MilloMille回答をアップしてください。 –

関連する問題