在pyqt5教程的這一部分,我們將討論拖放操作。
在電腦圖形用戶界面,拖放事件就是點擊一個虛擬對象,并將其拖動到其他位置或到另一個虛擬物體的動作。在一般情況下,它可以被用于調(diào)用多種動作,或創(chuàng)建兩個抽象對象之間的關聯(lián)的各種類型。
拖放事件是圖形用戶界面的一部分。拖放操作使用戶能夠直觀地操作一些復雜的事情。
通常情況下,我們可以拖放兩種類型:數(shù)據(jù)或某些圖形對象。如果我們從一個應用程序拖動圖像到另一個,我們拖放的是二進制數(shù)據(jù)。如果我們拖放firefox標簽并將其移動到另一個地方,我們拖放的是圖形組件。
簡單拖放事件
在這個例子中,我們有一個qlineedit控件和一個qpushbutton控件。我們從單行文本編輯控件中將輸入的文本選中后拖到按鈕控件上后松開鼠標,按鈕的標簽將發(fā)生變化。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ pyqt5 教程 這是一個簡單的拖放例子。 作者:我的世界你曾經(jīng)來過 博客:http://blog.csdn.net/weiaitaowang 最后編輯:2016年8月5日 """ import sys from pyqt5.qtwidgets import qapplication, qwidget, qlineedit, qpushbutton class button(qpushbutton): def __init__( self , title, parent): super ().__init__(title, parent) self .setacceptdrops(true) def dragenterevent( self , e): if e.mimedata().hasformat( 'text/plain' ): e.accept() else : e.ignore() def dropevent( self , e): self .settext(e.mimedata().text()) class example(qwidget): def __init__( self ): super ().__init__() self .initui() def initui( self ): edit = qlineedit('', self ) edit.setdragenabled(true) edit.move( 30 , 65 ) button = button( '按鈕' , self ) button.move( 190 , 65 ) self .setgeometry( 300 , 300 , 300 , 150 ) self .setwindowtitle( '簡單拖放' ) if __name__ = = '__main__' : app = qapplication(sys.argv) ex = example() ex.show() sys.exit(app.exec_()) |
這個例子介紹了一個簡單的拖放操作。
1
2
3
4
5
|
class button(qpushbutton): def __init__( self , title, parent): super ().__init__(title, parent) self .setacceptdrops(true) |
為了在qpushbutton控件中顯示放置的文字,我們必須將qpushbutton控件的一些方法重寫。因此,我們創(chuàng)造我們自己的按鈕類將從qpushbutton類繼承。
1
|
self .setacceptdrops(true) |
為控件啟用拖放事件。
1
2
3
4
5
|
def dragenterevent( self , e): if e.mimedata().hasformat( 'text/plain' ): e.accept() else : e.ignore() |
首先,重寫了dragenterevent()方法。告知我們接受的數(shù)據(jù)類型(text/plain)。通常情況下,它是純文本。
1
2
|
def dropevent( self , e): self .settext(e.mimedata().text()) |
接下來重寫了dropevent()方法,這里定義了drop事件將要做的事情。在這里我們改變按鈕控件的文本。
1
2
|
edit = qlineedit('', self ) edit.setdragenabled(true) |
若要啟用qlineedit控件的拖動操作,需要做的是調(diào)用setdragenabled()方法來激活它。
程序執(zhí)行后
拖放按鈕控件
在下面的例子中,我們將演示如何拖放一個按鈕控件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ pyqt5 教程 在這個程序中,我們可以按上用鼠標左鍵點擊或拖動一個按鈕,用鼠標右鍵單擊刪除按鈕。 作者:我的世界你曾經(jīng)來過 博客:http://blog.csdn.net/weiaitaowang 最后編輯:2016年8月5日 """ import sys from pyqt5.qtwidgets import qapplication, qwidget, qpushbutton from pyqt5.qtcore import qt, qmimedata from pyqt5.qtgui import qdrag class button(qpushbutton): def __init__( self , title, parent): super ().__init__(title, parent) def mousemoveevent( self , e): if e.buttons() ! = qt.rightbutton: return mimedata = qmimedata() drag = qdrag( self ) drag.setmimedata(mimedata) drag.sethotspot(e.pos() - self .rect().topleft()) drag.exec_(qt.moveaction) def mousepressevent( self , e): qpushbutton.mousepressevent( self , e) if e.button() = = qt.leftbutton: print ( 'press' ) class example(qwidget): def __init__( self ): super ().__init__() self .initui() def initui( self ): self .setacceptdrops(true) self .button = button( '按鈕' , self ) self .button.move( 100 , 65 ) self .setgeometry( 300 , 300 , 280 , 150 ) self .setwindowtitle( '按鈕拖放' ) def dragenterevent( self , e): e.accept() def dropevent( self , e): position = e.pos() self .button.move(position) e.setdropaction(qt.moveaction) e.accept() if __name__ = = '__main__' : app = qapplication(sys.argv) ex = example() ex.show() sys.exit(app.exec_()) |
在我們的代碼示例中,窗口有一個qpushbutton 按鈕。如果我們用鼠標左鍵按下按鈕,'press' 消息打印到控制臺。如果用鼠標右鍵按住按鈕并移動鼠標,程序?qū)?zhí)行一個拖放按鈕控件事件。
1
2
3
4
|
class button(qpushbutton): def __init__( self , title, parent): super ().__init__(title, parent) |
創(chuàng)建一個button 類從qpushbutton派生。我們還重寫了qpushbutton的兩種方法:mousemoveevent()和mousepressevent()。該mousemoveevent()方法是其中拖放操作開始的地方。
1
2
|
if e.buttons() ! = qt.rightbutton: return |
在這里,我們確認執(zhí)行拖放事件只能使用鼠標右鍵。鼠標左鍵被保留用于單擊按鈕事件。
1
2
3
4
|
mimedata = qmimedata() drag = qdrag( self ) drag.setmimedata(mimedata) drag.sethotspot(e.pos() - self .rect().topleft()) |
創(chuàng)建qdrag 對象。這個類提供了基于mime的拖放數(shù)據(jù)傳輸?shù)闹С帧?/p>
1
|
drag.exec_(qt.moveaction) |
拖動對象的start()開始方法。
1
2
3
4
|
def mousepressevent( self , e): qpushbutton.mousepressevent( self , e) if e.button() = = qt.leftbutton: print ( 'press' ) |
如果我們使用鼠標左鍵點擊按鈕,打印 ‘press' 到控制臺。請注意,我們使用mousepressevent()方法獲取鼠標按鍵信息。
1
2
|
position = e.pos() self .button.move(position) |
在dropevent()方法中的代碼告訴我們,松開鼠標按鈕完成拖放操作。找出當前鼠標指針位置并將按鈕移動到相應的位置。
1
2
|
e.setdropaction(qt.moveaction) e.accept() |
我們指定放置動作的類型。在當前情況下,它是一個移動動作。
程序執(zhí)行后
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weiaitaowang/article/details/52131159