Check multiple items at the same time for pyqt (learning offered by netizens)

Source: Internet
Author: User

After selecting multiple items, you can select/to select multiple items with the Space key, as shown in the following figure:

Http://oglop.gitbooks.io/pyqt-pyside-cookbook/list/img/checkbox_multi_toggle.gif

 

#-*-Coding: UTF-8 -*-

# Python: 2.x

_ Author _ = 'admin'

From pyqt4.qt import *

From pyqt4.qtcore import *

From pyqt4.qtgui import *

Import OS, sys

Class th (qlistwidget ):

Def _ init _ (self, types, parent = none ):

Super (Th, self). _ init _ (parent)

Self. seticonsize (qsize (124,124) # Set the Icon size

Self. setselectionmode (q1_actitemview. extendedselection) # The description is as follows:

Self. setacceptdrops (true) # setacceptdrops (bool)

Self. setselectionrectvisible (true)

Def keypressevent (self, e ):

If E. Key () = QT. key_space:

If self. selecteditems ():

New_state = QT. unchecked if self. selecteditems () [0]. checkstate () else QT. Checked

For item in self. selecteditems ():

If item. Flags () & QT. itemisusercheckable:

Item. setcheckstate (new_state)

Self. viewport (). Update ()

Elif E. Key () = QT. key_delete:

For item in self. selecteditems ():

Self. takeitem (self. Row (item ))

Def iterallitems (Self ):

For I in range (self. Count ()):

Yield self. Item (I)

Class DIA (qmainwindow ):

Def _ init _ (Self ):

Super (Dia, self). _ init __()

Self. listitems = []

Myqwidget = qwidget ()

Boxlayout = qvboxlayout ()

Myqwidget. setlayout (boxlayout)

Self. setcentralwidget (myqwidget)

 

Self. listwidgeta = th (Self)

For I in range (5 ):

Qlistwidgetitem ('item' + STR (I), self. listwidgeta)

 

For item in self. listwidgeta. iterallitems ():

Item. setflags (item. Flags () | QT. itemisusercheckable) # optional items for setting listwidget

Item. setcheckstate (QT. Checked) # assign an initial value. All entries are selected.

Boxlayout. addwidget (self. listwidgeta)

Self. listwidgeta. setacceptdrops (false)

Self. listwidgeta. viewport (). Update () # The default window rectangle is the same as the device rectangle.

 

If _ name __= = '_ main __':

APP = qapplication (SYS. argv)

D = DIA ()

D. Show ()

D. Resize (400,140)

Sys.exit(app.exe C _())

# Description

"""

Listwidget. setselectionmode (qtgui. qw.actitemview. extendedselection) # Set the selection mode

Select mode: extendedselection press Ctrl multiple select, singleselection single choice multiselection click multiple select contiguousselection drag multiple select

 

Setacceptdrops (bool)

Set the component to accept the drag-and-drop action. When the drag-and-drop action occurs, corresponding events such as qdragenterevent, qdragmoveevent, qdragleaveevent, and qdropevent occur. You can redefine dragenterevent () and dragmoveevent (Event (), dragleaveevent (), dropevent (), and other event handlers,

To process the corresponding drag-and-drop events, dragenterevent () and dropevent () are usually used ().

More instructions for accessing documents or viewing (http://blog.csdn.net/jiong_1988/article/details/7494607)

 

Selecteditems () method to obtain the selected items, which can be multiple

 

Setcentralwidget () sets a widget as the central widget of the main window, and the central widget means that it occupies the central position of the main window during display.

"""

Check multiple items at the same time for pyqt (learning offered by netizens)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.