12,964
回編集
(ページの作成:「== 概要 == Qtのコントロールであるラインエディットについて記載する。<br> <br><br> == 入力文字の確認 == ==== QValidatorクラスを使…」) |
|||
111行目: | 111行目: | ||
* <code>QValidator</code>クラスを継承した派生クラスにて、以前の文字数を保存しておき、増加した文字数分の内容を全てを確認する。 | * <code>QValidator</code>クラスを継承した派生クラスにて、以前の文字数を保存しておき、増加した文字数分の内容を全てを確認する。 | ||
<br><br> | <br><br> | ||
== フォーカスイベント == | |||
QLineEditクラスには、textChangedやtextEdited等のシグナルは存在するが、フォーカスに関係したシグナルが存在しない。 | |||
例えば、QLineEditの初期表示において、"キーワードを入力してください..."等を表示して、フォーカスインで消去する場合、 | |||
QLineEditクラスのfocusInEventおよびfocusOutEventをオーバーライドすることで実現できる。 | |||
<br> | |||
以下の例では、QLineEditを継承した派生クラスを作成して、フォーカスイベントを処理している。 | |||
Qt Designerで配置したラインエディットのコントロールは、QLineEditクラスを継承した派生クラスに昇格させる。 | |||
<syntaxhighlight lang="c++"> | |||
// QLineEditEx.cpp | |||
#include "QLineEditEx.h" | |||
QLineEditEx::QLineEditEx(QWigedt *parent) : QLineEdit(parent) | |||
{ | |||
} | |||
void QLineEditEx::focusInEvent(QFocusEvent *e) | |||
{ | |||
QLineEdit::focusInEvent(e); | |||
} | |||
void QLineEditEx::focusOutEvent(QFocusEvent *e) | |||
{ | |||
QLineEdit::focusOutEvent(e); | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// QLineEditEx.h | |||
#pragma once | |||
#include <QWigedt> | |||
#include <QLineEdit> | |||
class QLineEditEx : public QLineEdit | |||
{ | |||
Q_OBJECT | |||
public: | |||
explicit QLineEditEx(QWigedt *parent = nullptr); | |||
protected: | |||
void focusInEvent(QFocusEvent *) override; | |||
void focusOutEvent(QFocusEvent *) override; | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// MainWindow.cpp | |||
#include "MainWindow.h" | |||
#include "ui_MainWindow.h" | |||
MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent), ui(new Ui::Widget) | |||
{ | |||
ui->setupUi(this); | |||
} | |||
MainWindow::~MainWindow() | |||
{ | |||
delete ui; | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// MainWindow.h | |||
#pragma once | |||
#include <QMainWindow> | |||
namespace Ui {class MainWindow;} | |||
class MainWindow : public QMainWindow | |||
{ | |||
Q_OBJECT | |||
public: | |||
explicit MainWindow(QMainWindow *parent = nullptr); | |||
~MainWindow(); | |||
private: | |||
Ui::MainWindow *ui; | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:Qt]] | [[カテゴリ:Qt]] |