행위

Pyqt db 연결

DB CAFE

Dbcafe (토론 | 기여)님의 2020년 3월 26일 (목) 02:52 판 (새 문서: import os from PyQt5 import QtCore, QtGui, QtWidgets, QtSql class BlobDelegate(QtWidgets.QStyledItemDelegate): def displayText(self, value, locale): if isinstance(value,...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)
thumb_up 추천메뉴 바로가기


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_())