"Pyqt db 연결"의 두 판 사이의 차이
DB CAFE
(새 문서: import os from PyQt5 import QtCore, QtGui, QtWidgets, QtSql class BlobDelegate(QtWidgets.QStyledItemDelegate): def displayText(self, value, locale): if isinstance(value,...) |
(차이 없음)
|
2020년 3월 26일 (목) 02:52 판
thumb_up 추천메뉴 바로가기
- DBA { Oracle DBA 명령어 > DBA 초급 과정 > DBA 고급 과정 }
- 튜닝 { 오라클 튜닝 목록 }
- 모델링 { 데이터 모델링 가이드 }
import os from PyQt5 import QtCore, QtGui, QtWidgets, QtSql
class BlobDelegate(QtWidgets.QStyledItemDelegate):
def displayText(self, value, locale): if isinstance(value, QtCore.QByteArray): value = value.data().decode() return super(BlobDelegate, self).displayText(value, locale)
def createConnection():
db = QtSql.QSqlDatabase.addDatabase("QSQLITE") file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "db.db") db.setDatabaseName(file) if not db.open(): QtWidgets.QMessageBox.critical( None, QtWidgets.qApp.tr("Cannot open database"), QtWidgets.qApp.tr( "Unable to establish a database connection.\n" "This example needs SQLite support. Please read " "the Qt SQL driver documentation for information " "how to build it.\n\n" "Click Cancel to exit." ), QtWidgets.QMessageBox.Cancel, ) return False return True
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
if not createConnection(): sys.exit(-1)
w = QtWidgets.QTableView() w.horizontalHeader().setStretchLastSection(True) w.setWordWrap(True) w.setTextElideMode(QtCore.Qt.ElideLeft) delegate = BlobDelegate(w) w.setItemDelegateForColumn(4, delegate) model = QtSql.QSqlQueryModel() model.setQuery("SELECT * FROM tblEvents") w.setModel(model) w.resize(640, 480) w.show()
sys.exit(app.exec_())