📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| (同じ利用者による、間の6版が非表示) | |||
| 1行目: | 1行目: | ||
== 概要 == | == 概要 == | ||
Qtのコントロールであるラインエディットについて記載する。<br> | Qtのコントロールであるラインエディットについて記載する。<br> | ||
<br><br> | |||
== テキストの入力 == | |||
<syntaxhighlight lang="c++"> | |||
QString str = ""; | |||
// ...略 | |||
if(!str.isEmpty()) | |||
{ | |||
lineEdit->setText(str); | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
== テキストの取得 == | |||
<syntaxhighlight lang="c++"> | |||
QString str = lineEdit->text(); | |||
</syntaxhighlight> | |||
<br><br> | |||
== パスワード入力モードにする == | |||
Qt Designerのプロパティ[echoMode]を[Password]に設定する。<br> | |||
または、ソースコードで以下を記述する。<br> | |||
<syntaxhighlight lang="c++"> | |||
lineEdit->setEchoMode(QLineEdit::Password) | |||
</syntaxhighlight> | |||
<br><br> | <br><br> | ||
| 110行目: | 137行目: | ||
* ラインエディットに対するコピー&ペーストを不可にする。 | * ラインエディットに対するコピー&ペーストを不可にする。 | ||
* <code>QValidator</code>クラスを継承した派生クラスにて、以前の文字数を保存しておき、増加した文字数分の内容を全てを確認する。 | * <code>QValidator</code>クラスを継承した派生クラスにて、以前の文字数を保存しておき、増加した文字数分の内容を全てを確認する。 | ||
<br><br> | |||
== 貼り付けを無効にする == | |||
キーボード入力以外の入力方法として、ラインエディットに文字列を入力するには、以下の方法がある。<br> | |||
* コンテキストメニュー(右クリックメニュー)から貼り付ける。 | |||
* 文字列を選択してドラッグ&ドロップする。 | |||
* [Ctrl] + [V]キーで貼り付ける。 | |||
<br> | |||
上記の方法を無効化する方法を以下に示す。<br> | |||
* コンテキストメニューの無効化 | |||
*: Qt Designerにて、<code>contextMenuPolicy</code>プロパティの値をを<code>NoContextMenu</code>にする。 | |||
*: また、ソースコードにて以下を記述する。 | |||
*: <code>setContextMenuPolicy(Qt::NoContextMenu);</code> | |||
*: 無効にしない場合、値を<code>NoContextMenu</code>ではなく<code>CustomContextMenu</code>にして、独自のコンテキストメニューを作成してもよい。 | |||
*: <br> | |||
* ドラッグ&ドロップの無効化 | |||
*: Qt Designerにて、<code>acceptDrops</code>プロパティのチェックボックスからチェックを外す。 | |||
*: また、ソースコードにて以下を記述する。 | |||
*: <code>setAcceptDrops(false);</code> | |||
*: <br> | |||
* [Ctrl] + [V]キーの無効化 | |||
*: <code>QLineEdit::keyPressEvent</code>メソッドをオーバーライドする。 | |||
<br> | |||
以下の例では、ラインエディットを<code>QLineEdit</code>クラスを継承した派生クラス<code>QLineEditEx</code>クラスに昇格している。<br> | |||
また、ラインエディットの<code>contextMenuPolicy</code>プロパティの値を<code>acceptDrops</code>に設定している。<br> | |||
<br> | |||
押下されたキーの組み合わせが貼り付けに該当する場合、該当イベントを無視するようにしている。<br> | |||
<syntaxhighlight lang="c++"> | |||
// QLineEditEx.cpp | |||
#include "QLineEditEx.h" | |||
QLineEditEx::QLineEditEx(QWigedt *parent) : QLineEdit(parent) | |||
{ | |||
} | |||
void QLineEditEx::keyPressEvent(QKeyEvent *event) | |||
{ | |||
if(event->matches(QKeySequence::Paste)) | |||
{ | |||
event->ignore(); | |||
} | |||
else | |||
{ | |||
return QLineEdit::keyPressEvent(event); | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// QLineEditEx.h | |||
#pragma once | |||
#include <QWigedt> | |||
#include <QLineEdit> | |||
#include <QKeyEvent> | |||
class QLineEditEx : public QLineEdit | |||
{ | |||
Q_OBJECT | |||
public: | |||
explicit QLineEditEx(QWigedt *parent = nullptr); | |||
protected: | |||
void keyPressEvent(QKeyEvent *) 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><br> | <br><br> | ||
| 301行目: | 436行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
{{#seo: | |||
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki | |||
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 | |||
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux | |||
|image=/resources/assets/MochiuLogo_Single_Blue.png | |||
}} | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:Qt]] | [[カテゴリ:Qt]] | ||