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

回答編集履歴

7

typo修正

2021/01/29 02:36

投稿

退会済みユーザー
answer CHANGED
@@ -28,7 +28,7 @@
28
28
  void check_length(const QString& str)
29
29
  {
30
30
  // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル送出
31
- if(this->before_length_ == 0 && str.length() == 1) emit set_a_char();
31
+ if(this->before_length_ == 0 && str.length() <= 1) emit set_a_char();
32
32
  this->before_length_ = str.length();
33
33
  }
34
34
 
@@ -160,7 +160,7 @@
160
160
  void MainWindow::check_length(const QString& str)
161
161
  {
162
162
  // 編集前の文字列が0で編集後の文字列が1の場合ラベル消去
163
- if(this->before_length_ == 0 && str.length() == 1)
163
+ if(this->before_length_ == 0 && str.length() <= 1)
164
164
  {
165
165
  this->ui_->label_1->clear();
166
166
  this->ui_->label_2->clear();

6

typo修正

2021/01/29 02:36

投稿

退会済みユーザー
answer CHANGED
@@ -117,14 +117,12 @@
117
117
  Q_OBJECT
118
118
 
119
119
  int before_length_;// 編集前の文字列長
120
+ Ui::MainWindow* ui_;
120
121
 
121
122
  public:
122
123
  MainWindow(QWidget *parent = nullptr);
123
124
  ~MainWindow();
124
125
 
125
- private:
126
- Ui::MainWindow *ui_;
127
-
128
126
  private slots:
129
127
  // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル送出
130
128
  void check_length(const QString&);

5

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

2021/01/29 02:32

投稿

退会済みユーザー
answer CHANGED
@@ -83,4 +83,100 @@
83
83
  }
84
84
  ```
85
85
 
86
- ![img](d3703fcf6749fc03c5355f3f4225ef5e.gif)
86
+ ![img](d3703fcf6749fc03c5355f3f4225ef5e.gif)
87
+
88
+ ### 追記-MainWindowクラスでの実装
89
+
90
+ uiファイルの方で
91
+ |クラス名|変数名|
92
+ |---|---|
93
+ |QLabel|label_1|
94
+ |QLabel|label_2|
95
+ |QLabel|label_3|
96
+ |QLineEdit|lineEdit|
97
+ |QPushButton|pushButton|
98
+
99
+ を用意しておく。
100
+
101
+ ```c++
102
+ // mainwindow.h
103
+ #pragma once
104
+
105
+ #include <QMainWindow>
106
+
107
+ QT_BEGIN_NAMESPACE
108
+ namespace Ui
109
+ {
110
+ class MainWindow;
111
+ }
112
+ QT_END_NAMESPACE
113
+
114
+ class MainWindow
115
+ :public QMainWindow
116
+ {
117
+ Q_OBJECT
118
+
119
+ int before_length_;// 編集前の文字列長
120
+
121
+ public:
122
+ MainWindow(QWidget *parent = nullptr);
123
+ ~MainWindow();
124
+
125
+ private:
126
+ Ui::MainWindow *ui_;
127
+
128
+ private slots:
129
+ // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル送出
130
+ void check_length(const QString&);
131
+
132
+ // ラベルの文字列復活
133
+ void button_clicked();
134
+ };
135
+
136
+ ```
137
+
138
+ ```c++
139
+ // mainwindow.cpp
140
+ #include "mainwindow.h"
141
+ #include "./ui_mainwindow.h"
142
+
143
+ MainWindow::MainWindow(QWidget *parent)
144
+ : QMainWindow(parent)
145
+ , before_length_(0)
146
+ , ui_(new Ui::MainWindow)
147
+ {
148
+ this->ui_->setupUi(this);
149
+
150
+ // ラインエディットに編集が入った場合文字列長のチェックをする
151
+ this->connect(this->ui_->lineEdit, &QLineEdit::textEdited, this, &MainWindow::check_length);
152
+
153
+ // ボタンが押されたら文字復活
154
+ this->connect(this->ui_->pushButton, &QPushButton::clicked, this, &MainWindow::button_clicked);
155
+ }
156
+
157
+ MainWindow::~MainWindow()
158
+ {
159
+ delete this->ui_;
160
+ }
161
+
162
+ void MainWindow::check_length(const QString& str)
163
+ {
164
+ // 編集前の文字列が0で編集後の文字列が1の場合ラベル消去
165
+ if(this->before_length_ == 0 && str.length() == 1)
166
+ {
167
+ this->ui_->label_1->clear();
168
+ this->ui_->label_2->clear();
169
+ this->ui_->label_3->clear();
170
+ }
171
+
172
+ this->before_length_ = str.length();
173
+ }
174
+
175
+ void MainWindow::button_clicked()
176
+ {
177
+ this->ui_->label_1->setText("1");
178
+ this->ui_->label_2->setText("2");
179
+ this->ui_->label_3->setText("3");
180
+ }
181
+
182
+ ```

4

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

2021/01/29 02:28

投稿

退会済みユーザー
answer CHANGED
@@ -27,7 +27,7 @@
27
27
  private slots:
28
28
  void check_length(const QString& str)
29
29
  {
30
- // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル
30
+ // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル
31
31
  if(this->before_length_ == 0 && str.length() == 1) emit set_a_char();
32
32
  this->before_length_ = str.length();
33
33
  }
@@ -54,7 +54,7 @@
54
54
  {
55
55
  QApplication a(argc, argv);
56
56
 
57
- auto root = QWidget();
57
+ QWidget root;
58
58
  auto* layout = new QVBoxLayout(&root);
59
59
 
60
60
  auto* label_1 = new QLabel("1", &root);// ラベル1
@@ -81,4 +81,6 @@
81
81
 
82
82
  return a.exec();
83
83
  }
84
- ```
84
+ ```
85
+
86
+ ![img](d3703fcf6749fc03c5355f3f4225ef5e.gif)

3

main.cpp側にコメント挿入

2021/01/28 19:02

投稿

退会済みユーザー
answer CHANGED
@@ -57,11 +57,11 @@
57
57
  auto root = QWidget();
58
58
  auto* layout = new QVBoxLayout(&root);
59
59
 
60
- auto* label_1 = new QLabel("1", &root);
60
+ auto* label_1 = new QLabel("1", &root);// ラベル1
61
- auto* label_2 = new QLabel("2", &root);
61
+ auto* label_2 = new QLabel("2", &root);// ラベル2
62
- auto* label_3 = new QLabel("3", &root);
62
+ auto* label_3 = new QLabel("3", &root);// ラベル3
63
- auto* line_edit = new MyLineEdit(&root);
63
+ auto* line_edit = new MyLineEdit(&root);// 今回作成したクラス
64
- auto* back_bt = new QPushButton("back", &root);
64
+ auto* back_bt = new QPushButton("back", &root);// 再びラベルに文字を入れるためのボタン
65
65
 
66
66
  layout->addWidget(label_1);
67
67
  layout->addWidget(label_2);
@@ -71,7 +71,10 @@
71
71
 
72
72
  root.setLayout(layout);
73
73
 
74
+ // 自作したクラスからset_a_charシグナルが飛んできたらラベルを消す
74
75
  QObject::connect(line_edit, &MyLineEdit::set_a_char, [&](){label_1->clear(); label_2->clear(); label_3->clear();});
76
+
77
+ // ボタンが押されたらラベルに文字挿入
75
78
  QObject::connect(back_bt, &QPushButton::clicked, [&](){label_1->setText("1"); label_2->setText("2"); label_3->setText("3");});
76
79
 
77
80
  root.show();

2

TYPO

2021/01/28 17:42

投稿

退会済みユーザー
answer CHANGED
@@ -24,7 +24,7 @@
24
24
  this->connect(this, &MyLineEdit::textEdited, this, &MyLineEdit::check_length);
25
25
  }
26
26
 
27
- public slots:
27
+ private slots:
28
28
  void check_length(const QString& str)
29
29
  {
30
30
  // 編集前の文字列が0で編集後の文字列が1の場合のみset_a_charシグナル創出

1

記載漏れ

2021/01/28 17:14

投稿

退会済みユーザー
answer CHANGED
@@ -20,6 +20,7 @@
20
20
  explicit MyLineEdit(QWidget* parent = nullptr)
21
21
  :QLineEdit(parent), before_length_(0)
22
22
  {
23
+ // 文字列が編集されたらcheck_lengthスロット実行
23
24
  this->connect(this, &MyLineEdit::textEdited, this, &MyLineEdit::check_length);
24
25
  }
25
26