2015-10-06 72 views
0

我在我的UI中有一個帶有圖像的QScrollArea,我希望在點擊圖像時獲得一些價值。如何通過鼠標事件拖動獲得價值

更明確的是,我需要改變圖像的亮度,我會用鼠標得到的值。我已經看到MouseMoveEvent,但我不知道如何使用它。

如果我單擊並拖動時獲得鼠標的位置,則可以提取一個值以更改我的圖像的亮度,這一點我知道。我只是不知道我將如何得到這個位置。

有誰知道我能做到這一點?

Ps .:我的QScrollArea創建於Design,所以我沒有任何我寫的規範QScrollArea的規範。

+1

向我們展示一些代碼。你有什麼嘗試? –

+0

我沒有這方面的任何內容,我試圖把一個QScrollBar放在QSrollArea裏面,也許當我滾動鼠標時圖像會改變。我可以做到這一點,但這不是我想要的,導致scrollBar一直出現,只有當我點擊ScrollBar時纔會有效,所以我沒有任何代碼顯示,對不起:s – alitalvez

回答

0

您需要的所有信息都在發送到您的小部件的mouseMoveEvent處理程序的QMouseEvent對象中。

QMouseEvent::buttons()
QMouseEvent::pos()

一個簡單的方法做你追求的是每當你收到一個「鼠標移動事件」和QMouseEvent對象報告一個按鈕可改變圖像的亮度(這意味着用戶在按住按鈕的同時移動鼠標)。

void MyWidget::mousePressEvent(QMouseEvent* event) 
{ 
    if (event->button() == Qt::LeftButton) 
    { 
     // Keep the clicking position in some private member of type 'QPoint.' 

     m_lastClickPosition = event->pos(); 
    } 
} 


void MyWidget::mouseMoveEvent(QMouseEvent* event) 
{ 
    // The user is moving the cursor. 
    // See if the user is pressing down the left mouse button. 

    if (event->buttons() & Qt::LeftButton) 
    { 
     const int deltaX = event->pos().x() - m_lastClickPosition.x(); 
     if (deltaX > 0) 
     { 
      // The user is moving the cursor to the RIGHT. 
      // ... 
     } 
     else if (deltaX < 0) // This second IF is necessary in case the movement was all vertical. 
     { 
      // The user is moving the cursor to the LEFT. 
      // ... 
     } 
    } 
} 
+0

我昨天發現如何獲取我想要的鼠標位置,漂亮的樣子。所以,這應該工作兩個,無論如何。 – alitalvez

+0

你好。你可以在這裏回答你自己的問題,所以下次你可以爲自己寫一個答案並選擇它作爲解決方案。這似乎是一種鼓勵性的做法。問候。 –