큰 빨간 버튼을 사용하여 언제든지 종료 할 수 있도록 10 개의 프로그램 소스를 수정해야합니다. 나중에 다시 시작할 때 중단했던 동일한 위치에서 작업 할 수 있습니다. 기존의 모든 변수를 가져 오는 방법이 있습니까?기존 변수를 모두 가져 와서 Qt의 QSettings에 저장할 수 있습니까?
답변
당신이 필요로하는 마술 지팡이가 없습니다. 특히, 프로그램이 다시로드되면 이전 실행에서 모든 포인터가 유효하지 않게됩니다. 그리고 프로그램 자체가 아마도 다른 주소에로드되기 때문에 메모리 덤프를 저장하여이 문제를 해결할 수는 없습니다. 모든 관련 데이터 구조를 명시 적으로 저장하고 복원하는 작업을해야합니다.
변수를 직렬화 한 다음 추가 기능을 추가하여 파일 존재 여부를 확인하고 다시로드 할 수 있습니다. QDataStream 클래스와이 기능이 제공하는 기능에 대해 살펴보아야합니다 .
원본 포스터의 필요에 따라 QSettings로 필요한 작업을 수행 할 수 있으므로 QDataStream 및 QFile을 처리 할 필요가 없습니다. QSettings는 QRect 등 다양한 Qt 유형뿐만 아니라 다양한 기본 데이터 유형을 처리 할 수 있습니다. – Xenakios
다른 답변에서 설명한 것처럼 C++에서는 프로그램의 상태/변수 직렬화 및 비 직렬화를 포괄적으로 처리 할 수있는 메커니즘이 없습니다. 모든 방법은 그러한 기능을 명시 적으로 다루는 수기 코드를 필요로합니다.
예를 들어 QSet에 관해서는 QMainWindow에 2 가지 메소드를 추가 할 수 있습니다 (프로그램의 필요한 상태에 액세스 할 수있는 클래스가 무엇이든간에 사용하고 있다고 가정 할 때). 상태를 저장하고 복원합니다. QSettings 개체를 사용합니다. 같은
뭔가 다음 restoreGeometry 잘 작동하는 위젯이 구축 된 후, loadStateFromSettings() 호출이되어야한다고
void MainWindow::saveStateToSettings() // QMainWindow already has method saveState() to store dockwidgets and toolbars locations and visibility, so don't use that function name for this
{
QSettings settings; // the QSettings default constructor can be made to have default parameters like shown in the main() function code below
settings.setValue("mywindowgeometry",saveGeometry()); // QWidget::saveGeometry is the recommended way to serialize the position, size and monitor number etc of QWidget
settings.setValue("myvariable1",m_myVariable1); // m_myVariable1 could be any of various basic C++ or Qt value datatypes, like int, float, QString, QRect, QByteArray etc, let's assume here it is a double floating point number. DON'T store pointers using this, serializing pointers is almost always useless and/or dangerous
settings.setValue("checkbox1checked",ui->checkBox->isChecked()); // store a bool
settings.setValue("plaintextedit1text",ui->plainTextEdit->toPlainText()); // store a QString
// write similar code as above to save all other needed state
// that's all there is to it to save the state!
}
void MainWindow::loadStateFromSettings()
{
QSettings settings;
restoreGeometry(settings.value("mywindowgeometry").toByteArray()); // QWidget::restoreGeometry restores the widget geometry from data that was generated previously with QWidget::saveGeometry
m_myVariable1=settings.value("myvariable1",0.5).toDouble(); // the 0.5 sets a default value if the QSettings instance is missing the variable or there's some other problem with the QSettings instance
ui->checkBox->setChecked(settings.value("checkbox1checked",true).toBool()); // again, the "true" value will be used in case of problem with QSettings
ui->plainTextEdit->setPlainText(settings.value("plaintextedit1text").toString()); // no value as the default value "magically" gives an empty QString
}
주, 위젯 클래스 생성자 자체를 호출하지 않습니다. 코드를 컴파일하고 직접 작동하지 않을 수있다, 그래서 내가 주로 메모리에서이 입력 한
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSettings::setDefaultFormat(QSettings::IniFormat); // now QSettings default constructor anywhere in the program code creates a QSettings object that uses ini-files. Note that on Windows you could use the registry and on Mac plist-files. Read the QSettings documentation for more on this
QApplication::setApplicationName("MyApplication"); // you should set this for your app object so QSettings can store the settings for your app in a location that can be identified by that name
QApplication::setOrganizationName("MyName"); // you should set this for your app object, the organization name is effectively your "company" name, and it makes QSettings store the settings for your app(s) in a location that can be identified by that name
MainWindow w;
w.loadStateFromSettings();
w.show();
return a.exec();
}
, 그러나 희망 그것은 당신에게 그것에 대해 이동하는 방법에 대한 아이디어를 제공합니다 : 귀하의 main() 함수는 뭔가처럼 될 수있다 .
- 해결책은 QSettings
- 을 사용하는 것입니다 Sessions
- 또 다른 솔루션을 사용하는 것입니다 그리고 세 번째 솔루션은 Serialization
- 1. Dongle에 저장된 라이센스 정보를 파일에서 가져 와서 저장할 수 있습니까?
- 2. DataGridView 행 값을 가져 와서 변수에 저장할 수 있습니까?
- 3. 변수를 가져 와서 MKAnnotationView에 연결하십시오.
- 4. mysql에서 모든 값을 가져 와서 변수를 만듭니다.
- 5. JNI Java Env 변수를 저장할 수 있습니까?
- 6. PHP onclick이 텍스트를 가져 와서 변수를 만듭니다.
- 7. 자바 스크립트 변수를 모두 가져 옵니까?
- 8. JS/JQuery에서는 다차원 배열에서 단일 요소를 가져 와서 다른 배열에 저장할 수 있습니까?
- 9. 다른 테이블에서 다른 값을 가져 와서 다른 테이블에 저장할 수 있습니까?
- 10. 어떻게 Excel 파일을 가져 와서 내용을 파싱하고 데이터베이스 열에 Excel 시트의 열 데이터를 저장할 수 있습니까?
- 11. 여러 테이블에서 데이터를 가져 와서 시간순으로 모두 정렬
- 12. 여러 객체 속성이있는 해시. 어떻게 모두 저장할 수 있습니까?
- 13. Photoshop PSD를 Expression Blend로 가져 와서 기존 레이아웃 구조를 채울 수 있습니까?
- 14. 기존 Java 프로젝트를 Eclipse로 가져 와서 어떻게 실행합니까?
- 15. GET 변수를 가져 와서 아약스 jquery를 통해 보내야합니다.
- 16. 웹 페이지를 가져 와서 데이터베이스에 저장 하시겠습니까?
- 17. iPhone SDK : 하나의보기에서 인스턴스 변수를 가져 와서 다른보기에 액세스 할 수 있습니까?
- 18. 어떻게 bash를 사용하여 파일에 변수를 저장할 수 있습니까?
- 19. 스테이지의 현재보기를 가져 와서 정보를 내보낼 수 있습니까? 이러한 옵션의
- 20. Javascript 라이브러리를 가져 와서 Flash로 실행할 수 있습니까?
- 21. Cakephp - 내가 모델을 가져 와서 app_helper에서 쿼리를 쓸 수 있습니까?
- 22. 테이블 너비를 자동 너비로 가져 와서 화면을 채울 수 있습니까?
- 23. PHP는 수은 저장소에서 코드를 가져 와서 풀어 낼 수 있습니까?
- 24. 나는 datepicker에서 날짜를 가져 와서 다시 다시 넣을 수 있습니까?
- 25. 어떻게 listview에서 이미지를 가져 와서 picturebox에 할당 할 수 있습니까?
- 26. 부모로부터 자식으로 변수를 가져 와서 동적 텍스트 필드에 삽입
- 27. URL에서 모든 매개 변수를 가져 와서 PHP로 인쇄하려면 어떻게해야합니까?
- 28. Observer를 CoreData 객체에 안전하게 저장할 수 있습니까?
- 29. 정보를 다른 테이블에 저장할 때 정보를 데이터베이스에 저장할 수 있습니까?
- 30. Qt의 커밋을 어디에서 찾을 수 있습니까?
내가 내 자신의 개체에는 직렬화 할 수 사용하는 것입니다? – user907406