4๊ฐ. ์ ํ์ผ ์ด๊ธฐ

1. ๋ชฉํ
์ ํ์ผ ๋ฉ๋ด๋ฅผ ๋๋ ์ ๋
- ํ์ฌ ๋ฌธ์๊ฐ ์์ ๋์ด ์๋ค๋ฉด ๋จผ์ ์ ์ฅ ์ฌ๋ถ๋ฅผ ๋ฌป๊ณ
- ์ทจ์๋ฅผ ๋๋ฅด๋ฉด ์๋ฌด ์ผ๋ ํ์ง ์๊ณ
- ๊ณ์ ์งํ์ ์ ํํ๋ฉด ์๋ํฐ๋ฅผ ๊นจ๋ํ ์ ๋ฌธ์ ์ํ๋ก ์ด๊ธฐํํ๋ค.
์ ์ฅ ๊ธฐ๋ฅ์ ์ด์ ๋จ๊ณ์์ ๊ตฌํํ Qt ์คํ์ผ ํ์ผ ์ ์ฅ ํจ์์
(QFile, QIODevice, QDir, QTextStream ๊ธฐ๋ฐ)์ธ save_file ์ ์ฌ์ฌ์ฉํ๋ค.
2. ์ ์ฒด ๋ก์ง
# mainwindow.py
from PySide6.QtWidgets import QMainWindow, QFileDialog, QMessageBox
from PySide6.QtCore import QFile, QIODevice, QDir, QTextStream
from ui_form import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.current_file = ''
self.ui.actionNew.triggered.connect(self.file_new)
self.ui.actionSave.triggered.connect(self.save_file)
def file_new(self):
if not self.maybe_save():
return
self.ui.textEdit.clear()
self.current_file = ''
self.ui.textEdit.document().setModified(False)
self.setWindowTitle('์ ๋ชฉ ์์ - ๋ฉ๋ชจ์ฅ')
def maybe_save(self) -> bool:
if not self.ui.textEdit.document().isModified():
return True
ret = QMessageBox.warning(
self,
'๋ฌธ์ ์ ์ฅ',
'๋ฌธ์๊ฐ ์์ ๋์์ต๋๋ค.\n๋ณ๊ฒฝ ๋ด์ฉ์ ์ ์ฅํ์๊ฒ ์ต๋๊น',
QMessageBox.StandardButton.Save
| QMessageBox.StandardButton.Discard
| QMessageBox.StandardButton.Cancel
)
if ret == QMessageBox.StandardButton.Save:
return self.save_file()
if ret == QMessageBox.StandardButton.Cancel:
return False
return True
def save_file(self) -> bool:
filename, _ = QFileDialog.getSaveFileName(
self,
'ํ์ผ ์ ์ฅ',
QDir.homePath(),
'Text Files (*.txt);;All Files (*)'
)
if not filename:
return False
file = QFile(filename)
if not file.open(QIODevice.WriteOnly | QIODevice.Text):
QMessageBox.critical(self, '์ค๋ฅ', f'ํ์ผ์ ์ด ์ ์์ต๋๋ค:\n{file.errorString()}')
return False
stream = QTextStream(file)
stream << self.ui.textEdit.toPlainText()
file.close()
self.current_file = filename
QMessageBox.information(self, '์ ์ฅ ์ฑ๊ณต', f'ํ์ผ์ด ์ ์ฅ๋์์ต๋๋ค.\n\n{filename}')
return True
# main.py
import sys
from PySide6.QtWidgets import QApplication
from mainwindow import MainWindow
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
3. ํ์ํ import + ์ฃผ์ ์ค๋ช
๋์ผ
4. ๋ฉ๋ด ์ก์ ๊ณผ ์ฌ๋กฏ ์ฐ๊ฒฐํ๊ธฐ (actionNew ↔ file_new)

class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# ํ์ฌ ์ด๋ ค ์๋ ํ์ผ ๊ฒฝ๋ก๋ฅผ ๊ธฐ์ตํ๊ธฐ ์ํ ๋ฉค๋ฒ ๋ณ์
# ์ ๋ฌธ์ ์ํ์์๋ ๋น ๋ฌธ์์ด๋ก ๋๋ค
self.current_file = ''
# 9.3 ์์ ์ด๋ฏธ ๊ตฌํํ ํ์ผ ์ ์ฅ ๊ธฐ๋ฅ๊ณผ ์ฐ๊ฒฐ
# ์ ํ์ผ์ ๋ง๋ค ๋ ์์ ๋ด์ฉ ์ ์ฅ ์ฌ๋ถ๋ฅผ ๋ฌป๊ธฐ ์ํด save_file ์ ์ฌ์ฌ์ฉํ๋ค
self.ui.actionSave.triggered.connect(self.save_file)
# 9.4 ์ ํ์ผ ์ด๊ธฐ ๊ธฐ๋ฅ
# ๋ฉ๋ด์ ์ ํ์ผ ์ก์
์ file_new ๋ฉ์๋์ ์ฐ๊ฒฐํ๋ค
self.ui.actionNew.triggered.connect(self.file_new)
์ฌ๊ธฐ์ ์ค์ํ ์ ์
์ ํ์ผ ๋์์ด ๋จ๋
์ผ๋ก ์กด์ฌํ๋ ๊ฒ์ด ์๋๋ผ
๋ฐ๋์ save_file, maybe_save ์ ํจ๊ป ๋์๊ฐ๋ค๋ ์ ์ด๋ค.
5. ์ค์ ์ ํ์ผ ์ด๊ธฐ ๊ธฐ๋ฅ ๊ตฌํ ํจ์
5.1 ์ ํ์ผ ์ฒ๋ฆฌ ํจ์ file_new

def file_new(self):
# ์ ํ์ผ์ ๋ง๋ค๊ธฐ ์ ์
# ํ์ฌ ๋ฌธ์์ ์ ์ฅ๋์ง ์์ ๋ณ๊ฒฝ ์ฌํญ์ด ์๋ค๋ฉด
# ๋จผ์ ์ ์ฅ ์ฌ๋ถ๋ฅผ ๋ฌผ์ด์ผ ํ๋ค
if not self.maybe_save():
# maybe_save ๊ฐ False ๋ฅผ ๋ฐํํ๋ฉด
# ์ฌ์ฉ์๊ฐ ์ ์ฅ ์ทจ์๋ฅผ ์ ํํ ๊ฒ์ด๋ฏ๋ก
# ์ ํ์ผ ์์ฑ๋ ์ทจ์ํ๋ค
return
# ์ฌ๊ธฐ๊น์ง ์๋ค๋ฉด
# 1) ๋ณ๊ฒฝ ์ฌํญ์ด ์์๊ฑฐ๋
# 2) ๋ณ๊ฒฝ ์ฌํญ์ ์ด๋ฏธ ์ ์ฅํ๊ฑฐ๋
# 3) ๋ณ๊ฒฝ ์ฌํญ์ ๋ฒ๋ฆฌ๊ฒ ๋ค๊ณ ์ ํํ ์ํ์ด๋ค
# ์๋ํฐ ๋ด์ฉ์ ์ง์์ ์์ ํ ์ ๋ฌธ์ ์ํ๋ก ๋ง๋ ๋ค
self.ui.textEdit.clear()
# ํ์ฌ ํ์ผ ๊ฒฝ๋ก๋ ์ด๊ธฐํํ๋ค
# ๋น ๋ฌธ์์ด์ ์์ง ๋์คํฌ์ ์ ์ฅ๋์ง ์์ ์ ๋ฌธ์๋ผ๋ ์๋ฏธ๋ก ์ฌ์ฉํ๋ค
self.current_file = ''
# ์์ ์ํ ํ๋๊ทธ๋ฅผ ์ด๊ธฐํํ๋ค
self.ui.textEdit.document().setModified(False)
# ์ฐฝ ์ ๋ชฉ์ ์ ๋ฌธ์์์ ์ ์ ์๋๋ก ๋ณ๊ฒฝํ๋ค
self.setWindowTitle('์ ๋ชฉ ์์ - ๋ฉ๋ชจ์ฅ')
5.2 ์ ์ฅ ์ฌ๋ถ๋ฅผ ๋ฌป๋ ํจ์ maybe_save

def maybe_save(self) -> bool:
# ๋ฌธ์๊ฐ ์์ ๋์ง ์์๋ค๋ฉด
# ๋ฐ๋ก ๊ณ์ ์งํํด๋ ๋๋ฏ๋ก True ๋ฅผ ๋ฐํํ๋ค
if not self.ui.textEdit.document().isModified():
return True
# ์ฌ๊ธฐ๊น์ง ์๋ค๋ ๊ฒ์
# textEdit ๋ด์ฉ์ด ๋ง์ง๋ง ์ ์ฅ ์ดํ๋ก ๋ณ๊ฒฝ๋์๋ค๋ ์๋ฏธ์ด๋ค
# ์ฌ์ฉ์์๊ฒ ๋ณ๊ฒฝ ๋ด์ฉ์ ์ ์ฅํ ์ง ๋ฌผ์ด๋ณธ๋ค
ret = QMessageBox.warning(
self,
'๋ฌธ์ ์ ์ฅ',
'๋ฌธ์๊ฐ ์์ ๋์์ต๋๋ค.\n๋ณ๊ฒฝ ๋ด์ฉ์ ์ ์ฅํ์๊ฒ ์ต๋๊น',
QMessageBox.StandardButton.Save
| QMessageBox.StandardButton.Discard
| QMessageBox.StandardButton.Cancel
)
# ์ ์ฅ ๋ฒํผ์ ๋๋ ๋ค๋ฉด
# save_file ์ ํธ์ถํด์ ์ค์ ๋ก ์ ์ฅ์ ์๋ํ๋ค
# save_file ์ด True ๋ฅผ ๋ฐํํ๋ฉด ๊ณ์ ์งํ ๊ฐ๋ฅ
if ret == QMessageBox.StandardButton.Save:
return self.save_file()
# ์ทจ์ ๋ฒํผ์ ๋๋ ๋ค๋ฉด
# ํธ์ถํ ์ชฝ์์ ์์
์ ์ค๋จํด์ผ ํ๋ฏ๋ก False ๋ฅผ ๋ฐํํ๋ค
if ret == QMessageBox.StandardButton.Cancel:
return False
# ๋๋จธ์ง ๊ฒฝ์ฐ๋ ๋ฒ๋ฆฌ๊ธฐ ๋ฒํผ์ ๋๋ฅธ ๊ฒฝ์ฐ์ด๋ฏ๋ก
# ์ ์ฅํ์ง ์๊ณ ๊ณ์ ์งํํด๋ ๋๋ค
return True
6. ์คํ
6. ์คํ ํ ์คํธ
7.ํ์ต ์ฃผ์ ํฌ์ธํธ
1 ์ ํ์ผ ๊ธฐ๋ฅ์ ํต์ฌ ๊ตฌ์กฐ
- ๋จ์ํ textEdit ๋ฅผ clear ํ๋ ๊ฒ์ด ์ ๋ถ๊ฐ ์๋๋ค.
- ๋ฐ๋์ ๋ค์ ์์๋ฅผ ๋ฐ๋ผ์ผ ์์ ํ๋ค.
1 ์์ ์ฌ๋ถ ํ์ธ maybe_save
2 ์ฌ์ฉ์๊ฐ ์ ์ฅ ๋๋ ๋ฒ๋ฆฌ๊ธฐ ๋ฅผ ์ ํํ๋ฉด ์ ๋ฌธ์๋ก ์ด๊ธฐํ
3 ์ทจ์๋ฅผ ์ ํํ๋ฉด ์๋ฌด ๊ฒ๋ ํ์ง ์์
2 ์์ ์ฌ๋ถ ๊ฒ์ฌ์ document modified ํ๋๊ทธ
- QTextDocument ์ isModified ๋ฅผ ์ฌ์ฉํ๋ฉด
์๋ํฐ๊ฐ ๋ง์ง๋ง ์ ์ฅ ์ดํ๋ก ๋ณ๊ฒฝ๋์๋์ง ์ฝ๊ฒ ํ์ธํ ์ ์๋ค. - ์ ๋ฌธ์๋ฅผ ๋ง๋ค๊ฑฐ๋ ์ ์ฅ์ ์๋ฃํ ๋ค์๋
setModified False ๋ก ์ํ๋ฅผ ์ด๊ธฐํํด์ผ ํ๋ค.
3 maybe_save ์ ๋ฐํ๊ฐ ์๋ฏธ
- True ๊ณ์ ์งํํด๋ ๊ด์ฐฎ๋ค.
์ ํ์ผ ๋ง๋ค๊ธฐ, ํ๋ก๊ทธ๋จ ์ข ๋ฃ ๋ฑ ๋ค์ ์์ ์ ์ํํ ์ ์๋ค. - False ์ฌ์ฉ์๊ฐ ์ทจ์๋ฅผ ๋๋ ์ผ๋ฏ๋ก
ํธ์ถํ ์ชฝ์์๋ ์์ ์ ์ค๋จํด์ผ ํ๋ค.
4 Qt ํ์ผ I O ์์ ์ฐ๊ณ
- ์ ํ์ผ ๊ธฐ๋ฅ ์์ฒด๋ ๋์คํฌ I O ๋ฅผ ์ง์ ์ฌ์ฉํ์ง ์์ง๋ง
๋ด๋ถ์ ์ผ๋ก save_file ์ ํธ์ถํ ์ ์์ผ๋ฏ๋ก
์ ์ฅ ๋ก์ง์ QFile, QIODevice, QTextStream ์ผ๋ก ๊ตฌํ๋์ด ์์ด์ผ
Qt ์ ๋ค๋ฅธ ๋ถ๋ถ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ฐ๊ฒฐ๋๋ค.
5 current_file ๋ฉค๋ฒ์ ์ญํ
- ํ์ฌ ์ด๋ ค ์๋ ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ์ ์ฅํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
- ์ ํ์ผ์ผ ๊ฒฝ์ฐ ๋น ๋ฌธ์์ด๋ก ๋์ด
์์ง ๋์คํฌ์ ์ ์ฅ๋์ง ์์ ๋ฌธ์๋ผ๋ ๊ฒ์ ํ์ํ ์ ์๋ค. - ์ดํ ๋จ๊ณ์์
์ด๋ฏธ ํ์ผ ์ด๋ฆ์ด ์๋ ๊ฒฝ์ฐ์๋ ๋ฐ๋ก ๋ฎ์ด์ฐ๊ธฐ ์ ์ฅ์ ํ๋ ๋ฑ
๋ ์งํ๋ ์ ์ฅ ๋ก์ง์ ๋ง๋ค ์ ์๋ค.
๋จ๊ณ๋ณ ์์ฑ ํ์ผ
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote





