12,964
回編集
110行目: | 110行目: | ||
* ラインエディットに対するコピー&ペーストを不可にする。 | * ラインエディットに対するコピー&ペーストを不可にする。 | ||
* <code>QValidator</code>クラスを継承した派生クラスにて、以前の文字数を保存しておき、増加した文字数分の内容を全てを確認する。 | * <code>QValidator</code>クラスを継承した派生クラスにて、以前の文字数を保存しておき、増加した文字数分の内容を全てを確認する。 | ||
<br><br> | |||
== 貼り付けを無効にする == | |||
キーボード入力以外の入力方法として、ラインエディットに文字列を入力するには、以下の方法がある。<br> | |||
* コンテキストメニュー(右クリックメニュー)から貼り付ける。 | |||
* 文字列を選択してドラッグ&ドロップする。 | |||
* [Ctrl] + [V]キーで貼り付ける。 | |||
<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> | |||
以下の例では、ラインエディットをQLineEditクラスを継承した派生クラスQLineEditExクラスに昇格している。 | |||
また、ラインエディットのcontextMenuPolicyプロパティをacceptDropsに設定している。 | |||
押下されたキーの組み合わせが貼り付けに該当する場合、該当イベントを無視するようにしている。 | |||
<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> | ||