Populating table cells with various widgets



  • Hi everyone,

    I am just starting to learn QT (using PySide) and am wondering how I can use widgets such as ComboBox, PushButton, CheckBox etc. inside of (or as) table cells.
    I've searched high and low for examples but haven't been able to find what I need.

    Would anybody be willing to lead me in the right direction? It'd be very much appreciated!

    Thanks,
    frank



  • Hi,

    You're in the wrong section. Your question has nothing to do with Qt Quick & QML. This is a Qt (PySide) related question. Thus, you should have asked "here":http://qt-project.org/forums/viewforum/15/

    However, if I understood correctly, you're trying to insert widgets such as QComboBox or QPushButton into a QTableView.
    What you need is a "QStyledItemDelegate":http://qt-project.org/doc/qt-4.8/qstyleditemdelegate.html.
    Juste create your own custom delegate, and override the createEditor(), setEditorData(), setModelData() and sizeHint() functions (you may have to override paint() as well).
    You can look at the "SpinBoxDelegate":http://doc.qt.nokia.com/4.7-snapshot/itemviews-spinboxdelegate.html example.



  • Oops, sorry about that. I misunderstood the forum description.
    Thanks heaps for the reply, I will take a look at your suggestion.


  • Moderators

    I have moved the thread to the Language Bindings forum.



  • thanks.

    So I had a look at the "SpinBoxDelegate example":http://qt.gitorious.org/pyside/pyside-examples/blobs/492cb950507fc1606615c9869148baae45b33235/examples/itemviews/spinboxdelegate.py but realised that it's using the QTableView and not QTableWidget. QTableView seems to use spinbox widgets automatically anyway for numbers which confused me a bit (see below code).

    Also, in the example the entire QTableView is set to use the SpinBoxDelegate, but I need different types of widgets/delegates in the table (combo boxes, colour swatches, etc).
    I'm having a hard time finding the right information about this as every example I stumble across seems to tackle things differently.
    Any more pointers anyone? I'd be very grateful!

    Thanks,
    frank

    @import sys
    from PySide import QtCore, QtGui

    class TableTest( QtGui.QTableView ):
    def init( self ):
    super( TableTest, self ).init()

        self.model = QtGui.QStandardItemModel( 1, 3 )
        self.setModel( self.model )
    
        testFloat = 1.234
        testString = 'hello'
        testList = [ 'A', 'B', 'C' ]
    
        index = self.model.index( 0, 0, QtCore.QModelIndex() )
        self.model.setData( index, testFloat )
        index = self.model.index( 0, 1, QtCore.QModelIndex() )
        self.model.setData( index, testString )
        index = self.model.index( 0, 2, QtCore.QModelIndex() )
        self.model.setData( index, testList )
    
                
        self.setWindowTitle( "Spin Box Delegate  - not?" )
        self.show()
    

    def main():
    app = QtGui.QApplication( sys.argv )
    tableView = TableTest()
    sys.exit(app.exec_())

    if name == "main":
    main()@



  • bq. but realised that it’s using the QTableView and not QTableWidget.

    QTableWidget just inherits QTableView and implements a custom model (QTableModel). Anyway, for scalability reasons, the Q*Widget should only be used for static content

    bq. but I need different types of widgets/delegates in the table (combo boxes, colour swatches, etc).

    You are in charge of creating the corresponding widget for the cell. This is the QStyledItemDelegate::createEditor role.



  • bq. for scalability reasons, the Q*Widget should only be used for static content

    you mean in this case using QTableWidget will be too slow for large tables as opposed to sub classing QTableView and building the required stuff yourself?
    Someone pointed out the QTableWidget.setCellWidget method which lets you do what I'm after very easily, but according to what you are suggesting this will become a problem?
    I'm just really trying to learn things the right way if possible so please excuse my ignorant questions

    bq. You are in charge of creating the corresponding widget for the cell. This is the QStyledItemDelegate::createEditor role.

    thanks, I will investigate that more.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.