PYQT tableviewbug (User-supplied)

Source: Internet
Author: User

#!/usr/bin/env python

#-*-Coding:utf8-*-

# Some imports:

From __future__ import print_function

Import Sys

# Display Infos:

Pythonversion = sys.version_info[0] * ten + sys.version_info[1]

Print (' pythonversion: ', pythonversion)

# PyQt or Pyside?

Pyside = False

If not ("PYQT" in SYS.ARGV):

Try

From pyside import Qtcore, Qtgui

Pyside = True

Except

Pass

If Pyside:

Import Pyside

Print (' Pyside ', pyside.__version__)

Else

Import SIP

Sip.setapi (' QString ', 2)

Sip.setapi (' Qvariant ', 2)

From PYQT4 import Qtcore, Qtgui

Print (' PYQT ', qtcore.pyqt_version_str)

# Display Qt version:

Qtversion = Qtcore.qversion ()

Print (' qtversion: ', qtversion)

Class Testmodel (Qtgui.qstandarditemmodel):

def __init__ (self, Parent=none):

Super (Testmodel, self). __init__ (parent)

Self.headers = [' id ', ' FirstName ', ' LastName ']

Self.datas = []

Self.datas.append ([' 101 ', ' Danny ', ' Young ')

Self.datas.append ([' 102 ', ' Christine ', ' Holand '])

Self.datas.append ([' 103 ', ' Lars ', ' Gordon '])

Self.datas.append ([' 104 ', ' Roberto ', ' Robitaille '])

Self.datas.append ([' The ' ", ' Maria ', ' Papadopoulos ')

Self.emptyrow = []

Nbcols = Len (self.headers)

Self.setcolumncount (Nbcols)

Nbrows = Len (self.datas)

Self.setrowcount (Nbrows)

For Col in Range (Nbcols):

Self.setheaderdata (col, QtCore.Qt.Horizontal, Self.headers[col])

For row in range (nbrows):

For Col in Range (Nbcols):

data = Self.datas[row][col]

Self.setdata (Self.index (Row, col, Qtcore.qmodelindex ()), data)

def data (self, Index, role=qtcore.qt.displayrole):

column, row = Index.column (), Index.row ()

if role = = QtCore.Qt.TextAlignmentRole:

If column > 0:

Return QtCore.Qt.AlignLeft

Else

Return QtCore.Qt.AlignRight

elif role = = QtCore.Qt.DisplayRole:

return Self.datas[row][column]

Return

def columnCount (self, Parent=qtcore.qmodelindex ()):

Return Len (self.headers)

def rowCount (self, Parent=qtcore.qmodelindex ()):

Return Len (Self.datas)

Class MainForm (Qtgui.qdialog):

def __init__ (self, Parent=none):

Super (MainForm, self). __init__ (parent)

Self.model = Testmodel ()

Self.tableview = Qtgui.qtableview ()

Self.tableView.setModel (Self.model)

Insertrowbutton = Qtgui.qpushbutton ("InsertRow")

InsertRowButton.clicked.connect (Self.insertrow)

Removerowbugbutton = Qtgui.qpushbutton ("Removerowbug")

RemoveRowBugButton.clicked.connect (Self.removerowbug)

Removerowbutton = Qtgui.qpushbutton ("Removerow")

RemoveRowButton.clicked.connect (Self.removerow)

Quitbutton = Qtgui.qpushbutton ("Quit")

QuitButton.clicked.connect (self.accept)

Buttonlayout = Qtgui.qhboxlayout ()

Buttonlayout.addwidget (Insertrowbutton)

Buttonlayout.addwidget (Removerowbugbutton)

Buttonlayout.addwidget (Removerowbutton)

Buttonlayout.addstretch ()

Buttonlayout.addwidget (Quitbutton)

Splitter = Qtgui.qsplitter (QtCore.Qt.Horizontal)

Self.vbox = Qtgui.qvboxlayout ()

Self.vbox.addWidget (Self.tableview)

Widget = Qtgui.qwidget ()

Widget.setlayout (Self.vbox)

Splitter.addwidget (widget)

Layout = Qtgui.qvboxlayout ()

Layout.addwidget (Splitter)

Layout.addlayout (Buttonlayout)

Self.setlayout (Layout)

Self.setminimumwidth (400)

def insertRow (self):

index = Self.tableView.currentIndex ()

If Index.isvalid ():

row = Index.row ()

Column = Index.column ()

Else

Row, column = 0, 0

If Self.model.insertRow (row):

Emptyrow = [' 999 ', ' AAA ', ' zzzzzzzzzzz ']

Row + = 1

Self.model.datas.insert (row, Emptyrow)

index = Self.model.index (row, column)

Self.tableView.setFocus ()

Self.tableView.setCurrentIndex (Index)

def removerowbug (self):

index = Self.tableView.currentIndex ()

If not Index.isvalid ():

Return

row = Index.row ()

Column = Index.column ()

if (Self.model.removeRow (row)):

Self.model.datas.pop (Row)

If row > Self.model.rowCount ()-1:

Row-= 1

# Qtableview.verticalheader is not repaint

# This for verify Verticalheader:

Print (Self.model.rowCount (), Self.tableView.verticalHeader (). Count ())

# I test that and nothing is OK:

#self. Tableview.resizerowstocontents ()

#self. Tableview.verticalheader (). Reset ()

#self. Tableview.verticalheader (). Repaint ()

QtCore.QCoreApplication.processEvents ()

index = Self.model.index (row, column)

Self.tableView.setFocus ()

Self.tableView.setCurrentIndex (Index)

def removerow (self):

index = Self.tableView.currentIndex ()

If not Index.isvalid ():

Return

row = Index.row ()

Column = Index.column ()

if (Self.model.removeRow (row)):

Self.model.datas.pop (Row)

If row > Self.model.rowCount ()-1:

Row-= 1

# Recreate the TableView solve the bug, but isn't good:

Self.vbox.removeWidget (Self.tableview)

Self.tableView.setModel (None)

Del Self.tableview

Self.tableview = Qtgui.qtableview ()

Self.tableView.setModel (Self.model)

Self.vbox.addWidget (Self.tableview)

index = Self.model.index (row, column)

Self.tableView.setFocus ()

Self.tableView.setCurrentIndex (Index)

if __name__ = = ' __main__ ':

App = Qtgui.qapplication (SYS.ARGV)

form = MainForm ()

Form.show ()

Sys.exit (App.exec_ ())

PYQT tableviewbug (User-supplied)

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.