Qt 5.10 Book Examples
DocWindow.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // DocWindow.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include "DocWindow.h"
13 
14 // ----------------------------------------------------------------------
15 DocWindow::DocWindow(QWidget* pwgt/*=0*/) : QTextEdit(pwgt)
16 {
17 }
18 
19 // ----------------------------------------------------------------------
21 {
22  QString str = QFileDialog::getOpenFileName();
23  if (str.isEmpty()) {
24  return;
25  }
26 
27  QFile file(str);
28  if (file.open(QIODevice::ReadOnly)) {
29  QTextStream stream(&file);
30  setPlainText(stream.readAll());
31  file.close();
32 
33  m_strFileName = str;
34  emit changeWindowTitle(m_strFileName);
35  }
36 }
37 
38 // ----------------------------------------------------------------------
40 {
41  if (m_strFileName.isEmpty()) {
42  slotSaveAs();
43  return;
44  }
45 
46  QFile file(m_strFileName);
47  if (file.open(QIODevice::WriteOnly)) {
48  QTextStream(&file) << toPlainText();
49  file.close();
50  emit changeWindowTitle(m_strFileName);
51  }
52 }
53 
54 // ----------------------------------------------------------------------
56 {
57  QString str = QFileDialog::getSaveFileName(0, m_strFileName);
58  if (!str.isEmpty()) {
59  m_strFileName = str;
60  slotSave();
61  }
62 }
void slotLoad()
Definition: DocWindow.cpp:20
void slotSaveAs()
Definition: DocWindow.cpp:55
void slotSave()
Definition: DocWindow.cpp:39
void changeWindowTitle(const QString &)
DocWindow(QWidget *pwgt=0)
Definition: DocWindow.cpp:15