質問編集履歴

3

分かりやすくしました。

2018/04/09 12:35

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,11 @@
10
10
 
11
11
 
12
12
 
13
- 一応動くものは作成できているのですが、ちょっと不恰好になっています。
13
+ 下記の通り一応動くものは作成できているのですが(②で①を動かす感じです)
14
+
15
+ ちょっと不恰好になっています。
16
+
17
+
14
18
 
15
19
  サンプルコードなどありましたら教えてもらえると嬉しいです。
16
20
 
@@ -22,7 +26,7 @@
22
26
 
23
27
 
24
28
 
25
- mainwindow.py
29
+ mainwindow.py(Qtで作成したソース)
26
30
 
27
31
  ```Python
28
32
 
@@ -106,7 +110,7 @@
106
110
 
107
111
 
108
112
 
109
-
113
+ ②Qtのモジュールを動かすモジュール
110
114
 
111
115
  ```python
112
116
 

2

質問を記入しました

2018/04/09 12:34

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1 +1,185 @@
1
+ ### 前提・実現したいこと
2
+
3
+
4
+
5
+ Qt Creater で作成したPython用のGUIのコードを
6
+
7
+ そのまま活かしてコーディングをしたいと思っています。
8
+
1
- ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
9
+ その場合、どうメインのモジュルから動かすかについて悩んでいます。
10
+
11
+
12
+
13
+ 一応動くものは作成できているのですが、ちょっと不恰好になっています。
14
+
15
+ サンプルコードなどありましたら教えてもらえると嬉しいです。
16
+
17
+ (ググってみたんですが、Qtで作ったGUIモジュールをそのまま活かしているケースはあまりなく。。)
18
+
19
+
20
+
21
+ ### 該当のソースコード
22
+
23
+
24
+
25
+ ●mainwindow.py
26
+
27
+ ```Python
28
+
29
+ # -*- coding: utf-8 -*-
30
+
31
+
32
+
33
+ # Form implementation generated from reading ui file 'mainwindow.ui'
34
+
35
+ #
36
+
37
+ # Created by: PyQt5 UI code generator 5.10.1
38
+
39
+ #
40
+
41
+ # WARNING! All changes made in this file will be lost!
42
+
43
+
44
+
45
+ from PyQt5 import QtCore, QtGui, QtWidgets
46
+
47
+
48
+
49
+ class Ui_MainWindow(object):
50
+
51
+ def setupUi(self, MainWindow): *引数にMainWindowが渡されている。
52
+
53
+ MainWindow.setObjectName("MainWindow")
54
+
55
+ MainWindow.resize(308, 206)
56
+
57
+
58
+
59
+ self.textBrowser = QtWidgets.QTextBrowser(MainWindow)
60
+
61
+ self.textBrowser.setGeometry(QtCore.QRect(40, 90, 180, 79))
62
+
63
+ self.textBrowser.setObjectName("textBrowser")
64
+
65
+
66
+
67
+ self.centralWidget = QtWidgets.QWidget(MainWindow)
68
+
69
+ self.centralWidget.setObjectName("centralWidget")
70
+
71
+
72
+
73
+ self.pushButton = QtWidgets.QPushButton(self.centralWidget)
74
+
75
+ self.pushButton.setGeometry(QtCore.QRect(250, 40, 51, 21))
76
+
77
+ self.pushButton.setObjectName("pushButton")
78
+
79
+ self.lineEdit = QtWidgets.QLineEdit(self.centralWidget)
80
+
81
+ self.lineEdit.setGeometry(QtCore.QRect(20, 10, 281, 21))
82
+
83
+ self.lineEdit.setText("")
84
+
85
+ self.lineEdit.setObjectName("lineEdit")
86
+
87
+ MainWindow.setCentralWidget(self.centralWidget) *引数MainWindowにはsetCentralWidgetというメソッドがあることになっている。
88
+
89
+
90
+
91
+ self.retranslateUi(MainWindow)
92
+
93
+ QtCore.QMetaObject.connectSlotsByName(MainWindow)
94
+
95
+
96
+
97
+ def retranslateUi(self, MainWindow):
98
+
99
+ _translate = QtCore.QCoreApplication.translate
100
+
101
+ MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
102
+
103
+ self.pushButton.setText(_translate("MainWindow", "SUBMIT"))
104
+
105
+ ```
106
+
107
+
108
+
109
+
110
+
111
+ ```python
112
+
113
+ from PyQt5.QtWidgets import (QApplication, QWidget,
114
+
115
+ QGridLayout, QVBoxLayout, QHBoxLayout,
116
+
117
+ QLabel, QLineEdit, QPushButton, QMainWindow)
118
+
119
+ import mainwindow
120
+
121
+
122
+
123
+ class MainWindow(QMainWindow):
124
+
125
+ # 適当なクラスを継承する。
126
+
127
+ # どのクラスを継承するかはQtで作った.pyファイルの内容に左右される。
128
+
129
+ # 今回は、mainwindow.py L30 の、
130
+
131
+ # MainWindow.setCentralWidget(self.centralWidget)
132
+
133
+ # にて出てくるsetCentralWidgetというメソッドを含むクラスを継承する必要があった。
134
+
135
+ # どのクラスを継承するか、というのはどこかのマニュアルにでも書いてないものかと思うが。。
136
+
137
+ # まあでも、とりあえずQWidgetを継承させてダメだったらQMainWindowを継承とかでもいいかもしれない。
138
+
139
+
140
+
141
+ def __init__(self, parent=None):
142
+
143
+ super(MainWindow, self).__init__(parent)
144
+
145
+ self.ui = mainwindow.Ui_MainWindow()
146
+
147
+ self.ui.setupUi(self)
148
+
149
+ # ここまで呪文。import元のコードのsetupUiを呼び出してセットアップする。
150
+
151
+ self.ui.pushButton.clicked.connect(self.submit)
152
+
153
+ # ここでどんなイベントを起こしたいのか記述する。
154
+
155
+ # self.ui.追加したオブジェクト名.イベント名.connect(self.メソッド名)
156
+
157
+
158
+
159
+ def submit(self):
160
+
161
+ # イベントによって動く具体的な部分。
162
+
163
+ text = self.ui.lineEdit.text()
164
+
165
+ self.ui.textBrowser.append(text)
166
+
167
+
168
+
169
+
170
+
171
+ if __name__ == '__main__':
172
+
173
+ import sys
174
+
175
+ app = QApplication(sys.argv)
176
+
177
+ main_window = MainWindow()
178
+
179
+ main_window.show()
180
+
181
+ # main_windowの表示
182
+
183
+ sys.exit(app.exec_())
184
+
185
+ ```

1

2018/04/07 10:56

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,9 +1 @@
1
- Qt Createrを使ってGUI部分を作成しています。
2
-
3
-
4
-
5
- 下記のようにメインのコードを書いているのですが、
6
-
7
-
8
-
9
- Qt Createrで作成したクラスに渡す引数をどうするか。何を継承させるか。
1
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー