QtDesigner でユーザー インターフェイスを再設計するときにコードの変更を保存する方法
Qt Designer を使用して Python アプリケーションの GUI を作成する場合、後続の UI でカスタム コードの変更が失われるのはイライラします再設計します。この記事では、この問題の解決策を検討し、ロジックを個別の Python ファイルに分割するという概念を紹介します。
コード変更の維持
Qt デザイナーを使用して UI を再設計するときにコードの変更が失われないようにするには、次のことをお勧めします。 Qt Designer から生成されたコードを別に保ちながら、GUI ロジックを実装するセカンダリ ファイルを作成します。
たとえば、 MainWindow テンプレートは「design.ui」で使用され、それを「Ui_Design.py」に変換し、ロジックを実装するための新しいファイル「logic.py」を作成します。
# Ui_Design.py (Generated from Qt Designer) from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): [...] def retranslateUi(self, MainWindow): [...] # logic.py from PyQt5 import QtCore, QtGui, QtWidgets from Ui_Design import Ui_MainWindow class Logic(QMainWindow, Ui_MainWindow): def __init__(self, *args, **kwargs): QMainWindow.__init__(self, *args, **kwargs) self.setupUi(self)
このアプローチを使用すると、UI の変更が確実に行われます。 Qt Designer では、「logic.py」のカスタム コードは上書きされません。
ファイル全体にロジックを分散するPython
コードをさらに整理して管理するには、ロジック クラスを複数のファイルに分割し、各ファイルが機能の特定の側面に重点を置くようにすることができます。
これを実現するには、ロジック クラスは次のようにする必要があります。特定のものに従ってください構造体:
class Logic(PyQtClass, DesignClass): def __init__(self, *args, **kwargs): PyQtClass.__init__(self, *args, **kwargs) self.setupUi(self)
ここで:
- PyQtClass: 選択した Qt テンプレートに依存します (例: Window の場合は QMainWindow、QWidget の場合は QWidget) Widget).
- DesignClass: Qt デザイナーによって生成されたクラス (例: Ui_MainWindow)。
親ウィンドウの CloseEvent で PyQt MessageBox を閉じる
別のファイルにロジックを実装する例として、次のファイルを作成することを検討してください。 PyQt アプリケーションを閉じる前に確認メッセージ ボックスを表示する close イベント ハンドラー。
class Logic(QMainWindow, Ui_MainWindow): def __init__(self, *args, **kwargs): QMainWindow.__init__(self, *args, **kwargs) self.setupUi(self) def closeEvent(self, event): answer = QtWidgets.QMessageBox.question( self, 'Are you sure you want to quit ?', 'Task is in progress !', QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No) if answer == QtWidgets.QMessageBox.Yes: event.accept() else: event.ignore()
コードを個別のファイルに分割し、ロジック クラス構造に従うことで、Qt ベースの Python アプリケーションを簡単に保守および拡張できます。 UI の再設計中もカスタム コードの変更を維持しながら。
以上がPython で Qt Designer UI を再設計するときにカスタム コードを保存するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します

Useforloopswhenthenumberofiterationsisknowninadvance、andwhiloopswheniterationsdependonacondition.1)forloopsareidealforsecenceslikelistoranges.2)


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

WebStorm Mac版
便利なJavaScript開発ツール
