Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the model to a QTableWidget

Tags:

c++

sql

qt

I want to write an application that can access a table in the database. I took QSqlTableModel as model component for the table.

The problem with the QTableView is that it seems to have no method that returns the currently selected record in the table so i took the QTableWidget class which interhits QTableView.

But when i try to set the model to this table widget with ->setModel() i get

the following error message:

c:/Qt/qt/include/QtGui/../../src/gui/itemviews/qtablewidget.h:337: error: `virtual void QTableWidget::setModel(QAbstractItemModel*)' is private.

The message says that the method "setModel" is private. Looking into the documentation tells me that it is public.

What can I do?

like image 273
crew4ok Avatar asked Jul 16 '09 13:07

crew4ok


People also ask

What is QTableWidget?

The QTableWidget class provides an item-based table view with a default model. Table widgets provide standard table display facilities for applications. The items in a QTableWidget are provided by QTableWidgetItem. If you want a table that uses your own data model you should use QTableView rather than this class.

What is the difference between QTableView and QTableWidget?

The major difference between the two is that QTableView requires you to use an external class derived from QAbstractItemModel with it. The rowCount(), columnCount(), and data() methods for the item model are used by the table view to tell it how to fill in and format the table cells.

What is a QT model?

Two of the standard models provided by Qt are QStandardItemModel and QFileSystemModel. QStandardItemModel is a multi-purpose model that can be used to represent various different data structures needed by list, table, and tree views. This model also holds the items of data.

How to clear QTableWidget?

[slot] void QTableWidget::clear() Removes all items in the view. This will also remove all selections and headers. If you don't want to remove the headers, use QTableWidget::clearContents().


2 Answers

As others have noted, it's not QTableWidget that you want. It's indeed QTableView. Getting the records is then done like this:

static QList<QSqlRecord> selected_records( const QTableView * tv ) {
    // make sure we're really dealing with what we think we're dealing with:
    assert( static_cast<QSqlTableModel*>( tv->model() )
            == qobject_cast<QSqlTableModel*>( tv->model() );
    const QSqlTableModel * const tm = static_cast<QSqlTableModel*>( tv->model() );
    const QModelIndexList mil = tv->selectionModel()->selectedRows();
    QList<QSqlRecord> result;
    Q_FOREACH( const QModelIndex & mi, mil )
        if ( mi.isValid() )
            result.push_back( tm->record( mi.row() ) );
    return result;
}

If, OTOH, you are working in a slot connected to the - say - clicked(QModelIndex) signal of QTableView (really: QAbstractItemView), then this code is what you want:

void slotClicked( const QModelIndex & mi ) {
    // make sure we're really dealing with what we think we're dealing with:
    assert( static_cast<QSqlTableModel*>( tableView->model() )
            == qobject_cast<QSqlTableModel*>( tableView->model() );
    const QSqlRecord rec = static_cast<QSqlTableModel*>( tableView->model() )
               ->record( mi.row() );
    // use 'rec'
} 

Yes, Qt could have that built-in, and esp. QSqlTableModel could have a more convenient way to map a QModelIndex back to a QSqlRecord, but there you go.

like image 171
Marc Mutz - mmutz Avatar answered Oct 13 '22 00:10

Marc Mutz - mmutz


The method is public at the level of QAbstractItemView but QTableWidget has a built-in model which you can't change.

To get the selection, you must call selectedItems() (which is again a method of QAbstractItemView and not QTableView which is why you missed it in the docs).

like image 23
Aaron Digulla Avatar answered Oct 13 '22 01:10

Aaron Digulla