Setting up the model on a QTableWidget

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

The problem with QTableView is that it does not have a method that returns the currently selected record in the table, so I took the QTableWidget class, which intercepts QTableView.

But when I try to install the model on this table widget using -> setModel (), I get

following error message:

C: / Qt / Qt / enable / QtGui /../../ SRC / GUI / itemviews / qtablewidget.h: 337: Error: `virtual void QTableWidget :: setModel (QAbstractItemModel *) 'is private.

The message says the setModel method is private. A look in the documentation tells me that it is publicly available.

What can I do?

+7
c ++ sql qt
source share
5 answers

As others have noted, this is not the QTableWidget that you want. This is really a QTableView . Getting records is as follows:

 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 a signal - say - clicked(QModelIndex) QTableView (valid: QAbstractItemView ), then you need this code:

 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 can have built-in and esp. QSqlTableModel may have a more convenient way to map QModelIndex to QSqlRecord , but there you go.

+5
source share

This method is publicly available at the QAbstractItemView level, but QTableWidget has a built-in model that you cannot change.

To get the selection, you must call selectedItems() (this is again the QAbstractItemView method, not the QTableView , so you skipped it in the docs).

+2
source share

it is closed in QTableWidget

 class Q_GUI_EXPORT QTableWidget : public QTableView { ... ... private: void setModel(QAbstractItemModel *model); ... 

it is publicly available in QAbstractItemView

therefore you cannot call this function here ...

check qtablewidget.h in include \ Qt \ qtablewidget.h

This may not be a very good answer, but at least it shows why it doesn't work ...

0
source share

QTableWidget: More

The QTableWidget class provides a tabular presentation based on elements with a default model.

Table widgets provide standard applications for displaying tables for applications. Elements in a QTableWidget are provided by a QTableWidgetItem.

If you need a table that uses your own data model, you should use a QTableView, not this class.

The widget class handles the model itself, if you want to use your own model, use the View class.

You are right that there seem to be no methods for determining the choice for a TableView or SQLModel. You can get your own class from TableView and track the current selection through selectionChanged slot .

OR

Use QTableView :: selectionModel () and call selection () . This is similar to mmutz answer . Be sure to read this code for details about what you really hit the record.

0
source share

I only used the architecture model once, but I will try to give you a general idea of ​​this architecture, because it seems to me that you still do not understand it very well. So this is likely to be incomplete and simplistic, but hopefully somewhat correct.

If you are working with a view, you can provide your own model. If you work with widgets, then you do not work with the qt model, but insert the elements yourself. It is advisable that you work with the model for decoupling things (so that you can have more than one view for the same model or change the model later ...)

When you use a model, the view itself will know how to ask the model you provide to populate the view (using the data function). There are several ways to get a choice from this view: I processed it by connecting the click signal that is displayed when a user clicks on the view into a slot function that I wrote myself. The click signal provides a table / list index, which I map to an element in my model in this slot function.

There are probably more ways to do this, but here's how I did it, and it works great.

To get a general idea of ​​the qt model presentation architecture:

http://doc.trolltech.com/4.5/model-view-programming.html

0
source share

All Articles