Listconfirm [qtablewidget] [Form validation GUI]

Source: Internet
Author: User

# Coding = GBK

From pyqt4 import qtgui, qtcore

#-------------------------------------------------------------------

# Listconfirm provides a GUI interface for responding to or confirming list items

#-------------------------------------------------------------------

Class listconfirm (qtgui. qwidget ):

U''' listconfirm provides a GUI interface for responding to or confirming the list items '''

Def _ init _ (self, items, choices = none, defaultchoiceindex = 0,

Ctltype = qtgui. qradiobutton, headers = none, Title = '', parent = none ):

Self. APP = qtgui. qapplication ([])

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

# Parameter inspection and Processing

If choices is none:

Choices = ('yes', 'no ')

Else:

If type (choices) not in (list, tuple) or Len (choices) <2:

Raise typeerror ('argument choices must be like' +

'[Choice1, choice2...] Or (choice1, choice2...)' +

', And its length must not less than 2 .')

Else:

Choices = tuple (choices)

If type (defaultchoiceindex )! = Int or \

Defaultchoiceindex <0 or defaultchoiceindex> Len (choices)-1:

Raise typeerror ('argument ultultchoiceindex must be a integer '+

'Object and 0 <= defaultchoiceindex <= Len (choices)-1 ')

Arg_items_typeerror = 'argument items must be like' + \

'[(Item1, CI), (item2, CI)...] or' + \

'[[Item1, CI], [item2, CI]...] or' + \

'Simply [Item1, item2. ..] Or (Item1, item2....). '+ \

'Ci means choiceindex .'

Self. Items, self. Choices, self. Groups = [], [], []

If all (MAP (lambda X: type (x) not in (tuple, list), items )):

Self. Items = List (items)

Self. Choices = [defaultchoiceindex for I in range (LEN (items)]

Elif all (MAP (lambda X: type (x) in (tuple, list), items )):

If all (MAP (lambda X: Len (x) = 2, items )):

For X in items:

Self. Items. append (X [0])

If type (X [1])! = Int:

Raise typeerror ('bad choice index. It must be a int object .')

Else:

If not 0 <= x [1] <= Len (choices)-1:

Raise valueerror ('bad choice index. Its value' +

'Is out of the range ')

Else:

Self. Choices. append (X [1])

Else:

Raise typeerror (arg_items_typeerror)

Else:

Raise typeerror (arg_items_typeerror)

If ctltype not in (qtgui. qradiobutton, qtgui. qcombobox ):

Raise valueerror ('argument ctltype must be a STR whose value' +

"Is 'combobox' or 'radiobutton '.")

If headers is not none:

If type (headers) not in (tuple, list) or Len (headers )! = 2:

Raise typeerror ('argument headers must be like' +

'[Header1, header2. ..] Or (header1, header2....)' +

', And its length must be 2 .')

Else:

If Len (choices) = 2:

Headers = ('item', 'state ')

Else:

Headers = ('item', 'choice ')

If title = '': Title = 'list item response or confirmation window '. Decode ('gbk ')

Self. setwindowtitle (title)

Screen = qtgui. q1_topwidget (). availablegeometry (0)

Self. setgeometry (screen. Width ()/4-1,

Screen. Height ()/5-1,

Screen. Width () * 1/3,

Screen. Height () * 3/5

)

# Layv

Layv = qtgui. qvboxlayout ()

Self. setlayout (layv)

# Self.tw

Self.tw = qtgui. qtablewidget (LEN (items), 2, self)

Layv. addwidget (self.tw)

Self.tw. sethorizontalheaderlabels (headers)

For I in range (LEN (items )):

Self.tw. setitem (I, 0, qtgui. qtablewidgetitem (self. items [I])

If ctltype = qtgui. qcombobox:

CTL = ctltype ()

CTL. seteditable (false)

CTL. additems (choices)

CTL. setcurrentindex (defaultchoiceindex)

Self.tw. setcellwidget (I, 1, CTL)

Else:

Gp = qtgui. qgroupbox ()

Layhgroup = qtgui. qhboxlayout ()

GP. setlayout (layhgroup)

For J in range (LEN (choices )):

CTL = ctltype ()

Layhgroup. addwidget (CTL)

CTL. settext (choices [J])

If J = defachochoiceindex:

CTL. setchecked (true)

Else:

CTL. setchecked (false)

Self.tw. setcellwidget (I, 1, GP)

Self.tw. setrowheight (I, 35)

Self.tw. resizecolumnstocontents ()

If self.tw. columnwidth (0) + self.tw. columnwidth (1) <self. Width ()-50:

Self.tw. setcolumnwidth (0, self. Width () -self.tw. columnwidth (1)-50)

# Layh

Layh = qtgui. qhboxlayout ()

Layv. addlayout (layh)

# Self. btnok

Self. btnok = qtgui. qpushbutton ('confirmed'. Decode ('gbk '))

Self. btnok. clicked. Connect (self. btnok_clicked)

Layh. addwidget (self. btnok)

# Self. btncancel

Self. btncancel = qtgui. qpushbutton ('cancel'. Decode ('gbk '))

Self. btncancel. clicked. Connect (self. btncancel_clicked)

Layh. addwidget (self. btncancel)

Self. Show ()

Self.app.exe C _()

Def btnok_clicked (self, event ):

For I in range (LEN (self. Items )):

CTL = self.tw. cellwidget (I, 1)

If type (CTL) = qtgui. qcombobox:

Self. Choices [I] = CBB. currentindex ()

Else:

Lay = CTL. layout ()

For J in range (lay. Count ()):

CB = lay. itemat (j). widget ()

If CB. ischecked ():

Self. Choices [I] = J

Self. Close ()

Def btncancel_clicked (self, event ):

Self. Close ()

 

 

 

Def listconfirm_test ():

Items = ['(1) Smart module import'. Decode ('gbk '),

'(2) network inspection'. Decode ('gbk '),

'(3) module download'. Decode ('gbk '),

'(4) module installation'. Decode ('gbk '),

'(5) software download'. Decode ('gbk '),

'(6) software installation'. Decode ('gbk '),

'(7) Get Rid Of All hardcoded'. Decode ('gbk '),

'(8) departure file dump'. Decode ('gbk ')

]

Headers = ['task', 'done']

Choices = ('yes', 'no ')

Defaultchoiceindex = 1

Title = 'fill in the task completion report '. Decode ('gbk ')

Print listconfirm (items, choices, 1, qtgui. qradiobutton, headers, title). Choices

 

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

Listconfirm_test ()

 

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.