I'm want to use a QTableView with a QSqlTabkeModel. The table's content is invisible after seting up the model.
The QTableView widget is created in designer.
initalizeModel test method is from http://doc.qt.nokia.com/stable/sql-tablemodel.html
void DatabaseWindow::initializeModel()
{
mDataTableModel->setTable("person");
mDataTableModel->select();
mDataTableModel
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("ID"));
mDataTableModel
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("First name"));
mDataTableModel
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("Last name"));
//here i call ExtendedTableWidget's setUpView method
mTableWidget->setUpView(mDataTableModel);
}
void DatabaseWindow::initializeModel()
{
mDataTableModel->setTable("person");
mDataTableModel->setEditStrategy(QSqlTableModel::OnRowChange);
mDataTableModel->select();
mDataTableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
mDataTableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
mDataTableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
//here i call ExtendedTableWidget's setUpView method
mTableWidget->setUpView(mDataTableModel);
}
To copy to clipboard, switch view to plain text mode
header:
namespace Ui
{
class ExtendedTableWidget;
}
class ExtendedTableWidget
: public QWidget{
Q_OBJECT
public:
explicit ExtendedTableWidget
(QWidget *parent
= 0);
private:
private:
Ui::ExtendedTableWidget *mUi;
};
namespace Ui
{
class ExtendedTableWidget;
}
class ExtendedTableWidget : public QWidget
{
Q_OBJECT
public:
explicit ExtendedTableWidget(QWidget *parent = 0);
void setUpView(QSqlTableModel *model);
private:
private:
Ui::ExtendedTableWidget *mUi;
};
To copy to clipboard, switch view to plain text mode
cpp:
#include "extendedtablewidget.h"
#include "ui_extendedtablewidget.h"
ExtendedTableWidget
::ExtendedTableWidget(QWidget *parent
) : mUi(new Ui::ExtendedTableWidget())
{
mUi->setupUi(this);
}
{
mUi->tableMainData->setModel(model);
//the tpmTable is show the content, but tableMainData isn't
tpmTable.setModel(model);
tpmTable.show();
}
#include "extendedtablewidget.h"
#include "ui_extendedtablewidget.h"
ExtendedTableWidget::ExtendedTableWidget(QWidget *parent) :
QWidget(parent),
mUi(new Ui::ExtendedTableWidget())
{
mUi->setupUi(this);
}
void ExtendedTableWidget::setUpView(QSqlTableModel *model)
{
mUi->tableMainData->setModel(model);
mUi->tableMainData->setSelectionModel(new QItemSelectionModel(model));
//the tpmTable is show the content, but tableMainData isn't
QTableView *tpmTable = new QTableView();
tpmTable.setModel(model);
tpmTable.show();
}
To copy to clipboard, switch view to plain text mode
Bookmarks