0

私はできるUSBデバイスを持っています

  • 直線軸に沿って「移動」し、
  • 目標に立ち止まり、
  • その位置を報告し、
  • その目標を報告し、
  • ホスト アプリケーションによってターゲットが設定されます。

ここで、位置とターゲットを示すために使用できるウィジェットを作成したいと考えていますが、ターゲット インジケーターをマウスでドラッグできるようにしたいと考えています。これまでに達成したのは、次のような表示部分です。

ここに画像の説明を入力

黒く塗りつぶされた三角形が位置です。ターゲット インジケータは緑色です。

私が欲しいのは、ターゲットインジケーターをドラッグできるようにすることです。これが行われている間、デバイスのターゲットは現在のターゲット インジケーターの値で更新される必要があります。私は gui プログラミングが初めてで (これまでは USB の処理しかしていませんでした。これが私の最初のカスタム ウィジェットです)、どうすればこれを達成できるかわかりません。

ターゲット インジケーターをウィジェットの一部として残しておくのは良い考えですか、それとも何らかの子にする必要がありますか? この種の仕事には一般的なパターンがあると確信していますが、それらを見つけるための適切なキーワードがわかりません。「QWidget ドラッグ」のようなものをグーグル検索しても、ドラッグ アンド ドロップ機能にたどり着くだけで、これは私が必要としているものではありません。

4

1 に答える 1

1

I don't think you should bother with child widgets. In your main QWidget, you just need to override three virtual protected functions:

QWidget::mousePressEvent -- this should check whether or not the user clicked on the target indicator, and if so, set a bool "clickedOnIndicator" to true (and accept the mouse event); otherwise set the bool to false, ignore the mouse event, and call the default implementation

QWidget::mouseMoveEvent -- first check if clickedOnIndicator is true or not; if so, move the indicator to the new position and emit a signal YourWidget::indicatorMoved(int), otherwise call the default implementation

QWidget::paintEvent -- paint the indicator at the correct position (and do whatever else you were doing in the paint event)

于 2012-10-31T20:18:47.873 に答える