Qt World Summit: Submit your Presentation

QTableWidget with different types

  • Hello, I would like to display an editable QTableWidget, but with some item only editable as int and some others as strings.
    I can't find a solution in the doc.

    Is it possible? How could it be done?


  • Well, you can set any widget into Cell, for example:
    QLineEdit for strings and QSpinBox for digits.
    @QTableWidget::setCellWidget ( int row, int column, QWidget * widget )@

  • That's a good idea. Thanks.

    However, I've just tested it and there's a problem :
    My QTableWidget is composed by about a thousand cells. So when I apply the method that create QLineEdit or QSpinBox, it takes about 5 seconds to display while when I was using only QTableWidgetItem it was immediate.

    So, If someone know a way to specify the type of a QTableWidgetItem, it would be much better.

  • QTableWidget provides basicaly possibility and works slowly. For more flexible possibility use QStandardItemModel & QTableView or QAbstractItemModel & QTableView

  • I found a more simple solution with the class QIntValidator
    I put a QLineEdit with this validator on the item of the table I want to edit.
    Works perfectly.

    Thanks for helps.

  • I used this solution where you have a QLineEdit in each cell.

    for(int trow=0; trow <= 2; trow++ )
            for(int tcolumn=0; tcolumn <= 3; tcolumn++ )
                QLineEdit * tableline = new QLineEdit;
                tableline->setValidator( new QDoubleValidator(0, 100, 2, this) );
                ui->tableWidget->setCellWidget ( trow, tcolumn,  tableline);

  • probably it's just my personal and never ending hate for setCellWidget but, especially for cases like this one where it applies to many cells, a combination of setFlag and a QStyledItemDelegate derived class is the way to go

Log in to reply