main.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2022 The Qt Company Ltd.
  2. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. import sys
  4. import logging
  5. from PySide6.QtCore import QDir, QFile, QUrl
  6. from PySide6.QtGui import QGuiApplication
  7. from PySide6.QtQml import QQmlApplicationEngine
  8. from PySide6.QtSql import QSqlDatabase
  9. # We import the file just to trigger the QmlElement type registration.
  10. import sqlDialog
  11. logging.basicConfig(filename="chat.log", level=logging.DEBUG)
  12. logger = logging.getLogger("logger")
  13. def connectToDatabase():
  14. database = QSqlDatabase.database()
  15. if not database.isValid():
  16. database = QSqlDatabase.addDatabase("QSQLITE")
  17. if not database.isValid():
  18. logger.error("Cannot add database")
  19. write_dir = QDir("")
  20. if not write_dir.mkpath("."):
  21. logger.error("Failed to create writable directory")
  22. # Ensure that we have a writable location on all devices.
  23. abs_path = write_dir.absolutePath()
  24. filename = f"{abs_path}/chat-database.sqlite3"
  25. # When using the SQLite driver, open() will create the SQLite
  26. # database if it doesn't exist.
  27. database.setDatabaseName(filename)
  28. if not database.open():
  29. logger.error("Cannot open database")
  30. QFile.remove(filename)
  31. if __name__ == "__main__":
  32. app = QGuiApplication()
  33. connectToDatabase()
  34. engine = QQmlApplicationEngine()
  35. engine.load(QUrl("chat.qml"))
  36. if not engine.rootObjects():
  37. sys.exit(-1)
  38. app.exec()