PDA

View Full Version : QTableView invisible content



danczer
23rd December 2010, 15:17
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->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);
}


header:


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;
};


cpp:


#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();
}

Lykurg
23rd December 2010, 15:22
is the model set up correctly? can you access any data in the model? Also maybe do you delete the model before the view is shown? Also where do you set the database for your model?

danczer
23rd December 2010, 15:44
Ty for fast replay!

I recreated the "tableMainData"(QTableView) in designer and it is working well now. The previous "tableMainData"(QTableView) was morphed from QTableWidget.