12

新しい PyQt アプリを作成しています。PyQt と Qt 全般に関する知識を向上させる手段として、できるだけ多くの PyQt API を使用して、プログラムと ui に関連するすべてを実行しようとしています。

私が持っている質問は、コマンドライン引数の解析をエレガントに処理するための API が PyQt/Qt 内にあるかどうかです。

これまでの私の研究は次のようになりました。

  • QApplication のビルトイン arg 解析を処理しないことを除いて、Python の opt_parserモジュールでうまく動作させる方法の例。
  • PyKDE の KCmdLineArgs (不要な KDE 依存関係を導入します)
  • KCmdLineArgs は Qt5.1 のアップストリームに QCommandLineParser として移植されているようですが、これはクールですが、今から 18 か月後ではなく、今すぐ使用できるようにしたいと考えています。

では、PyQt アプリケーションは通常、これをどのように処理するのでしょうか? または、opt_parser/argparse が進むべき道ですか?

これは良い解決策とはほど遠い...

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys, argparse
from PyQt4 import QtGui

def main(argv):

  app = QtGui.QApplication(argv) # QApplication eats argv in constructor

  # We can get a QStringList out of QApplication of those arguments it 
  # didn't decide were reserved by Qt.
  argv2 = app.arguments()   

  # now we need to turn them back into something that optparse/argparse 
  # can understand, since a QStringList is not what it wants
  argv3 = []
  for i in argv2:
    argv3.append(str(i))

  # now we can pass this to optparse/argparse
  process_args(argv3)

  # dummy app
  mw = QtGui.QMainWindow()
  mw.show()
  sys.exit(app.exec_())

def process_args(argv):
  parser = argparse.ArgumentParser(description='PyQt4 argstest', 
                                   add_help=False)

  # we now have to add all of the options described at 
  # http://qt-project.org/doc/qt-4.8/qapplication.html#QApplication
  # but have them do nothing - in order to have them show up in the help list

  # add this to the list if Qt is a debug build (How to detect this?)
  parser.add_argument("-nograb", action=ignore,
                      help="don't grab keyboard/mouse for debugging")

  # add these to the list if Qt is a debug build for X11
  parser.add_argument("-dograb", action=ignore,
                      help="grab keyboard/mouse for debugging")
  parser.add_argument("-sync", action=ignore,
                      help="run in synchronous mode for debugging")

  # add all the standard args that Qt will grab on all platforms
  parser.add_argument("-reverse", action=ignore,
                      help="run program in Right-to-Left mode")
  # an example -- there are 10 such items in the docs for QApplication

  # then we need to figure out if we're running on X11 and add these
  parser.add_argument("-name", action=ignore,
                      help="sets the application name")
  # an example -- there are 13 such items in the docs

  # reimplement help (which we disabled above) so that -help works rather 
  # than --help; done to be consistent with the style of args Qt wants
  parser.add_argument("-h", "-help", action='help',
                      help="show this help message and exit")

  parser.parse_args(argv[1:])

class ignore(argparse.Action):
  # we create an action that does nothing, so the Qt args do nothing
  def __call__(self, parser, namespace, values, option_string=None):
    pass

if __name__ == "__main__":
  main(sys.argv)
4

3 に答える 3

10

ここでの最善の解決策は、argparseモジュールのparse_known_args()メソッド ( docs ) を使用して、Qt 以外のコマンド ライン オプションを最初に処理することです。を構成する作業はもう必要ありArgumentParserません — 呼び出すメソッドを変更するだけで、単一のオブジェクトの代わりにタプルが返されます。これにより、両方の長所が得られます。

いくつかの Qt 4.8 引数と他のいくつかの引数のみをキャッチする単純化された例ですが、一般的なアイデアを提供します。

# my_script.py

import argparse
from PyQt4 import QtGui  # this will work with PySide.QtGui, too

def process_cl_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-s', '--swallow', action='store')  # optional flag
    parser.add_argument('holy_hand_grenade', action='store')  # positional argument

    parsed_args, unparsed_args = parser.parse_known_args()
    return parsed_args, unparsed_args

if __name__ == '__main__':
    parsed_args, unparsed_args = process_cl_args()
    # QApplication expects the first argument to be the program name.
    qt_args = sys.argv[:1] + unparsed_args
    app = QtGui.QApplication(qt_args)
    # ... the rest of your handling: `sys.exit(app.exec_())`, etc.

これを次のように実行するとします。

$ python my_script.py -dograb --swallow=unladen 3 -style cde

次にparsed_args、通常のNamespacewith holy_hand_grenadeset to3および--swallowset toを持ち'unladen'、一方unparsed_args、単純な list: を持ちます['-dograb', '-style,' 'cde']QApplicationこれは、プログラム名 from を含めて、通常どおり に渡すことができます(これを指摘してくれたmarcinsys.argv[0]に感謝します)。;と連結するための配列を取得するために使用します。あなたも同じようにできるsys.argv[:1]unparsed_args[sys.argv[0]]

特に、これにより、必要に応じて代わりに Qt UI を起動するかどうか、コマンド ラインとして実行するか、単体テストを実行するかなどを指定するようにアプリケーションをセットアップできます。argparse非 Qt (またはその他のもの) の引数を最初に処理することは、使用しているセットアップに依存しないため、より優れていますが、その逆です。

于 2014-01-16T15:56:59.493 に答える
4

使用している場合はargparsePython 2.7を使用します( optparseの場合 < 2.7)。コマンドライン オプションを処理するためにパッケージが PyQt に固有である必要はありません。

于 2012-07-29T21:51:57.560 に答える