How do I get the Checked items in a Qlistview? - python

I've populated a Qlistview from a file, each line in the file becomes a row. Now, I'd like to have another function that creates another file from all the checked items in the qlistview. My listview goes as follows.
def show_list(self, file_in):
QListView.__init__(self)
QListView.setWindowFlags(self, QtCore.Qt.WindowStaysOnTopHint)
QListView.setWindowTitle(self, "ListView")
self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
list_view = QListView(self)
list_view.setMinimumSize(350,350)
self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.addWidget(list_view)
self.verticalLayout.addWidget(self.buttonBox)
self.buttonBox.accepted.connect(self.close)
self.buttonBox.rejected.connect(self.close)
model = QStandardItemModel(list_view)
with open(file_in) as f:
if f is not None:
item = f.readlines()
for line in item:
item = QStandardItem(line)
item.setCheckable(True)
item.setCheckState(QtCore.Qt.Unchecked)
model.appendRow(item)
list_view.setModel(model)
list_view.show()
This is my attempt so far to get my desired result. Unfortunately, It doesn't print my checked items. When called like this self.print_checked_items(model) I'm wondering what could be wrong?
def print_checked_items(self, model):
path = "/home/test1/checked.txt"
for index in range(model.rowCount()):
item = model.item(index)
if item.isCheckable() and item.checkState() == QtCore.Qt.Checked:
with open(path, "a") as f_out:
print ('%s\n' % item.text())
f_out.write('%s\n' % item.text()

When I run this in Python 3.5 and PyQt5, it works fine, it prints the correct modes and checkmarked items. I removed the file reading/writing lines for testing. For PyQt4 and Python 2.7, you should just have to fix a couple of imports and the print statement. Run it, checkmark a couple items, and after 5 seconds what do you see in console?
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QWizardPage, QListView
class AppRemovalPage(QWizardPage):
def __init__( self, parent ):
super(AppRemovalPage, self).__init__(parent)
self.setTitle('Apps to Remove')
self.setSubTitle('Listview')
self.list_view = QListView(self)
self.list_view.setMinimumSize(465, 200)
self.isWritten = False
loo = "/home/test1/file.txt"
self.model = QtGui.QStandardItemModel(self.list_view)
for line in ('a', 'b', 'c', 'd', 'e'):
self.item = QtGui.QStandardItem(line)
self.item.setCheckable(True)
self.item.setCheckState(QtCore.Qt.Unchecked)
self.model.appendRow(self.item)
self.list_view.setModel(self.model)
self.list_view.show()
def print_checked_items(self):
for index in range(self.model.rowCount()):
item = self.model.item(index)
if item.checkState() == QtCore.Qt.Checked:
if self.isWritten:
mode = "a"
else:
mode = "w"
self.isWritten = True
print ('%s' % item.text())
print("print checked items executed")
app = QApplication([])
listview = AppRemovalPage(None)
listview.show()
QTimer.singleShot(5000, listview.print_checked_items)
app.exec_()
If I checkmark a, c and d I see this:
a w
c a
d a
print checked items executed
Update to show how Python file object also works (and is in fact better code because it supports use of context management):
def print_checked_items(self):
path = "checked.txt"
mode = 'a' if self.isWritten else 'w'
if len(self.items) > 0:
with open(path, mode) as file:
for item in self.items:
print('%s' % item.text())
file.write(item.text() + "\n")
file.close()
print("print checked items executed")
The connection can be written wizard.button(QWizard.NextButton).clicked.connect(appremoval.print_checked_items)

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class AppRemovalPage(QtGui.QWizardPage):
def __init__(self, parent=None):
super(AppRemovalPage, self).__init__(parent=parent)
self.setTitle('Apps to Remove')
self.setSubTitle('Listview')
self.list_view = QtGui.QListView(self)
self.list_view.setMinimumSize(465, 200)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.list_view)
self.setLayout(layout)
self.items = []
self.isWritten = False
loo = "/home/test1/file.txt"
self.model = QtGui.QStandardItemModel(self.list_view)
self.model.itemChanged.connect(self.setItems)
file = QtCore.QFile(loo)
if file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
while not file.atEnd():
line = bytearray(file.readLine()).decode().strip()
item = QtGui.QStandardItem(line)
item.setCheckable(True)
item.setCheckState(QtCore.Qt.Unchecked)
self.model.appendRow(item)
self.list_view.setModel(self.model)
self.list_view.show()
def setItems(self, item):
if item.checkState() == QtCore.Qt.Checked:
self.items.append(item)
if item.checkState() == QtCore.Qt.Unchecked:
self.items.remove(item)
def print_checked_items(self):
path = "/home/test1/checked.txt"
mode = QtCore.QFile.Append if self.isWritten else QtCore.QFile.WriteOnly
if len(self.items) > 0:
file = QtCore.QFile(path)
if file.open(mode):
for item in self.items:
print('%s' % item.text())
file.write(item.text() + "\n")
file.close()
print("print checked items executed")
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
wizard = QtGui.QWizard()
appremoval = AppRemovalPage()
wizard.addPage(appremoval)
wizard.addPage(QtGui.QWizardPage())
wizard.button(QtGui.QWizard.NextButton).clicked.connect(appremoval.print_checked_items)
wizard.show()
sys.exit(app.exec_())
Output:
a
d
e
print checked items executed

Related

Sort QTreeWidget column containing major.minor versions [duplicate]

How can I sort a coloumn in pyqt by the highest number? Currently I have setSortingEnabled(True) and that only sorts it by the most numbers (ex. 1,1,1,1,2,2,2,3,3) i want to do it by the highest number for example (ex. 58,25,15,10). Thanks!
Data Update:
def setmydata(self):
for n, key in enumerate(self.data):
for m, item in enumerate(self.data[key]):
newitem = QtGui.QTableWidgetItem(item)
self.setItem(m, n, newitem)
Whole code:
import sys
from PyQt4.QtGui import QTableWidget
from PyQt4 import QtGui,QtCore,Qt
import MySQLdb as mdb
from functools import partial
import time
class Window(QtGui.QDialog):
process_column_signal = QtCore.pyqtSignal()
def __init__(self,parent=None):
super(Window, self).__init__()
self.layout = QtGui.QVBoxLayout(self)
self.db = mdb.connect('serv','user','pass','db')
self.model = self.db.cursor()
self.initialData = self.get_data_status()
self.table1 = MyTableStatus(self.initialData, 145, 4)
callback = partial(self.process_column,self.table1)
self.process_column_signal.connect(callback)
self.layout.addWidget(self.table1)
self.timer_status = QtCore.QTimer()
self.timer_status.timeout.connect(self.updateAllViews)
self.timer_status.timeout.connect(self.some_method)
# check every half-second
self.timer_status.start(1000*5)
def some_method(self):
self.process_column_signal.emit()
def get_data_status(self):
self.model.execute("""SELECT cpu_juliet,cpu,cpu_julietleft FROM status
WHERE date = (SELECT MAX(date) FROM status)""")
rows_status_cpu = self.model.fetchone()
self.listb1 = ['%s' % rows_status_cpu[0],'%s' % rows_status_cpu[2],'%s' % rows_status_cpu[1],'%s' % rows_status_cpu[1]]#['%s %s' % self.rows_status]
self.model.execute("""SELECT disk_queue_juliet FROM status
WHERE date = (SELECT MAX(date) FROM status)""")
rows_status_disk_queue = self.model.fetchone()
self.lista1 = 'Juliet','Julietleft','Pong','Hulk'
self.listc1 = ['%s' % rows_status_disk_queue,'%s' % rows_status_disk_queue,'%s' % rows_status_disk_queue,'%s' % rows_status_disk_queue ]
if self.listb1[0] >= '80' or self.listc1[0] >= '9':
server_status_Juliet = 'WARNING'
else:
server_status_Juliet = 'Normal'
if self.listb1[1] >= '80' or self.listc1[1] >= '9':
server_status_Julietleft = 'WARNING'
else:
server_status_Julietleft = 'Normal'
if self.listb1[2] >= '80' or self.listc1[2] >= '9':
server_status_Pong = 'WARNING'
else:
server_status_Pong = 'Normal'
if self.listb1[3] >= '80' or self.listc1[3] >= '9':
server_status_Hulk = 'WARNING'
else:
server_status_Hulk = 'Normal'
self.listd1 = ['%s' % server_status_Juliet,'%s' % server_status_Julietleft,'%s' % server_status_Pong,'%s' % server_status_Hulk]
# if server_status_Hulk == "WARNING": #or server_status_Pong == "WARNING" or server_status_Julietleft == "WARNING" or server_status_Juliet == "WARNING":
# self.serverstatus.setStyleSheet("QTabWidget {color: red}")
#status label conditions
self.mystruct1 = {'A':self.lista1, 'B':self.listb1, 'C':self.listc1, 'D':self.listd1}
return self.mystruct1
def updateAllViews(self):
_ = self.get_data_status()
self.updateTable()
def updateTable(self):
self.table1.updateFromDict(self.mystruct1)
def process_column(table1, processCol=1):
colCount = table1.table1.rowCount()
for row in xrange(table1.table1.rowCount()):
for col in xrange(4):
try:
item = table1.table1.item(row, 3)
text = item.text()
if (float(text) >= 20.0 ):
for col in xrange(colCount):
print row
item = table1.table1.item(row,col)
item.setBackground(QtGui.QBrush(QtCore.Qt.yellow))
except:
pass
class MyTableStatus(QTableWidget):
def __init__(self, thestruct, *args):
QTableWidget.__init__(self, *args)
self.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Preferred)
self.setHorizontalHeaderLabels(['Server', 'Avg. Disk Queue','CPU Load',"Status"])
self.setSortingEnabled(False)
self.data = {}
self.setmydata()
def updateFromDict(self, aDict):
self.data.clear()
self.data.update(aDict)
self.setmydata()
def setmydata(self):
for n, key in enumerate(self.data):
for m, item in enumerate(self.data[key]):
newitem = QtGui.QTableWidgetItem(item)
self.setItem(m, n, newitem)
def main():
app = QtGui.QApplication(sys.argv)
app.setStyle(QtGui.QStyleFactory.create("plastique"))
main_window = Window()
main_window.repaint()
main_window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Its sorting alpha-numerically (so, in terms of strings, '1', '10', '11', '12', '2', '20', '21', '22', '3', '4' etc. is the proper sort order. It appears that for a QTableWidgetItem, if you use the setData(Qt.EditRole, value) method, the sort order will work. Depending on your version of Qt (I assume) you may have to overload the less than method of your table widget item.
from PyQt4.QtCore import Qt, QVariant
from PyQt4.QtGui import QApplication, QTableWidget, QTableWidgetItem
class MyTableWidgetItem(QTableWidgetItem):
def __lt__(self, other):
if ( isinstance(other, QTableWidgetItem) ):
my_value, my_ok = self.data(Qt.EditRole).toInt()
other_value, other_ok = other.data(Qt.EditRole).toInt()
if ( my_ok and other_ok ):
return my_value < other_value
return super(MyTableWidgetItem, self).__lt__(other)
if ( __name__ == '__main__' ):
app = None
if ( QApplication.instance() is None ):
app = QApplication([])
widget = QTableWidget()
widget.setWindowFlags(Qt.Dialog)
widget.setSortingEnabled(True)
widget.setRowCount(50)
widget.setColumnCount(3)
for row in range(50):
# create a normal QTableWidgetItem
a = QTableWidgetItem()
a.setText(str(row))
widget.setItem(row, 0, a)
# create a proper sorted item
b = QTableWidgetItem()
b.setData(Qt.EditRole, QVariant(row))
widget.setItem(row, 1, b)
# create a custom sorted item
c = MyTableWidgetItem()
c.setData(Qt.EditRole, QVariant(row))
widget.setItem(row, 2, c)
widget.show()
if ( app ):
app.exec_()
An alternative to using a custom Item is to use a SortFilterProxy (which you probably should be doing anyway). In that case you can define a lessThan method that will be used for sorting. Here's mine:
class SortFilterProxyModel(QSortFilterProxyModel):
def lessThan(self, left_index, right_index):
left_var = left_index.data(Qt.EditRole)
right_var = right_index.data(Qt.EditRole)
try:
return float(left_var) < float(right_var)
except (ValueError, TypeError):
pass
return left_var < right_var
It uses the EditRole to get the data for sorting, so you can do whatever mapping for usability you want to do in the DisplayRole cases. It also just tries to do numerical sorting for values than can be converted to float, or native sorting otherwise. Not the most general approach, but seems to work well for me. YMMV.
Another possible solution to sort numeric could be, to add a special attribute and compare it based on that value. Mybe that is a more java like way.
class MyTableWidgetItem(QTableWidgetItem):
def __init__(self, value):
QTableWidgetItem.__init__(self)
self.value = value
def __lt__(self, other):
if isinstance(other, MyTableWidgetItem):
return self.value < other.value
return super(QTableWidgetItem, self).__lt__(other)
A simpler way is to prepend a few blank spaces to your numeric text. The ascii value of ' ' is less than '0', so it will work as you want it to. Here is what I do:
element = str(num) # convert to str
padded = (' '+element)[-5:] # make all elements the same length
item.setText(padded)

qtableview add rows from a query and edit them in python

i use Python 2.7, MariaDB and Qt4. I have a table in which i want to insert rows from a query. I tried self.model = QtSql.QSqlQueryModel() but i found that this is only read only. Then i moved to something else.
My Model() class looks like this:
class Model(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
query = QtSql.QSqlQuery()
query.prepare("SELECT denumire,pret_in,pret_out,cantitate,unitate_masura,tax FROM produse WHERE denumire='"+str(self.run_denumire())+"';")
query.exec_()
while(query.next()):
nume_produs = str(query.value(0).toString())
pret_in = str(query.value(1).toString())
pret_out = str(query.value(2).toString())
cantitate = str(query.value(3).toString())
unitate_masura = str(query.value(4).toString())
tax_tva = str(query.value(5).toString())
self.items = [nume_produs,pret_in,pret_out,cantitate,unitate_masura]
def rowCount(self, parent=QModelIndex()):
return 1
def columnCount(self, parent=QModelIndex()):
return len(self.items)
def data(self, index, role):
if not index.isValid(): return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
column=index.column()
if column<len(self.items):
return QVariant(self.items[column])
else:
return QVariant()
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
I am new to Qt and i don't know how to insert rows one by one to appear on table. What you see here is a code found by me here on stackoverflow and i modified it with a query . I need some help on this .
My main code is like this:
import time,os.path, os,module
from PyQt4 import QtGui, uic,QtSql,QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class dialog_receptie(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
file_path = os.path.abspath("ui/receptie.ui")
uic.loadUi(file_path, self)
self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center())
My ui file is:file
The file was made by Qt Designer.
Thank you.
UPDATE 1:
I inserted in class dialog_receptie a function. It looks like this:
def show_lista_receptie(self):
# self.model = QtSql.QSqlQueryModel()
# self.model.setQuery("SELECT den_produs,concat(pret_in,' Lei'),concat(pret_out,' Lei'),val_tva,cantitate,um FROM receptie_temp;")
self.model = QtSql.QSqlTableModel()
self.model.setTable("produse")
self.model.setQuery("SELECT * FROM receptie_temp;")
self.model.setHeaderData(1, QtCore.Qt.Horizontal, self.tr("Produs "))
self.model.setHeaderData(4, QtCore.Qt.Horizontal, self.tr("Pret cumparare "))
self.model.setHeaderData(5, QtCore.Qt.Horizontal, self.tr("Pret vanzare "))
self.model.setHeaderData(6, QtCore.Qt.Horizontal, self.tr("TVA Produs "))
self.model.setHeaderData(7, QtCore.Qt.Horizontal, self.tr("Cantitate "))
self.model.setHeaderData(11, QtCore.Qt.Horizontal, self.tr("UM "))
self.produse_view.setModel(self.model)
self.produse_view.hideColumn(0) # hide id column
self.produse_view.hideColumn(2)
self.produse_view.hideColumn(3)
self.produse_view.hideColumn(8)
self.produse_view.hideColumn(9)
self.produse_view.hideColumn(10)
self.produse_view.hideColumn(12)
If i use this line self.model.setQuery("SELECT * FROM receptie_temp;") i get an error message:
File "E:\onedrive\Documents\optimpos\module\receptie.py", line 64, in show_lista_receptie
self.model.setQuery("SELECT * FROM receptie_temp;")
TypeError: setQuery(self, QSqlQuery): argument 1 has unexpected type 'str'
How can query data from receptie_temp table without using an array? And how are the edited values in the table updated to the sql table?
Thank You.
I recommend using the QSqlTableModel class:
The QSqlTableModel class provides an editable data model for a single
database table.
In your case:
import os
import sys
from PyQt4 import QtGui, uic, QtSql
from PyQt4.QtGui import *
class dialog_receptie(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
file_path = os.path.abspath("ui/receptie.ui")
uic.loadUi(file_path, self)
# self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center())
db = QtSql.QSqlDatabase.addDatabase('QMYSQL')
db.setHostName(HOSTNAME);
db.setDatabaseName(DATABASE);
db.setUserName(USER);
db.setPassword(PASSWORD)
self.model = QtSql.QSqlTableModel()
self.model.setTable("produse")
self.produse_view.setModel(self.model)
self.produse_view.hideColumn(0) # hide id column
self.addData(["a", "b", "c", "d", "e", "f"])
def addData(self, data):
rec = self.model.record()
for i in range(6):
rec.setValue(rec.field(i+1).name(), data[i])
self.model.insertRecord(-1, rec)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = dialog_receptie()
w.show()
sys.exit(app.exec_())

Adding checkBox as vertical header in QtableView

I am trying to have a QTableView of checkboxes, so I can use them for row selections... I have managed to do that, now I want the header Itself to be checkbox so I can check/Uncheck All or any row . I have been looking for days, but couldn't get to do it.
I tried to use setHeaderData to the model, but couldn't do it.
Any help would be appreciated.
I wasn't particularly happy with the C++ version that #tmoreau ported to Python as it didn't:
handle more than one column
handle custom header heights (for example multi-line header text)
use a tri-state checkbox
work with sorting
So I fixed all of those issues, and created an example with a QStandardItemModel which I generally would advocate over trying to create your own model based on QAbstractTableModel.
There are probably still some imperfections, so I welcome suggestions for how to improve it!
import sys
from PyQt4 import QtCore, QtGui
# A Header supporting checkboxes to the left of the text of a subset of columns
# The subset of columns is specified by a list of column_indices at
# instantiation time
class CheckBoxHeader(QtGui.QHeaderView):
clicked=QtCore.pyqtSignal(int, bool)
_x_offset = 3
_y_offset = 0 # This value is calculated later, based on the height of the paint rect
_width = 20
_height = 20
def __init__(self, column_indices, orientation = QtCore.Qt.Horizontal, parent = None):
super(CheckBoxHeader, self).__init__(orientation, parent)
self.setResizeMode(QtGui.QHeaderView.Stretch)
self.setClickable(True)
if isinstance(column_indices, list) or isinstance(column_indices, tuple):
self.column_indices = column_indices
elif isinstance(column_indices, (int, long)):
self.column_indices = [column_indices]
else:
raise RuntimeError('column_indices must be a list, tuple or integer')
self.isChecked = {}
for column in self.column_indices:
self.isChecked[column] = 0
def paintSection(self, painter, rect, logicalIndex):
painter.save()
super(CheckBoxHeader, self).paintSection(painter, rect, logicalIndex)
painter.restore()
#
self._y_offset = int((rect.height()-self._width)/2.)
if logicalIndex in self.column_indices:
option = QtGui.QStyleOptionButton()
option.rect = QtCore.QRect(rect.x() + self._x_offset, rect.y() + self._y_offset, self._width, self._height)
option.state = QtGui.QStyle.State_Enabled | QtGui.QStyle.State_Active
if self.isChecked[logicalIndex] == 2:
option.state |= QtGui.QStyle.State_NoChange
elif self.isChecked[logicalIndex]:
option.state |= QtGui.QStyle.State_On
else:
option.state |= QtGui.QStyle.State_Off
self.style().drawControl(QtGui.QStyle.CE_CheckBox,option,painter)
def updateCheckState(self, index, state):
self.isChecked[index] = state
self.viewport().update()
def mousePressEvent(self, event):
index = self.logicalIndexAt(event.pos())
if 0 <= index < self.count():
x = self.sectionPosition(index)
if x + self._x_offset < event.pos().x() < x + self._x_offset + self._width and self._y_offset < event.pos().y() < self._y_offset + self._height:
if self.isChecked[index] == 1:
self.isChecked[index] = 0
else:
self.isChecked[index] = 1
self.clicked.emit(index, self.isChecked[index])
self.viewport().update()
else:
super(CheckBoxHeader, self).mousePressEvent(event)
else:
super(CheckBoxHeader, self).mousePressEvent(event)
if __name__=='__main__':
def updateModel(index, state):
for i in range(model.rowCount()):
item = model.item(i, index)
item.setCheckState(QtCore.Qt.Checked if state else QtCore.Qt.Unchecked)
def modelChanged():
for i in range(model.columnCount()):
checked = 0
unchecked = 0
for j in range(model.rowCount()):
if model.item(j,i).checkState() == QtCore.Qt.Checked:
checked += 1
elif model.item(j,i).checkState() == QtCore.Qt.Unchecked:
unchecked += 1
if checked and unchecked:
header.updateCheckState(i, 2)
elif checked:
header.updateCheckState(i, 1)
else:
header.updateCheckState(i, 0)
app = QtGui.QApplication(sys.argv)
tableView = QtGui.QTableView()
model = QtGui.QStandardItemModel()
model.itemChanged.connect(modelChanged)
model.setHorizontalHeaderLabels(['Title 1\nA Second Line','Title 2'])
header = CheckBoxHeader([0,1], parent = tableView)
header.clicked.connect(updateModel)
# populate the models with some items
for i in range(3):
item1 = QtGui.QStandardItem('Item %d'%i)
item1.setCheckable(True)
item2 = QtGui.QStandardItem('Another Checkbox %d'%i)
item2.setCheckable(True)
model.appendRow([item1, item2])
tableView.setModel(model)
tableView.setHorizontalHeader(header)
tableView.setSortingEnabled(True)
tableView.show()
sys.exit(app.exec_())
I had the same issue, and find a solution here, in C++. There is no easy solution, you have to create your own header.
Here's my full code with PyQt4. It seems to work with Python2 and Python3.
I also implemented the select all / select none functionality.
import sys
import signal
#import QT
from PyQt4 import QtCore,QtGui
#---------------------------------------------------------------------------------------------------------
# Custom checkbox header
#---------------------------------------------------------------------------------------------------------
#Draw a CheckBox to the left of the first column
#Emit clicked when checked/unchecked
class CheckBoxHeader(QtGui.QHeaderView):
clicked=QtCore.pyqtSignal(bool)
def __init__(self,orientation=QtCore.Qt.Horizontal,parent=None):
super(CheckBoxHeader,self).__init__(orientation,parent)
self.setResizeMode(QtGui.QHeaderView.Stretch)
self.isChecked=False
def paintSection(self,painter,rect,logicalIndex):
painter.save()
super(CheckBoxHeader,self).paintSection(painter,rect,logicalIndex)
painter.restore()
if logicalIndex==0:
option=QtGui.QStyleOptionButton()
option.rect= QtCore.QRect(3,1,20,20) #may have to be adapt
option.state=QtGui.QStyle.State_Enabled | QtGui.QStyle.State_Active
if self.isChecked:
option.state|=QtGui.QStyle.State_On
else:
option.state|=QtGui.QStyle.State_Off
self.style().drawControl(QtGui.QStyle.CE_CheckBox,option,painter)
def mousePressEvent(self,event):
if self.isChecked:
self.isChecked=False
else:
self.isChecked=True
self.clicked.emit(self.isChecked)
self.viewport().update()
#---------------------------------------------------------------------------------------------------------
# Table Model, with checkBoxed on the left
#---------------------------------------------------------------------------------------------------------
#On row in the table
class RowObject(object):
def __init__(self):
self.col0="column 0"
self.col1="column 1"
class Model(QtCore.QAbstractTableModel):
def __init__(self,parent=None):
super(Model,self).__init__(parent)
#Model= list of object
self.myList=[RowObject(),RowObject()]
#Keep track of which object are checked
self.checkList=[]
def rowCount(self,QModelIndex):
return len(self.myList)
def columnCount(self,QModelIndex):
return 2
def addOneRow(self,rowObject):
frow=len(self.myList)
self.beginInsertRows(QtCore.QModelIndex(),row,row)
self.myList.append(rowObject)
self.endInsertRows()
def data(self,index,role):
row=index.row()
col=index.column()
if role==QtCore.Qt.DisplayRole:
if col==0:
return self.myList[row].col0
if col==1:
return self.myList[row].col1
elif role==QtCore.Qt.CheckStateRole:
if col==0:
if self.myList[row] in self.checkList:
return QtCore.Qt.Checked
else:
return QtCore.Qt.Unchecked
def setData(self,index,value,role):
row=index.row()
col=index.column()
if role==QtCore.Qt.CheckStateRole and col==0:
rowObject=self.myList[row]
if rowObject in self.checkList:
self.checkList.remove(rowObject)
else:
self.checkList.append(rowObject)
index=self.index(row,col+1)
self.dataChanged.emit(index,index)
return True
def flags(self,index):
if index.column()==0:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsUserCheckable
return QtCore.Qt.ItemIsEnabled
def headerData(self,section,orientation,role):
if role==QtCore.Qt.DisplayRole:
if orientation==QtCore.Qt.Horizontal:
if section==0:
return "Title 1"
elif section==1:
return "Title 2"
def headerClick(self,isCheck):
self.beginResetModel()
if isCheck:
self.checkList=self.myList[:]
else:
self.checkList=[]
self.endResetModel()
if __name__=='__main__':
app=QtGui.QApplication(sys.argv)
#to be able to close with ctrl+c
signal.signal(signal.SIGINT, signal.SIG_DFL)
tableView=QtGui.QTableView()
model=Model(parent=tableView)
header=CheckBoxHeader(parent=tableView)
header.clicked.connect(model.headerClick)
tableView.setModel(model)
tableView.setHorizontalHeader(header)
tableView.show()
sys.exit(app.exec_())
NB: You could store the rows in self.checkList. In my case, I often have to delete rows in random position so it was not sufficient.

Insert and remove items from a QAbstractListModel

I am trying to create a QAbstractListView for use with a QComboBox which maintains a sorted list of the items it contains. I've included some sample code below that illustrates my problem. When I update the items in the list, the currentIndex of the combo box does not update to reflect the changes to the model. I've tried using the rowsAboutToBeInserted and rowsInserted signals, but I can't see any effect (maybe I'm doing it wrong?).
My actual use case is a little more complex, but the example should be enough. The items being sorted aren't just strings, and take a little more effort to sort and have a ItemDataRole different to their DisplayRole.
The itemsAdded and itemsRemoved are my own functions, which will be connected to signals from another list which I'm trying to proxy.
To trigger the problem, press the 'Insert "c"' button. The string is inserted into the list correctly, but the selection moves from 'e' to 'd' (i.e. the selection index does not change).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class Model(QtCore.QAbstractListModel):
def __init__(self, *args, **kwargs):
QtCore.QAbstractListModel.__init__(self, *args, **kwargs)
self.items = []
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.items)
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid() is True:
if role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.items[index.row()])
elif role == QtCore.Qt.ItemDataRole:
return QtCore.QVariant(self.items[index.row()])
return QtCore.QVariant()
def itemsAdded(self, items):
# insert items into their sorted position
items = sorted(items)
row = 0
while row < len(self.items) and len(items) > 0:
if items[0] < self.items[row]:
self.items[row:row] = [items.pop(0)]
row += 1
row += 1
# add remaining items to end of list
if len(items) > 0:
self.items.extend(items)
def itemsRemoved(self, items):
# remove items from list
for item in items:
for row in range(0, len(self.items)):
if self.items[row] == item:
self.items.pop(row)
break
def main():
app = QtGui.QApplication([])
w = QtGui.QWidget()
w.resize(300,300)
layout = QtGui.QVBoxLayout()
model = Model()
model.itemsAdded(['a','b','d','e'])
combobox = QtGui.QComboBox()
combobox.setModel(model)
combobox.setCurrentIndex(3)
layout.addWidget(combobox)
def insertC(self):
model.itemsAdded('c')
button = QtGui.QPushButton('Insert "c"')
button.clicked.connect(insertC)
layout.addWidget(button)
w.setLayout(layout)
w.show()
app.exec_()
if __name__ == '__main__':
main()
A complete working example below, based on Tim's answer.
The call to setCurrentIndex isn't required. The view keeps track of this automatically when insertRows/removeRows are called correctly.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class Model(QtCore.QAbstractListModel):
def __init__(self, *args, **kwargs):
QtCore.QAbstractListModel.__init__(self, *args, **kwargs)
self.items = []
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.items)
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid() is True:
if role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.items[index.row()])
elif role == QtCore.Qt.ItemDataRole:
return QtCore.QVariant(self.items[index.row()])
return QtCore.QVariant()
def itemsAdded(self, items):
# insert items into their sorted position
items = sorted(items)
row = 0
while row < len(self.items) and len(items) > 0:
if items[0] < self.items[row]:
self.beginInsertRows(QtCore.QModelIndex(), row, row)
self.items.insert(row, items.pop(0))
self.endInsertRows()
row += 1
row += 1
# add remaining items to end of the list
if len(items) > 0:
self.beginInsertRows(QtCore.QModelIndex(), len(self.items), len(self.items) + len(items) - 1)
self.items.extend(items)
self.endInsertRows()
def itemsRemoved(self, items):
# remove items from the list
for item in items:
for row in range(0, len(self.items)):
if self.items[row] == item:
self.beginRemoveRows(QtCore.QModelIndex(), row, row)
self.items.pop(row)
self.endRemoveRows()
break
def main():
app = QtGui.QApplication([])
w = QtGui.QWidget()
w.resize(300,200)
layout = QtGui.QVBoxLayout()
model = Model()
model.itemsAdded(['a','b','d','e'])
combobox = QtGui.QComboBox()
combobox.setModel(model)
combobox.setCurrentIndex(3)
layout.addWidget(combobox)
def insertC(self):
model.itemsAdded('c')
def removeC(self):
model.itemsRemoved('c')
buttonInsert = QtGui.QPushButton('Insert "c"')
buttonInsert.clicked.connect(insertC)
layout.addWidget(buttonInsert)
buttonRemove = QtGui.QPushButton('Remove "c"')
buttonRemove.clicked.connect(removeC)
layout.addWidget(buttonRemove)
w.setLayout(layout)
w.show()
app.exec_()
if __name__ == '__main__':
main()
I guess you need to modify the selection index yourself, i.e. something like
if row < currentIndex():
setCurrentIndex( currentIndex() + 1 );
You should however read the following passage:
Models that provide interfaces to resizable list-like data structures can provide implementations of insertRows() and removeRows(). When implementing these functions, it is important to call the appropriate functions so that all connected views are aware of any changes:
• An insertRows() implementation must call beginInsertRows() before inserting new rows into the data structure, and it must call endInsertRows() immediately afterwards.
• A removeRows() implementation must call beginRemoveRows() before the rows are removed from the data structure, and it must call endRemoveRows() immediately afterwards.

How to get item selected from QListView in PyQt

I'm new to PyQt. So I'm trying to get selected item from QListView, I'm able to get selected items's index, but I'm not able to get the value of the index, can please someone help me.
Here is the code :
import sys
import os
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class asset(QtGui.QDialog):
def __init__(self,parent=None):
super(asset, self).__init__(parent)
self.assetList = QtGui.QListView(self)
self.assetList.clicked.connect(self.on_treeView_clicked)
######################################################################
# ----------------- ADD ITEMS----------------------------------------
######################################################################
list_data = listDirs('D:\\')
dir = listModel(list_data)
self.assetList.setModel(dir)
self.setStyleSheet('''
*{
background-color : rgb(65,65,65);
color : rgb(210,210,210);
alternate-background-color:rgb(55,55,55);
}
QTreeView,QListView,QLineEdit{
background-color : rgb(50,50,50);
color : rgb(210,210,210);
}
'''
)
self.setFocus()
#QtCore.pyqtSlot(QtCore.QModelIndex)
def on_treeView_clicked(self, index):
itms = self.assetList.selectedIndexes()
for it in itms:
print 'selected item index found at %s' % it.row()
class listModel(QAbstractListModel):
def __init__(self, datain, parent=None, *args):
""" datain: a list where each item is a row
"""
QAbstractListModel.__init__(self, parent, *args)
self.listdata = datain
def rowCount(self, parent=QModelIndex()):
return len(self.listdata)
def data(self, index, role):
if index.isValid() and role == Qt.DisplayRole:
return QVariant(self.listdata[index.row()])
else:
return QVariant()
def listDirs(*path):
completePath = os.path.join(*path)
dirs = os.listdir(os.path.abspath(completePath))
outputDir = []
for dir in dirs:
if os.path.isdir(os.path.join(completePath,dir)):
outputDir.append(dir)
return outputDir
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setStyle('plastique')
main = asset()
main.resize(200,200)
main.show()
sys.exit(app.exec_())
Thanks !
You can use the convenience method data of QModelIndex. It returns a QVariant. Just convert it to something you'd use, like a QString with .toString:
print 'selected item index found at %s with data: %s' % (it.row(), it.data().toString())
By the way, QListView.clicked will give you the index. Unless you have multiple selection, or override the default selection behavior, it will be the only selected item. You don't need to loop over selectedIndexes():
#QtCore.pyqtSlot(QtCore.QModelIndex)
def on_treeView_clicked(self, index):
print 'selected item index found at %s with data: %s' % (index.row(), index.data().toString())

Categories