[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] Best way to add combobox into table?
From:       Ke Gao <ke.gao.ut () gmail ! com>
Date:       2018-05-29 23:53:41
Message-ID: CAMd10VacevLpT+iq+ed1njKgYOXBOPiFdmopQL51jzeJnOd+JA () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Thanks Maurizio. The problem solved by using QTableView.

Previously, I used exactly the same way as your code. The
self.table.cellWidget(4,
0) returns a QWidget, rather than QComboBox. I'm using Anaconda python 3
with PyQt 5.6. I guess this is a bug of either Anaconda PyQt building or
PyQt 5.6 itself. When I insert QSpinBox into QTableWidget, it works well
and returns QSpinBox when using self.table.cellWidget(4, 0).




On Tue, May 29, 2018 at 4:00 PM, Maurizio Berti <maurizio.berti@gmail.com>
wrote:

> There are different approaches, depending on your needs and the size of
> the table.
> If the size is not very big, using QTableWidget is fine, and you can
> access the data as you wrote (it works, see below).
> While this has the advantage of mixing different kinds of widgets
> independently (not row or column related), it also means that you should
> track every combo, and adding or removing cells and widgets might be a bit
> annoying for very large models.
> The alternative is using a QStandardItemModel and set a custom item
> delegate (QItemDelegate or, better, QStyledItemDelegate) for the column or
> row you are interested into; if you want, you can set the default delegate
> for the whole table too. In this way the table only cares about its data
> (which is usually better) and you can set the combobox only when editing,
> through the createEditor method of the delegate.
> There is also the possibility of using QAbstractItemDelegate.setIndexWidget(index,
> widget), which is very similar to the use of the QTableWidget, but is not
> very suited for interaction.
> 
> I tried to use cellWidget to get the value from the combo and it works
> without problems. Are you sure you implemented it in the right way?
> Here's a simple example:
> 
> class Widget(QtWidgets.QWidget):
> def __init__(self):
> QtWidgets.QWidget.__init__(self)
> l = QtWidgets.QGridLayout()
> self.setLayout(l)
> self.table = QtWidgets.QTableWidget(20, 1)
> l.addWidget(self.table)
> for r in range(10):
> combo = QtWidgets.QComboBox()
> combo.addItems([str(i) for i in range(50)])
> self.table.setCellWidget(r, 0, combo)
> self.btn = QtWidgets.QPushButton()
> l.addWidget(self.btn)
> self.btn.clicked.connect(self.printValue)
> 
> def printValue(self):
> print(self.table.cellWidget(4, 0).currentText())
> 
> 
> 
> 
> 
> 2018-05-27 18:04 GMT+02:00 Ke Gao <ke.gao.ut@gmail.com>:
> 
> > Hi,
> > 
> > I want to add a combobox into a a table, what is the best way, using
> > tableview or tablewidget? I tried tablewidget and succeeded using
> > setCellWidget. However, I can't obtain the current text in the combobox.
> > For example, cellWidget(row, col).currentText(). Is there any way to read
> > and change the data in the combobox in tablewidget?
> > 
> > Thank you very much,
> > 
> > Ke
> > 
> > --
> > ............................................................
> > ............................................................
> > ......................
> > Ke Gao
> > 
> > _______________________________________________
> > PyQt mailing list    PyQt@riverbankcomputing.com
> > https://www.riverbankcomputing.com/mailman/listinfo/pyqt
> > 
> 
> 
> 
> --
> È difficile avere una convinzione precisa quando si parla delle ragioni
> del cuore. - "Sostiene Pereira", Antonio Tabucchi
> http://www.jidesk.net
> 



-- 
..............................................................................................................................................
 Ke Gao


[Attachment #5 (text/html)]

<div dir="ltr"><div><div>Thanks Maurizio. The problem solved by using QTableView. \
<br><br></div>Previously, I used exactly the same way as your code. The <font \
face="monospace, monospace">self.table.cellWidget(4, 0)<font \
face="arial,helvetica,sans-serif"> returns a QWidget, rather than QComboBox. I&#39;m \
using Anaconda python 3 with PyQt 5.6. I guess this is a bug of either Anaconda PyQt \
building or PyQt 5.6 itself. When I insert QSpinBox into QTableWidget, it works well \
and returns QSpinBox when using </font></font><font face="monospace, monospace"><font \
face="monospace, monospace">self.table.cellWidget(4, 0). \
<br><br><br></font></font></div><font face="monospace, monospace"><font \
face="monospace, monospace"></font></font><div><font face="monospace, \
monospace"><font face="monospace, monospace"><br></font></font></div></div><div \
class="gmail_extra"><br><div class="gmail_quote">On Tue, May 29, 2018 at 4:00 PM, \
Maurizio Berti <span dir="ltr">&lt;<a href="mailto:maurizio.berti@gmail.com" \
target="_blank">maurizio.berti@gmail.com</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div dir="ltr"><div>There are different approaches, depending \
on your needs and the size of the table.<br></div>If the size is not very big, using \
QTableWidget is fine, and you can access the data as you wrote (it works, see \
below).<div>While this has the advantage of mixing different kinds of widgets \
independently (not row or column related), it also means that you should track every \
combo, and adding or removing cells and widgets might be a bit annoying for very \
large models.<br><div>The alternative is using a QStandardItemModel and set a custom \
item delegate (QItemDelegate or, better, QStyledItemDelegate) for the column or row \
you are interested into; if you want, you can set the default delegate for the whole \
table too. In this way the table only cares about its data (which is usually better) \
and you can set the combobox only when editing, through the createEditor method of \
the delegate.</div></div><div>There is also the possibility of using \
QAbstractItemDelegate.<wbr>setIndexWidget(index, widget), which is very similar to \
the use of the QTableWidget, but is not very suited for \
interaction.</div><div><br></div><div>I tried to use cellWidget to get the value from \
the combo and it works without problems. Are you sure you implemented it in the right \
way?<br>Here&#39;s a simple example:</div><div><br></div><blockquote style="margin:0 \
0 0 40px;border:none;padding:0px"><div><div><font face="monospace, monospace">class \
Widget(QtWidgets.QWidget):</font></div></div><div><div><font face="monospace, \
monospace">      def __init__(self):</font></div></div><div><div><font \
face="monospace, monospace">            \
QtWidgets.QWidget.__init__(<wbr>self)</font></div></div><div><div><font \
face="monospace, monospace">            l = \
QtWidgets.QGridLayout()</font></div></div><div><div><font face="monospace, \
monospace">            self.setLayout(l)</font></div></div><div><div><font \
face="monospace, monospace">            self.table = QtWidgets.QTableWidget(20, \
1)</font></div></div><div><div><font face="monospace, monospace">            \
l.addWidget(self.table)</font></div></div><div><div><font face="monospace, \
monospace">            for r in range(10):</font></div></div><div><div><font \
face="monospace, monospace">                  combo = \
QtWidgets.QComboBox()</font></div></div><div><div><font face="monospace, monospace">  \
combo.addItems([str(i) for i in range(50)])</font></div></div><div><div><font \
face="monospace, monospace">                  self.table.setCellWidget(r, 0, \
combo)</font></div></div><div><div><font face="monospace, monospace">            \
self.btn = QtWidgets.QPushButton()</font></div></div><div><div><font face="monospace, \
monospace">            l.addWidget(self.btn)</font></div></div><div><div><font \
face="monospace, monospace">            \
self.btn.clicked.connect(self.<wbr>printValue)</font></div></div><div><div><font \
face="monospace, monospace"><br></font></div></div><div><div><font face="monospace, \
monospace">      def printValue(self):</font></div></div><div><div><font \
face="monospace, monospace">            print(self.table.cellWidget(4, \
0).currentText())</font></div></div></blockquote><div><br></div><div><br></div><div><br></div></div><div \
class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">2018-05-27 \
18:04 GMT+02:00 Ke Gao <span dir="ltr">&lt;<a href="mailto:ke.gao.ut@gmail.com" \
target="_blank">ke.gao.ut@gmail.com</a>&gt;</span>:<br></div></div><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div><div class="h5"><div \
dir="ltr"><div><div><div>Hi,<br><br></div>I want to add a combobox into a a table, \
what is the best way, using tableview or tablewidget? I tried tablewidget and \
succeeded using setCellWidget. However, I can&#39;t obtain the current text in the \
combobox. For example, cellWidget(row, col).currentText(). Is there any way to read \
and change the data in the combobox in tablewidget?<br><br></div>Thank you very \
much,<br><br></div>Ke<span class="m_2470344670096355731HOEnZb"><font \
color="#888888"><br clear="all"><div><div><div><div><br>-- <br><div \
class="m_2470344670096355731m_2102962780422994457gmail_signature" \
data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div \
dir="ltr"><div dir="ltr"><div><font size="1" face="arial,helvetica,sans-serif" \
color="#000000">..............................<wbr>..............................<wbr> \
..............................<wbr>..............................<wbr>......................</font></div><font \
face="tahoma,sans-serif" color="#3366ff"><div><font \
style="background-color:rgb(255,255,255)" face="arial black,sans-serif" \
color="#000000">Ke Gao</font></div></font></div></div></div></div></div></div> \
</div></div></div></div></font></span></div> \
<br></div></div>______________________________<wbr>_________________<br> PyQt mailing \
list      <a href="mailto:PyQt@riverbankcomputing.com" \
target="_blank">PyQt@riverbankcomputing.com</a><br> <a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noreferrer" \
target="_blank">https://www.riverbankcomputing<wbr>.com/mailman/listinfo/pyqt</a><span \
class="HOEnZb"><font color="#888888"><br></font></span></blockquote></div><span \
class="HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div \
class="m_2470344670096355731gmail_signature" data-smartmail="gmail_signature">È \
difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - \
&quot;Sostiene Pereira&quot;, Antonio Tabucchi<br><a href="http://www.jidesk.net" \
target="_blank">http://www.jidesk.net</a></div> </font></span></div>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" \
data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div \
dir="ltr"><div dir="ltr"><div><font color="#000000" face="arial,helvetica,sans-serif" \
size="1">............................................................................. \
.................................................................</font></div><font \
color="#3366ff" face="tahoma,sans-serif"><div><font color="#000000" face="arial \
black,sans-serif" style="background-color:rgb(255,255,255)">Ke \
Gao</font></div></font></div></div></div></div></div></div> </div>


[Attachment #6 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://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