回答編集履歴

7

typo修正

2021/01/29 02:36

投稿

退会済みユーザー
test CHANGED
@@ -58,7 +58,7 @@
58
58
 
59
59
  // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル送出
60
60
 
61
- if(this->before_length_ == 0 && str.length() == 1) emit set_a_char();
61
+ if(this->before_length_ == 0 && str.length() <= 1) emit set_a_char();
62
62
 
63
63
  this->before_length_ = str.length();
64
64
 
@@ -322,7 +322,7 @@
322
322
 
323
323
  // 編集前の文字列が0で編集後の文字列が1の場合ラベル消去
324
324
 
325
- if(this->before_length_ == 0 && str.length() == 1)
325
+ if(this->before_length_ == 0 && str.length() <= 1)
326
326
 
327
327
  {
328
328
 

6

typo修正

2021/01/29 02:36

投稿

退会済みユーザー
test CHANGED
@@ -236,6 +236,8 @@
236
236
 
237
237
  int before_length_;// 編集前の文字列長
238
238
 
239
+ Ui::MainWindow* ui_;
240
+
239
241
 
240
242
 
241
243
  public:
@@ -246,12 +248,6 @@
246
248
 
247
249
 
248
250
 
249
- private:
250
-
251
- Ui::MainWindow *ui_;
252
-
253
-
254
-
255
251
  private slots:
256
252
 
257
253
  // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル送出

5

MainWindowクラス側のみの変更追記

2021/01/29 02:32

投稿

退会済みユーザー
test CHANGED
@@ -169,3 +169,195 @@
169
169
 
170
170
 
171
171
  ![img](d3703fcf6749fc03c5355f3f4225ef5e.gif)
172
+
173
+
174
+
175
+ ### 追記-MainWindowクラスでの実装
176
+
177
+
178
+
179
+ uiファイルの方で
180
+
181
+ |クラス名|変数名|
182
+
183
+ |---|---|
184
+
185
+ |QLabel|label_1|
186
+
187
+ |QLabel|label_2|
188
+
189
+ |QLabel|label_3|
190
+
191
+ |QLineEdit|lineEdit|
192
+
193
+ |QPushButton|pushButton|
194
+
195
+
196
+
197
+ を用意しておく。
198
+
199
+
200
+
201
+ ```c++
202
+
203
+ // mainwindow.h
204
+
205
+ #pragma once
206
+
207
+
208
+
209
+ #include <QMainWindow>
210
+
211
+
212
+
213
+ QT_BEGIN_NAMESPACE
214
+
215
+ namespace Ui
216
+
217
+ {
218
+
219
+ class MainWindow;
220
+
221
+ }
222
+
223
+ QT_END_NAMESPACE
224
+
225
+
226
+
227
+ class MainWindow
228
+
229
+ :public QMainWindow
230
+
231
+ {
232
+
233
+ Q_OBJECT
234
+
235
+
236
+
237
+ int before_length_;// 編集前の文字列長
238
+
239
+
240
+
241
+ public:
242
+
243
+ MainWindow(QWidget *parent = nullptr);
244
+
245
+ ~MainWindow();
246
+
247
+
248
+
249
+ private:
250
+
251
+ Ui::MainWindow *ui_;
252
+
253
+
254
+
255
+ private slots:
256
+
257
+ // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル送出
258
+
259
+ void check_length(const QString&);
260
+
261
+
262
+
263
+ // ラベルの文字列復活
264
+
265
+ void button_clicked();
266
+
267
+ };
268
+
269
+
270
+
271
+ ```
272
+
273
+
274
+
275
+ ```c++
276
+
277
+ // mainwindow.cpp
278
+
279
+ #include "mainwindow.h"
280
+
281
+ #include "./ui_mainwindow.h"
282
+
283
+
284
+
285
+ MainWindow::MainWindow(QWidget *parent)
286
+
287
+ : QMainWindow(parent)
288
+
289
+ , before_length_(0)
290
+
291
+ , ui_(new Ui::MainWindow)
292
+
293
+ {
294
+
295
+ this->ui_->setupUi(this);
296
+
297
+
298
+
299
+ // ラインエディットに編集が入った場合文字列長のチェックをする
300
+
301
+ this->connect(this->ui_->lineEdit, &QLineEdit::textEdited, this, &MainWindow::check_length);
302
+
303
+
304
+
305
+ // ボタンが押されたら文字復活
306
+
307
+ this->connect(this->ui_->pushButton, &QPushButton::clicked, this, &MainWindow::button_clicked);
308
+
309
+ }
310
+
311
+
312
+
313
+ MainWindow::~MainWindow()
314
+
315
+ {
316
+
317
+ delete this->ui_;
318
+
319
+ }
320
+
321
+
322
+
323
+ void MainWindow::check_length(const QString& str)
324
+
325
+ {
326
+
327
+ // 編集前の文字列が0で編集後の文字列が1の場合ラベル消去
328
+
329
+ if(this->before_length_ == 0 && str.length() == 1)
330
+
331
+ {
332
+
333
+ this->ui_->label_1->clear();
334
+
335
+ this->ui_->label_2->clear();
336
+
337
+ this->ui_->label_3->clear();
338
+
339
+ }
340
+
341
+
342
+
343
+ this->before_length_ = str.length();
344
+
345
+ }
346
+
347
+
348
+
349
+ void MainWindow::button_clicked()
350
+
351
+ {
352
+
353
+ this->ui_->label_1->setText("1");
354
+
355
+ this->ui_->label_2->setText("2");
356
+
357
+ this->ui_->label_3->setText("3");
358
+
359
+ }
360
+
361
+
362
+
363
+ ```

4

type修正と分かりやすくするためgif挿入

2021/01/29 02:28

投稿

退会済みユーザー
test CHANGED
@@ -56,7 +56,7 @@
56
56
 
57
57
  {
58
58
 
59
- // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル
59
+ // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル
60
60
 
61
61
  if(this->before_length_ == 0 && str.length() == 1) emit set_a_char();
62
62
 
@@ -110,7 +110,7 @@
110
110
 
111
111
 
112
112
 
113
- auto root = QWidget();
113
+ QWidget root;
114
114
 
115
115
  auto* layout = new QVBoxLayout(&root);
116
116
 
@@ -165,3 +165,7 @@
165
165
  }
166
166
 
167
167
  ```
168
+
169
+
170
+
171
+ ![img](d3703fcf6749fc03c5355f3f4225ef5e.gif)

3

main.cpp側にコメント挿入

2021/01/28 19:02

投稿

退会済みユーザー
test CHANGED
@@ -116,15 +116,15 @@
116
116
 
117
117
 
118
118
 
119
- auto* label_1 = new QLabel("1", &root);
119
+ auto* label_1 = new QLabel("1", &root);// ラベル1
120
120
 
121
- auto* label_2 = new QLabel("2", &root);
121
+ auto* label_2 = new QLabel("2", &root);// ラベル2
122
122
 
123
- auto* label_3 = new QLabel("3", &root);
123
+ auto* label_3 = new QLabel("3", &root);// ラベル3
124
124
 
125
- auto* line_edit = new MyLineEdit(&root);
125
+ auto* line_edit = new MyLineEdit(&root);// 今回作成したクラス
126
126
 
127
- auto* back_bt = new QPushButton("back", &root);
127
+ auto* back_bt = new QPushButton("back", &root);// 再びラベルに文字を入れるためのボタン
128
128
 
129
129
 
130
130
 
@@ -144,7 +144,13 @@
144
144
 
145
145
 
146
146
 
147
+ // 自作したクラスからset_a_charシグナルが飛んできたらラベルを消す
148
+
147
149
  QObject::connect(line_edit, &MyLineEdit::set_a_char, [&](){label_1->clear(); label_2->clear(); label_3->clear();});
150
+
151
+
152
+
153
+ // ボタンが押されたらラベルに文字挿入
148
154
 
149
155
  QObject::connect(back_bt, &QPushButton::clicked, [&](){label_1->setText("1"); label_2->setText("2"); label_3->setText("3");});
150
156
 

2

TYPO

2021/01/28 17:42

投稿

退会済みユーザー
test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
 
52
52
 
53
- public slots:
53
+ private slots:
54
54
 
55
55
  void check_length(const QString& str)
56
56
 

1

記載漏れ

2021/01/28 17:14

投稿

退会済みユーザー
test CHANGED
@@ -41,6 +41,8 @@
41
41
  :QLineEdit(parent), before_length_(0)
42
42
 
43
43
  {
44
+
45
+ // 文字列が編集されたらcheck_lengthスロット実行
44
46
 
45
47
  this->connect(this, &MyLineEdit::textEdited, this, &MyLineEdit::check_length);
46
48