11๊ฐ. ๋ฌธ์ ํฌ๋ฉง

1. ๋ชฉํ
๋๋ณด๊ธฐ
- ๋ฉ๋ด Format ์ Bold, Italic, Underline ์ก์
์ ์ด์ฉํด์
์ ํํ ๋ฌธ์ ๋๋ ์ปค์ ์์น ์ดํ์ ์ ๋ ฅ๋๋ ๋ฌธ์์
๊ตต๊ฒ, ๊ธฐ์ธ์, ๋ฐ์ค ์คํ์ผ์ ์ ์ฉํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ค. - QTextEdit ์ ์ฒด์ ํฐํธ๋ฅผ ๋ฐ๊พธ๋ ๋ฐฉ์์ด ์๋๋ผ
QTextCharFormat ๊ณผ QFont ๋ฅผ ์ฌ์ฉํ์ฌ
์ ํ๋ ๋ฌธ์ ์์ญ์๋ง ์์์ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ดํดํ๋ค. - ํ์ฌ ์ํ๋ฅผ ํ ๊ธ ํํ๋ก ๊ตฌํํด์
Bold ๋ฒํผ์ ํ ๋ฒ ๋๋ฅด๋ฉด ๊ตต๊ฒ, ํ ๋ฒ ๋ ๋๋ฅด๋ฉด ์๋ ๋๊ป๋ก ๋์๊ฐ๋
์ฌ์ฉ์ฑ ์ข์ ํธ์ง ๊ธฐ๋ฅ์ ๊ตฌํํ๋ค. - Qt Designer ์์ ๋ง๋ QAction(actionBold, actionItalic, actionUnderline)์
๋ฉ์ธ ์๋์ฐ ์ฌ๋กฏ ๋ฉ์๋์ ์ฐ๊ฒฐํ๋ ํจํด์ ์ตํ๋ค. - ๋ฌธ์ ๋ด์ฉ์ QTextEdit ๋ด๋ถ์ QTextDocument ์์์๋ง ๋ค๋ฃน๋๋ค
2. ์ ์ฒด ๋ก์ง
๋๋ณด๊ธฐ
# mainwindow.py
from PySide6.QtGui import QFont, QTextCharFormat
. . .
class MainWindow(QMainWindow):
def __init__(self, parent=None):
. . .
self.ui.actionBold.triggered.connect(self.text_bold)
self.ui.actionItalic.triggered.connect(self.text_italic)
self.ui.actionUnderline.triggered.connect(self.text_underline)
. . .
def _merge_format_on_selection(self, fmt: QTextCharFormat):
cursor = self.editor.textCursor()
if cursor.hasSelection():
cursor.mergeCharFormat(fmt)
else:
self.editor.mergeCurrentCharFormat(fmt)
def text_bold(self):
cursor = self.editor.textCursor()
fmt = cursor.charFormat()
is_bold = fmt.fontWeight() > QFont.Normal
fmt.setFontWeight(QFont.Normal if is_bold else QFont.Bold)
self._merge_format_on_selection(fmt)
def text_italic(self):
cursor = self.editor.textCursor()
fmt = cursor.charFormat()
current_italic = fmt.fontItalic()
fmt.setFontItalic(not current_italic)
self._merge_format_on_selection(fmt)
def text_underline(self):
cursor = self.editor.textCursor()
fmt = cursor.charFormat()
current_underline = fmt.fontUnderline()
fmt.setFontUnderline(not current_underline)
self._merge_format_on_selection(fmt)
# 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 + ์ฃผ์ ์ค๋ช
๋๋ณด๊ธฐ
from PySide6.QtGui import QFont, QTextCharFormat
# QFont
# ๊ธ๊ผด ์ด๋ฆ, ํฌ๊ธฐ, ๊ตต๊ธฐ, ๊ธฐ์ธ์ ๋ฑ ํฐํธ ์ ๋ณด๋ฅผ ํํํ๋ ํด๋์ค
# ์ฌ๊ธฐ์๋ ๊ตต๊ฒ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ณ , ๊ตต๊ฒ ๋๋ ์ผ๋ฐ ๋๊ป๋ก ์ค์ ํ ๋ ์ฌ์ฉํ๋ค
#
# QTextCharFormat
# ๊ฐ๋ณ ๋ฌธ์์ ๋ํ ์์ ์ ๋ณด๋ฅผ ํํํ๋ ํด๋์ค
# ๊ตต๊ฒ, ๊ธฐ์ธ์, ๋ฐ์ค, ๊ธ์์ ๋ฑ ์คํ์ผ์ ๋ด๊ณ
# ์ ํ๋ ํ
์คํธ์ ๋ณํฉ ์ ์ฉํ ์ ์๋ค
4. ๋ฉ๋ด ์ก์
๊ณผ ์ฌ๋กฏ ์ฐ๊ฒฐํ๊ธฐ
๋๋ณด๊ธฐ
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# ์ค์ ํธ์ง๊ธฐ QTextEdit ์ ์์ฃผ ์ ๊ทผํ๋ฏ๋ก ๋ณ์นญ์ ๋ง๋ค์ด ๋๋ค
self.editor = self.ui.textEdit
# 9.10 ๋ฌธ์ ์คํ์ผ ํธ์ง ๊ธฐ๋ฅ ๊ตฌํ
# Format ๋ฉ๋ด์ Bold, Italic, Underline ์ก์
์
# MainWindow ์ ์ฌ๋กฏ ๋ฉ์๋์ ์ฐ๊ฒฐํ๋ค
# Bold ๋ฉ๋ด์ ํด๋ฐ ์์ด์ฝ์ text_bold ํจ์์ ์ฐ๊ฒฐ
self.ui.actionBold.triggered.connect(self.text_bold)
# Italic ๋ฉ๋ด์ ํด๋ฐ ์์ด์ฝ์ text_italic ํจ์์ ์ฐ๊ฒฐ
self.ui.actionItalic.triggered.connect(self.text_italic)
# Underline ๋ฉ๋ด์ ํด๋ฐ ์์ด์ฝ์ text_underline ํจ์์ ์ฐ๊ฒฐ
self.ui.actionUnderline.triggered.connect(self.text_underline)
- ๋ฉ๋ด Format ์์ Bold, Italic, Underline ์ ํด๋ฆญํ์ ๋
- ํด๋ฐ์ ๊ตต๊ฒ/๊ธฐ์ธ์/๋ฐ์ค ๋ฒํผ์ ๋๋ ์ ๋
- ๋จ์ถํค Ctrl+B, Ctrl+I, Ctrl+E ๋ฅผ ๋๋ ์ ๋
5. ๋ฌธ์ ํฌ๋ฉง ๊ธฐ๋ฅ ๊ตฌํ ํจ์
๋๋ณด๊ธฐ
5-1. ์ ํ ์์ญ์ ์์ ๋ณํฉ์ ๋์์ฃผ๋ ํฌํผ ๋ฉ์๋
def _merge_format_on_selection(self, fmt: QTextCharFormat):
# ํ์ฌ QTextEdit ์ ํ
์คํธ ์ปค์๋ฅผ ๊ฐ์ ธ์จ๋ค
cursor = self.editor.textCursor()
# ์ ํ๋ ํ
์คํธ๊ฐ ์์ ๊ฒฝ์ฐ
if cursor.hasSelection():
# ์ ํ๋ ๋ฌธ์๋ค์ ๋ฌธ์ ์์์ ๋ณํฉ ์ ์ฉํ๋ค
# ์ด๋ฏธ ์กด์ฌํ๋ ์์ ์์ ๋ณ๊ฒฝ๋ ๋ถ๋ถ๋ง ๋ฎ์ด์ด๋ค
cursor.mergeCharFormat(fmt)
else:
# ์ ํ ์์ญ์ด ์์ผ๋ฉด
# ์ดํ์ ์
๋ ฅ๋ ํ
์คํธ์ ๊ธฐ๋ณธ ๋ฌธ์ ์์์ ๋ณ๊ฒฝํ๋ค
self.editor.mergeCurrentCharFormat(fmt)
์ด ํจ์ ๋๋ถ์
- ์ ํ์ด ์์ ๋๋ ๊ทธ ๋ถ๋ถ์๋ง ์คํ์ผ์ ์ ์ฉํ๊ณ
- ์ ํ์ด ์์ ๋๋ ์ปค์ ์ดํ์ ์ ๋ ฅ๋๋ ๋ฌธ์์ ์คํ์ผ์ ์ ์ฉ
5-2. ๊ตต๊ฒ ํ ๊ธ ๊ธฐ๋ฅ
def text_bold(self):
# ํ์ฌ ํ
์คํธ ์ปค์๋ฅผ ๊ฐ์ ธ์จ๋ค
cursor = self.editor.textCursor()
# ํ์ฌ ์ปค์ ์์น์ ๋ฌธ์ ์์์ ๊ฐ์ ธ์จ๋ค
fmt = cursor.charFormat()
# ํ์ฌ ๊ธ์๊ฐ ๊ตต์ ์ํ์ธ์ง ํ์ธํ๋ค
# QFont.Normal ๋ณด๋ค ํฌ๋ฉด ๊ตต๊ฒ๋ผ๊ณ ํ๋จํ ์ ์๋ค
is_bold = fmt.fontWeight() > QFont.Normal
# ์ด๋ฏธ ๊ตต๊ฒ๋ผ๋ฉด ์ผ๋ฐ ๋๊ป๋ก,
# ์ผ๋ฐ ๋๊ป๋ผ๋ฉด ๊ตต๊ฒ๋ก ํ ๊ธํ๋ค
fmt.setFontWeight(QFont.Normal if is_bold else QFont.Bold)
# ์ ํ๋ ์์ญ ๋๋ ์ดํ ์
๋ ฅ๋ ๋ฌธ์์ ์์์ ๋ณํฉ ์ ์ฉํ๋ค
self._merge_format_on_selection(fmt)
- ๋ฒํผ์ ๋๋ฅผ ๋๋ง๋ค ํ์ฌ ์ํ๋ฅผ ์ฝ์ด์ ๋ฐ๋๋ก ๋ฐ๊พธ๋ ํ ๊ธ ๋ฐฉ์์ด๋ค.
- ์ด๋ ๊ฒ ํ๋ฉด ์ฌ์ฉ์๊ฐ Bold ๋ฒํผ์ ํ ๋ฒ ๋ ๋๋ฌ ๊ตต๊ฒ ์ํ๋ฅผ ํด์ ํ ์ ์๋ค.
5-3. ๊ธฐ์ธ์ ํ ๊ธ ๊ธฐ๋ฅ
def text_italic(self):
# ํ์ฌ ํ
์คํธ ์ปค์๋ฅผ ๊ฐ์ ธ์จ๋ค
cursor = self.editor.textCursor()
# ํ์ฌ ๋ฌธ์ ์์์ ๊ฐ์ ธ์จ๋ค
fmt = cursor.charFormat()
# ํ์ฌ ๊ธฐ์ธ์ ์ํ๋ฅผ ์ฝ์ด ์จ๋ค
current_italic = fmt.fontItalic()
# True ๋ False ๋ก, False ๋ True ๋ก ๋ฐ๋๋ก ํ ๊ธํ๋ค
fmt.setFontItalic(not current_italic)
# ์ ํ ์์ญ ๋๋ ์ดํ ์
๋ ฅ๋ ๋ฌธ์์ ์์์ ๋ณํฉ ์ ์ฉํ๋ค
self._merge_format_on_selection(fmt)
- ๋ฌธ์ ์์ ๊ฐ์ฒด์์ ๊ธฐ์ธ์ ์ฌ๋ถ๋ฅผ ์ฝ์ด์ค๊ณ
- not ์ฐ์ฐ์ผ๋ก ๋ฐ์ ์ํค๋ ์ ํ์ ์ธ ํ ๊ธ ํจํด์ ์ฌ์ฉํ๋ค.
5-4. ๋ฐ์ค ํ ๊ธ ๊ธฐ๋ฅ
def text_underline(self):
# ํ์ฌ ํ
์คํธ ์ปค์๋ฅผ ๊ฐ์ ธ์จ๋ค
cursor = self.editor.textCursor()
# ํ์ฌ ๋ฌธ์ ์์์ ๊ฐ์ ธ์จ๋ค
fmt = cursor.charFormat()
# ํ์ฌ ๋ฐ์ค ์ํ๋ฅผ ์ฝ์ด ์จ๋ค
current_underline = fmt.fontUnderline()
# ๋ฐ์ค ์ํ๋ฅผ ๋ฐ๋๋ก ํ ๊ธํ๋ค
fmt.setFontUnderline(not current_underline)
# ์ ํ ์์ญ ๋๋ ์ดํ ์
๋ ฅ๋ ๋ฌธ์์ ์์์ ๋ณํฉ ์ ์ฉํ๋ค
self._merge_format_on_selection(fmt)
- ๊ธฐ์ธ์๊ณผ ๋์ผํ ํจํด์ผ๋ก ๋ฐ์ค ์ฌ๋ถ๋ฅผ ํ ๊ธํ๋ค.
- QTextCharFormat ์ด ๋ฐ์ค, ์ทจ์์ ๋ฑ ๋ค์ํ ํจ๊ณผ๋ฅผ ๋ชจ๋ ๋ด๋นํ๋ค๋ ์ ์ ๋ณด์ฌ์ค๋ค.
6. ์คํ ํ ์คํธ
7. ํ์ต ์ฃผ์ ํฌ์ธํธ
๋๋ณด๊ธฐ
QTextEdit ์ ์ฒด ํฐํธ ๋ณ๊ฒฝ๊ณผ ๋ฌธ์ ์์ ๋ณ๊ฒฝ์ ์ฐจ์ด
- setFont ๋ฅผ ์ฌ์ฉํ๋ฉด ํธ์ง๊ธฐ ์ ์ฒด ๊ธฐ๋ณธ ํฐํธ๊ฐ ๋ณ๊ฒฝ๋๋ค.
- QTextCharFormat ๊ณผ mergeCharFormat ์ ์ฌ์ฉํ๋ฉด ์ ํ๋ ๋ฌธ์์๋ง ์์์ ์ ์ฉํ ์ ์๋ค.
- ์๋ ํ๋ก์ธ์์ ๊ฐ์ ๋ฆฌ์น ํ ์คํธ ํธ์ง ๊ธฐ๋ฅ์ ๊ตฌํํ ๋๋ ๋ฌธ์ ๋จ์ ์์์ด ํ์์ ์ด๋ค.
ํ ๊ธ ๋ฐฉ์์ผ๋ก ์ํ๋ฅผ ์ ์ดํ๋ ํจํด
- ํ์ฌ ์ํ๋ฅผ ์ฝ์ด์ ๋ฐ๋๋ก ์ค์ ํ๋ ํ ๊ธ ํจํด์
Bold, Italic, Underline ์ด ๋ชจ๋ ๋์ผํ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ค. - ํ์๋ค์๊ฒ ์ํ ๊ธฐ๋ฐ UI ๊ตฌํ์ ์ ํ์ ์ธ ์์ ๋ก ์ค๋ช ํ๊ธฐ ์ข๋ค.
์ปค์์ ์ ํ ์์ญ์ ์ญํ
- cursor.hasSelection ์ ํตํด
์ ํ์ด ์๋์ง ์๋์ง์ ๋ฐ๋ผ ๋์์ ๋ค๋ฅด๊ฒ ํ ์ ์๋ค. - ์ ํ์ด ์์ผ๋ฉด ๊ทธ ์์ญ์๋ง ์คํ์ผ์ ์ ์ฉํ๊ณ
์์ผ๋ฉด ์ดํ ์ ๋ ฅ๋ ๋ฌธ์์ ์คํ์ผ์ ์ ์ฉํ๋ UX๋ ๋๋ถ๋ถ์ ํ ์คํธ ํธ์ง๊ธฐ์ ๋์ผํ ๋์์ด๋ค.
QAction ๊ณผ ์ฌ๋กฏ ์ฐ๊ฒฐ ๊ตฌ์กฐ
- ui_form.py ์์ QAction ๋ค์ด ์ด๋ฏธ ์์ฑ๋์ด ์๊ณ
๋ฉ์ธ ์ฝ๋์์๋ ๊ทธ ์ก์ ์ triggered ์๊ทธ๋๋ง ์ฌ๋กฏ ๋ฉ์๋์ ์ฐ๊ฒฐํ๋ฉด ๋๋ค. - ์ด ๋๋ถ์ ๋ฉ๋ด, ํด๋ฐ, ๋จ์ถํค๊ฐ ๋ชจ๋ ๊ฐ์ ๊ธฐ๋ฅ์ ๊ณต์ ํ๊ฒ ๋๋ค.
ํ์ผ ์ ์ถ๋ ฅ๊ณผ์ ๋ถ๋ฆฌ
- ์ด ๋จ๊ณ๋ ์ด๋๊น์ง๋ ๋ฌธ์ ๋ด์ฉ์ ์คํ์ผ ํธ์ง๋ง ๋ค๋ฃจ๊ณ ๋์คํฌ ํ์ผ ์ ์ฅ๊ณผ๋ ๋ถ๋ฆฌ๋์ด ์๋ค.
- ํ์ผ ์ ์ฅ๊ณผ ์ฝ๊ธฐ๋ ๋ค๋ฅธ ๋จ๊ณ์์ QFile, QTextStream ์ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก ๋ฌถ๊ณ
์คํ์ผ ํธ์ง์ ๋ฉ๋ชจ๋ฆฌ ์์ QTextDocument ๋ง ๋ค๋ฃจ๋ ๊ตฌ์กฐ๊ฐ ์ค๊ณ ์ธก๋ฉด์์ ๊น๋ํ๋ค.
๋จ๊ณ๋ณ ์์ฑ ํ์ผ
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
