LoginSignup
0
0

PySide6 での QCoreApplication.application~

Posted at

初めに

Qt6のアプリを pythonへ移植した際にバージョン表記などで困ったので

QCoreApplicationのapplication~で取得できる情報

Qt6 で開発したアプリの場合: C:\Path\to\folder\app\app.exe

applicationDirPath applicationFilePath applicationName
C:\Path\to\folder\app C:\Path\to\folder\app\app.exe app

PySide6 で開発したアプリの場合: C:\Path\to\folder\app\app.py

pythonのインストール先: C:\Python\Python311

applicationDirPath applicationFilePath applicationName
C:\Python\Python311 C:\Python\Python311\python.exe python

pythonが.pyファイルを実行しているからこうなってしまうんですね
当たり前なのかもしれませんが

対応策

これが最善ではないだろが思いついた方法

  • applicatoinNameにはsys.argv[0]__file__ に aaa.py が入っているのでそれを使う
  • applicationDirPathapplicationFilePathは読み込み専用なので変更できないからQCoreApplicationのインスタンスに属性として設定し使用する
  • applicationVersionも使用したい場合は メインモジュールでQCoreApplicationのインスタンスへsetApplicationVersionで設定する
import sys
import PySide6.QtCore

if "__main__" == __name__:
  __version__ = "1.0.0.1"
  app = PySide6.QtCore.QCoreApplication(sys.argv)
  app.setApplicationVersion(__version__)
  # 以下 PySide6 使ってるので osモジュールではなくQFileInfoで
  fi = PySide6.QtCore.QFileInfo(__file__)
  app.setApplicationName(fi.baseName())
  app.application_file_path = fi.absoluteFilePath()
  app.application_dir_path = fi.absolutePath()

nuitka で .exe にした場合はまた別の話

終わりに

Qt6 のC++コードをPySide6へ移行することは比較的簡単だったが この様な細かい調整が必要

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0