[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: [PyQt] Why am I not able to check the checkboxes
From: Zoltan Szalai <defaultdict () gmail ! com>
Date: 2011-03-26 9:42:37
Message-ID: 4D8DB50D.3040002 () gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
hi,
you always return Qt.Unchecked for Qt.CheckStateRole in the data method ...
try the attached code.
bests
Zoltan
On 2011.03.21. 6:21, vijay swaminathan wrote:
> Hi All,
>
> I tried creating a treeview with checkboxes but I'm unable to select
> the checkboxes.
>
> on the flag method I had mentioned it as ItemisuserCheckable but still
> could not get it working...
>
> am I missing something here to enable the selection of checkboxes.
>
> A snippet of the code is:
>
> import sys
>
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
>
>
>
>
> class StbTreeView(QAbstractListModel):
> def __init__(self, args, parent=None):
> super(StbTreeView, self).__init__(parent)
>
> self.args = args
> print self.args
>
>
> def rowCount(self, parent):
> return len(self.args)
>
> def headerData(self, section, orientation, role):
> if role == Qt.DisplayRole:
> if orientation == Qt.Horizontal:
> return QString("Select STB's")
>
> def flags(self, index):
> row = index.row()
> if row:
> return Qt.ItemIsUserCheckable | Qt.ItemIsEnabled |
> Qt.ItemIsEditable | Qt.ItemIsSelectable
>
> def data(self, index, role=Qt.DisplayRole):
>
> if role == Qt.DisplayRole:
> row = index.row()
> return self.args[row]
>
> if role == Qt.CheckStateRole:
>
> row = index.row()
> return QVariant(Qt.Unchecked)
>
>
> def setData(self, index, value, role):
>
>
> if role == Qt.CheckStateRole:
> if value == Qt.Checked:
> row = index.row()
> selected_stb = self.args[row]
> print 'selected_stb is %s' % selected_stb
> print 'Value is %s' % value
>
> self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),index, index)
> return True
> #return QVariant(Qt.Checked)
>
>
>
>
> def main():
> myapp = QApplication(sys.argv)
>
> data = ['STB1', 'STB2', 'STB3', 'STB4', 'STB5', 'STB6', 'STB7',
> 'STB8']
>
> model = StbTreeView(data)
> tree_view = QTreeView()
> tree_view.show()
> tree_view.setModel(model)
>
>
> myapp.exec_()
>
>
> if __name__ == '__main__':
> main()
>
>
> _______________________________________________
> PyQt mailing list PyQt@riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[Attachment #5 (text/html)]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
hi,<br>
<br>
you always return Qt.Unchecked for Qt.CheckStateRole in the data
method ...<br>
try the attached code.<br>
<br>
bests<br>
Zoltan<br>
<br>
<br>
<br>
<br>
On 2011.03.21. 6:21, vijay swaminathan wrote:
<blockquote
cite="mid:AANLkTi=QrAwO0eCNZm0NE0uUJy2tps8DhF2YqX3Cvdvf@mail.gmail.com"
type="cite">Hi All,<br>
<br>
I tried creating a treeview with checkboxes but I'm unable to
select the checkboxes. <br>
<br>
on the flag method I had mentioned it as ItemisuserCheckable but
still could not get it working...<br>
<br>
am I missing something here to enable the selection of checkboxes.<br>
<br>
A snippet of the code is:<br>
<br>
import sys<br>
<br>
from PyQt4.QtCore import *<br>
from PyQt4.QtGui import *<br>
<br>
<br>
<br>
<br>
class StbTreeView(QAbstractListModel):<br>
def __init__(self, args, parent=None):<br>
super(StbTreeView, self).__init__(parent)<br>
<br>
self.args = args<br>
print self.args<br>
<br>
<br>
def rowCount(self, parent):<br>
return len(self.args)<br>
<br>
def headerData(self, section, orientation, role):<br>
if role == Qt.DisplayRole:<br>
if orientation == \
Qt.Horizontal:<br>
\
return QString("Select STB's")<br> <br>
def flags(self, index):<br>
row = index.row()<br>
if row:<br>
return \
Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsSelectable<br>
<br>
def data(self, index, role=Qt.DisplayRole):<br>
<br>
if role == Qt.DisplayRole:<br>
row = index.row()<br>
return \
self.args[row]<br> <br>
if role == Qt.CheckStateRole:<br>
<br>
row = index.row()<br>
return \
QVariant(Qt.Unchecked)<br> <br>
<br>
def setData(self, index, value, role):<br>
<br>
<br>
if role == Qt.CheckStateRole:<br>
if value == \
Qt.Checked:<br>
\
row = index.row()<br>
\
selected_stb = self.args[row]<br>
\
print 'selected_stb is %s' % selected_stb<br>
\
print 'Value is %s' % value<br>
self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),index,
index)<br>
\
return True<br>
\
#return QVariant(Qt.Checked)<br>
\
<br>
\
<br> <br>
<br>
def main():<br>
myapp = QApplication(sys.argv)<br>
<br>
data = ['STB1', 'STB2', 'STB3', 'STB4', 'STB5', 'STB6',
'STB7', 'STB8']<br>
<br>
model = StbTreeView(data)<br>
tree_view = QTreeView()<br>
tree_view.show()<br>
tree_view.setModel(model)<br>
<br>
<br>
myapp.exec_()<br>
<br>
<br>
if __name__ == '__main__':<br>
main()<br>
<br>
<pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
PyQt mailing list <a class="moz-txt-link-abbreviated" \
href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a> <a \
class="moz-txt-link-freetext" \
href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a></pre>
</blockquote>
<br>
</body>
</html>
["cb.py" (application/x-python)]
_______________________________________________
PyQt mailing list PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic