teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

v

2019/10/17 11:35

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -68,6 +68,7 @@
68
68
  def eventFilter(self, obj, event):
69
69
  if event.type() == QEvent.FocusIn:
70
70
  print(obj.objectName())
71
+ # フォーカスされた lineedit を記録しておく。
71
72
  self.SecondWindow.set_lineedit(obj)
72
73
  self.SecondWindow.show()
73
74
 
@@ -88,7 +89,6 @@
88
89
  self.pushButton3.clicked.connect(self.on_button3_clicked)
89
90
 
90
91
  def on_button1_clicked(self):
91
- # self.parent() で親オブジェクト取得
92
92
  self.lineEdit.setText("1番のボタンが押された。")
93
93
 
94
94
  def on_button2_clicked(self):

3

d

2019/10/17 11:35

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -68,6 +68,7 @@
68
68
  def eventFilter(self, obj, event):
69
69
  if event.type() == QEvent.FocusIn:
70
70
  print(obj.objectName())
71
+ self.SecondWindow.set_lineedit(obj)
71
72
  self.SecondWindow.show()
72
73
 
73
74
  return super().eventFilter(obj, event)
@@ -77,6 +78,8 @@
77
78
  def __init__(self, parent=None):
78
79
  super().__init__(parent)
79
80
  uic.loadUi("secondwindow.ui", self)
81
+ self.lineEdit = None
82
+
80
83
  # pushButton1 の clicked シグナルを on_button1_clicked スロットに接続
81
84
  self.pushButton1.clicked.connect(self.on_button1_clicked)
82
85
  # pushButton2 の clicked シグナルを on_button2_clicked スロットに接続
@@ -86,15 +89,18 @@
86
89
 
87
90
  def on_button1_clicked(self):
88
91
  # self.parent() で親オブジェクト取得
89
- self.parent().lineEdit1.setText("1番のボタンが押された。")
92
+ self.lineEdit.setText("1番のボタンが押された。")
90
93
 
91
94
  def on_button2_clicked(self):
92
- self.parent().lineEdit2.setText("2番のボタンが押された。")
95
+ self.lineEdit.setText("2番のボタンが押された。")
93
96
 
94
97
  def on_button3_clicked(self):
95
- self.parent().lineEdit3.setText("3番のボタンが押された。")
98
+ self.lineEdit.setText("3番のボタンが押された。")
96
99
 
100
+ def set_lineedit(self, lineEdit):
101
+ self.lineEdit = lineEdit
97
102
 
103
+
98
104
  if __name__ == "__main__":
99
105
  app = QApplication(sys.argv)
100
106
  win = MyMainWindow()

2

d

2019/10/17 11:31

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -57,9 +57,11 @@
57
57
  self.lineEdit1.installEventFilter(self)
58
58
  self.lineEdit2.installEventFilter(self)
59
59
  self.lineEdit3.installEventFilter(self)
60
+
61
+ # 表示するまで、windows のジオメトリが確定しないため、ここで表示する。
60
62
  self.show()
61
63
 
62
- # メインウィンドウのに表示
64
+ # メインウィンドウの右隣 Second を表示
63
65
  self.SecondWindow = Second(self)
64
66
  self.SecondWindow.move(self.x() + self.width(), self.y())
65
67
 
@@ -70,20 +72,20 @@
70
72
 
71
73
  return super().eventFilter(obj, event)
72
74
 
73
- def on_procStart(self, param):
74
- # クリックされた lineEdit に子画面から受け取った値をセットしたいです。
75
- self.lineEdit1.setText(param)
76
75
 
77
-
78
76
  class Second(QDialog):
79
77
  def __init__(self, parent=None):
80
78
  super().__init__(parent)
81
79
  uic.loadUi("secondwindow.ui", self)
80
+ # pushButton1 の clicked シグナルを on_button1_clicked スロットに接続
82
81
  self.pushButton1.clicked.connect(self.on_button1_clicked)
82
+ # pushButton2 の clicked シグナルを on_button2_clicked スロットに接続
83
83
  self.pushButton2.clicked.connect(self.on_button2_clicked)
84
+ # pushButton3 の clicked シグナルを on_button3_clicked スロットに接続
84
85
  self.pushButton3.clicked.connect(self.on_button3_clicked)
85
86
 
86
87
  def on_button1_clicked(self):
88
+ # self.parent() で親オブジェクト取得
87
89
  self.parent().lineEdit1.setText("1番のボタンが押された。")
88
90
 
89
91
  def on_button2_clicked(self):

1

d

2019/10/17 07:27

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -3,7 +3,7 @@
3
3
  > ボタンを押すと親画面にてクリックした lineEdit に「〇番のボタンが押された。」をテキスト表示したいのですが表示できずに躓いています。
4
4
 
5
5
  シグナルスロットをお使いください。
6
- ボタンが押されたときに呼び出す関数はすでに設定できているようなので、あとは `self.parent()` で親クラスのオブジェクトを取得して、lineEdit1, 2, 3 に setText で直接設定してください。
6
+ ボタンが押されたときに呼び出す関数はすでに設定できているようなので、あとは `self.parent()` で親クラスのオブジェクトを取得して、lineEdit1, lineEdit2, lineEdit3 に setText で直接設定してください。
7
7
 
8
8
 
9
9
  ## エラーが出る問題について