QSqlRelationalTableModel操作问题
代码如下:void CRelationDlg::on_proComboBox_currentIndexChanged(const QString &arg1)
{
if(arg1 == "none")
return;
QSqlRelationalTableModel *relationModel = new QSqlRelationalTableModel(this);
QString tablename = arg1+"_relation";
QString chanTableName = arg1+"_chan";
QSqlQuery query;
QString sql;
bool newcreate = false;
if(!db.tables().contains(tablename))
{
sql = QString("create table %1(no int primary key,pro varchar,chan varchar,sigID varchar,sigChan int)").arg(tablename);
query.exec(sql);
newcreate = true;
qDebug()<<"create table:"<<tablename;
}
relationModel->setEditStrategy(QSqlTableModel::OnFieldChange);
relationModel->setTable(tablename);
relationModel->setRelation(2,QSqlRelation(chanTableName,"name","name"));
relationModel->setRelation(3,QSqlRelation("signalDev","id","id"));
relationModel->setHeaderData(0, Qt::Horizontal, QObject::tr("序号"));
relationModel->setHeaderData(1, Qt::Horizontal, QObject::tr("保护类型"));
relationModel->setHeaderData(2, Qt::Horizontal, QObject::tr("通道名称"));
relationModel->setHeaderData(3, Qt::Horizontal, QObject::tr("信号源ID"));
relationModel->setHeaderData(4, Qt::Horizontal, QObject::tr("信号源通道"));
if((!newcreate))
{
relationModel->select();
}
ui->tableView->setModel(relationModel);
ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));
}
有两个问题:1、relationModel->setRelation(2,QSqlRelation(chanTableName,"name","name"));为什么必须两个名字要一样才能正常用呢?按qt助手里的解释,第二个参数应该是chanTableName里的列名,第三个参数应该是目标表的列名,各位看我建的新表里根本就没有name这一列,可是很奇怪,能正常工作,而且把第三个参数改为“chan”就不正常了。
2、如果发现数据库里没有这个表,就创建它,这个时候如果调用relationModel->select();运行的就会在下一行ui->tableView->setModel(relationModel);处报错,程序直接死掉,请问这是为什么? --------------------编程问答-------------------- class CRelationDlg : public QTableView
{
public:
void on_proComboBox_currentIndexChanged(const QString &arg1)
{
if(arg1 == "none")
return;
QSqlRelationalTableModel *relationModel = new QSqlRelationalTableModel(this);
QString tablename = arg1+"_relation";
QString chanTableName = arg1+"_chan";
QSqlQuery query;
QString sql;
bool newcreate = false;
if(!QSqlDatabase::drivers().contains("QSQLITE"))
{
QMessageBox::information(NULL, "tip", "System can't install SqlLite driver!");
return;
}
QString name = QCoreApplication::applicationDirPath() + "/mytest.db";
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", name);
db.setDatabaseName(name);
if (!db.open())
return;
query = QSqlQuery(tablename, db);
tablename = arg1;
if(!db.tables().contains(tablename))
{
sql = QString("create table %1(no int primary key,pro varchar,chan varchar,sigID varchar,sigChan int)").arg(tablename);
query.exec(sql);
newcreate = true;
qDebug()<<"create table:"<<tablename;
}
relationModel->setEditStrategy(QSqlTableModel::OnFieldChange);
relationModel->setTable(tablename);
//relationModel->setRelation(2,QSqlRelation(chanTableName,"name","name"));
relationModel->setRelation(3,QSqlRelation("signalDev","id","id"));
relationModel->setHeaderData(0, Qt::Horizontal, QObject::tr("序号"));
relationModel->setHeaderData(1, Qt::Horizontal, QObject::tr("保护类型"));
relationModel->setHeaderData(2, Qt::Horizontal, QObject::tr("通道名称"));
relationModel->setHeaderData(3, Qt::Horizontal, QObject::tr("信号源ID"));
relationModel->setHeaderData(4, Qt::Horizontal, QObject::tr("信号源通道"));
//if((!newcreate))
{
relationModel->select();
}
setModel(relationModel);
setItemDelegate(new QSqlRelationalDelegate(this));
}
};
class CRelationDlg : public QTableView
{
public:
void on_proComboBox_currentIndexChanged(const QString &arg1)
{
if(arg1 == "none")
return;
QSqlRelationalTableModel *relationModel = new QSqlRelationalTableModel(this);
QString tablename = arg1+"_relation";
QString chanTableName = arg1+"_chan";
QSqlQuery query;
QString sql;
bool newcreate = false;
if(!QSqlDatabase::drivers().contains("QSQLITE"))
{
QMessageBox::information(NULL, "tip", "System can't install SqlLite driver!");
return;
}
QString name = QCoreApplication::applicationDirPath() + "/mytest.db";
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", name);
db.setDatabaseName(name);
if (!db.open())
return;
query = QSqlQuery(tablename, db);
tablename = arg1;
if(!db.tables().contains(tablename))
{
sql = QString("create table %1(no int primary key,pro varchar,chan varchar,sigID varchar,sigChan int)").arg(tablename);
query.exec(sql);
newcreate = true;
qDebug()<<"create table:"<<tablename;
}
relationModel->setEditStrategy(QSqlTableModel::OnFieldChange);
relationModel->setTable(tablename);
//relationModel->setRelation(2,QSqlRelation(chanTableName,"name","name"));
relationModel->setRelation(3,QSqlRelation("signalDev","id","id"));
relationModel->setHeaderData(0, Qt::Horizontal, QObject::tr("序号"));
relationModel->setHeaderData(1, Qt::Horizontal, QObject::tr("保护类型"));
relationModel->setHeaderData(2, Qt::Horizontal, QObject::tr("通道名称"));
relationModel->setHeaderData(3, Qt::Horizontal, QObject::tr("信号源ID"));
relationModel->setHeaderData(4, Qt::Horizontal, QObject::tr("信号源通道"));
//if((!newcreate))
{
relationModel->select();
}
setModel(relationModel);
setItemDelegate(new QSqlRelationalDelegate(this));
}
};
这是我做的测试,不管怎么调用select都不会死,至于第一个问题,现在不作评论 --------------------编程问答-------------------- 最好不用这个类。用数据库里面的视图。操作简单。
补充:移动开发 , Qt