Pyqt qsortfilterproxymodel (provided by netizens)

Source: Internet
Author: User

#! /Usr/bin/ENV Python

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

 

From pyqt4 import qtcore, qtgui

 

Class mywindow (qtgui. qmainwindow ):

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

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

Self. centralwidget = qtgui. qwidget (Self)

Self. lineedit = qtgui. qlineedit (self. centralwidget)

Self. view = qtgui. qtableview (self. centralwidget)

Self. ComboBox = qtgui. qcombobox (self. centralwidget)

Self. Label = qtgui. qlabel (self. centralwidget)

 

Self. gridlayout = qtgui. qgridlayout (self. centralwidget)

Self. gridlayout. addwidget (self. lineedit, 0, 1, 1, 1)

Self. gridlayout. addwidget (self. View, 1, 0, 1, 3)

Self. gridlayout. addwidget (self. ComboBox, 0, 2, 1, 1)

Self. gridlayout. addwidget (self. Label, 0, 0, 1, 1)

 

Self. setcentralwidget (self. centralwidget)

Self. Label. settext ("RegEx filter ")

 

Self. Model = qtgui. qstandarditemmodel (Self)

 

For rowname in range (3) * 5:

Self. model. invisiblerootitem (). appendrow (

[Qtgui. qstandarditem ("Row {0} Col {1}". Format (rowname, column ))

For column in range (3)

]

)

 

Self. Proxy = qtgui. qsortfilterproxymodel (Self)

Self. Proxy. setsourcemodel (self. Model)

 

Self. View. setmodel (self. Proxy)

Self. ComboBox. additems (["column {0}". Format (x) for X in range (self. model. columncount ()])

 

Self. lineedit. textchanged. Connect (self. on_lineedit_textchanged)

Self. ComboBox. currentindexchanged. Connect (self. on_combobox_currentindexchanged)

 

Self. horizontalheader = self. View. horizontalheader ()

Self. horizontalheader. sectionclicked. Connect (self. on_view_horizontalheader_sectionclicked)

 

@ Qtcore. pyqtslot (INT)

Def on_view_horizontalheader_sectionclicked (self, logicalindex ):

Self. logicalindex = logicalindex

Self. menuvalues = qtgui. qmenu (Self)

Self. signalmapper = qtcore. qsignalmapper (Self)

 

Self. ComboBox. blocksignals (true)

Self. ComboBox. setcurrentindex (self. logicalindex)

Self. ComboBox. blocksignals (true)

 

Valuesunique = [self. model. Item (row, self. logicalindex). Text ()

For row in range (self. model. rowcount ())

]

 

Actionall = qtgui. qaction ("all", self)

Actionall. triggered. Connect (self. on_actionall_triggered)

Self. menuvalues. addaction (actionall)

Self. menuvalues. addseparator ()

 

For actionnumber, actionname in enumerate (sorted (List (SET (valuesunique )))):

Action = qtgui. qaction (actionname, self)

Self. signalmapper. setmapping (action, actionnumber)

Action. triggered. Connect (self. signalmapper. map)

Self. menuvalues. addaction (Action)

 

Self. signalmapper. mapped. Connect (self. on_signalmapper_mapped)

 

Headerpos = self. View. maptoglobal (self. horizontalheader. pos ())

 

Posy = headerpos. Y () + self. horizontalheader. Height ()

Posx = headerpos. X () + self. horizontalheader. sectionposition (self. logicalindex)

 

Self.menuvalues.exe C _ (qtcore. qpoint (posx, Posy ))

 

@ Qtcore. pyqtslot ()

Def on_actionall_triggered (Self ):

Filtercolumn = self. logicalindex

Filterstring = qtcore. qregexp ("",

Qtcore. QT. caseinsensitive,

Qtcore. qregexp. Regexp

)

 

Self. Proxy. setfilterregexp (filterstring)

Self. Proxy. setfilterkeycolumn (filtercolumn)

 

@ Qtcore. pyqtslot (INT)

Def on_signalmapper_mapped (self, I ):

Stringaction = self. signalmapper. Mapping (I). Text ()

Filtercolumn = self. logicalindex

Filterstring = qtcore. qregexp (stringaction,

Qtcore. QT. casesensitive,

Qtcore. qregexp. fixedstring

)

 

Self. Proxy. setfilterregexp (filterstring)

Self. Proxy. setfilterkeycolumn (filtercolumn)

 

@ Qtcore. pyqtslot (STR)

Def on_lineedit_textchanged (self, text ):

Search = qtcore. qregexp (text,

Qtcore. QT. caseinsensitive,

Qtcore. qregexp. Regexp

)

 

Self. Proxy. setfilterregexp (Search)

 

@ Qtcore. pyqtslot (INT)

Def on_combobox_currentindexchanged (self, index ):

Self. Proxy. setfilterkeycolumn (INDEX)

 

 

If _ name _ = "_ main __":

Import sys

 

APP = qtgui. qapplication (SYS. argv)

Main = mywindow ()

Main. Show ()

Main. Resize (400,600)

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

:

Pyqt qsortfilterproxymodel (provided 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.