有一个继承自QSqlTableModel和QTableView的FilmModel类。 我设置了模型
model = new FilmModel(this);
ui->filmList->setModel(model);
然后在QTableView插槽中双击(QModelIndex索引)我尝试获取记录。
QSqlRecord rr = model->record(index.row());
qDebug() << rr;
我得到空值。它正确写入日志列名称,但每个值都是“”。我究竟做错了什么?在这里编写CREATE TABLE请求可能会有用:
query.exec("CREATE TABLE IF NOT EXISTS films ("
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
"name VARCHAR, "
"year SMALLINT UNSIGNED, "
"country VARCHAR, "
"director VARCHAR, "
"actor VARCHAR, "
"type TINYINT UNSIGNED, "
"genre INT UNSIGNED, "
"score TINYINT UNSIGNED, "
"poster LONGBLOB)");
RDBMS是SQLite。
答案 0 :(得分:0)
我只修改了一点answer。这是我的代码,它可以工作:
//pro file:
QT += core gui sql
TARGET = test1
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp \
database.cpp
HEADERS += widget.h \
database.h
FORMS += widget.ui
//database.h:
#ifndef DATABASE_H
#define DATABASE_H
#include <QSqlDatabase>
class Database
{
public:
Database();
~Database();
QSqlDatabase db;
bool connection();
void createTables();
};
#endif // DATABASE_H
//database.cpp:
#include "database.h"
#include <QtGui>
#include <QSqlQuery>
Database::Database()
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
}
Database::~Database()
{
db.close();
}
bool Database::connection()
{
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Ok);
return false;
}
return true;
}
void Database::createTables()
{
QSqlQuery q;
q.exec("CREATE TABLE test(id integer primary key,name varchar(20))");
q.exec("INSERT INTO test(name) VALUES('foo')");
q.exec("INSERT INTO test(name) VALUES('fie')");
q.exec("INSERT INTO test(name) VALUES('bar')");
}
//widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QModelIndex>
class QSqlQueryModel;
class Database;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void slotTest(QModelIndex idx);
private:
QSqlQueryModel *model;
Database *m_db;
Ui::Widget *ui;
};
#endif
//widget.cpp:
#include "widget.h"
#include "ui_widget.h"
#include "database.h"
#include <QSqlQueryModel>
#include <QSqlRecord>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_db = new Database();
m_db->connection();
m_db->createTables();
model = new QSqlQueryModel();
model->setQuery("SELECT * from test");
ui->tableView->setModel(model);
connect(ui->tableView,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(slotTest(QModelIndex)));
}
Widget::~Widget()
{
delete ui;
}
void Widget::slotTest(QModelIndex idx)
{
QSqlRecord r;
if(idx.isValid())
{
r = model->record(idx.row());
qDebug() << r;
}
}
//widget.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>274</width>
<height>210</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
//main.cpp:
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}