开发者

QSqlRecord returned from QSqlTableModel::record() is empty

There are a class FilmModel inherited from QSqlTableModel and QTableView. I set model

model = new FilmModel(this);
ui->filmList->setModel(model);

Then in QTableView slot doubleClicked(QModelIndex index) i try to get record.

开发者_如何转开发
QSqlRecord rr = model->record(index.row());
qDebug() << rr;

And i get empty values. It writes to log column names properly but every values are "". What am i doing wrong? Perhaps would be useful to write CREATE TABLE request here:

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 is SQLite.


I modified just a little bit another answer. This is my code and it works:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜