Well, finally I managed to connect to the shared database. The cause was that the variable 'db'' must be a public member of the class, which is not properly documented nor in Qt documentation nor in the examples.
When I changed to:
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql/QSqlDatabase>
#include <QSqlTableModel>
#include <QMessageBox>
#include <QSqlError>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void connectar();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql/QSqlDatabase>
#include <QSqlTableModel>
#include <QMessageBox>
#include <QSqlError>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QSqlDatabase db; // declared as public
QSqlTableModel *model;
void connectar();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connectar();
model->setTable("families");
QMessageBox::critical(0,
"Error", model
->lastError
().
text());
// throws "Unable to find table families" error ui->llista->setModel(model); // llista is QListView
ui->llista->setModelColumn(1);
ui->llista->show();
connect(ui->btnClose, SIGNAL(clicked()), this, SLOT(close()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectar() {
QString nom
= "\\\\192.168.1.33\\dades\\proves.db";
db.setDatabaseName(name);
if (!db.open()) {
QMessageBox::critical(0,tr
("Connexió"), tr
("Unable to connect to %1").
arg(name
));
} else {
QMessageBox::information(0,tr
("Connexió"), tr
("Connected to %1 !!!").
arg(name
));
}
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connectar();
model = new QSqlTableModel(this);
model->setTable("families");
QMessageBox::critical(0,"Error", model->lastError().text()); // throws "Unable to find table families" error
ui->llista->setModel(model); // llista is QListView
ui->llista->setModelColumn(1);
ui->llista->show();
connect(ui->btnClose, SIGNAL(clicked()), this, SLOT(close()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectar() {
db = QSqlDatabase::addDatabase("QSQLITE");
QString nom = "\\\\192.168.1.33\\dades\\proves.db";
db.setDatabaseName(name);
if (!db.open()) {
QMessageBox::critical(0,tr("Connexió"), tr("Unable to connect to %1").arg(name));
} else {
QMessageBox::information(0,tr("Connexió"), tr("Connected to %1 !!!").arg(name));
}
}
To copy to clipboard, switch view to plain text mode
the connection is successfully established, but then the error "Unable to find table families" appears. Of course, I've made sure that the table exists in the database. Tests with other tables in the same database throw the same error.
I am aware that Sqlite is not the proper database for sharing; in fact, I've always preferred MySQL in all my projects. But I would rather not to change the desktop application mentioned in my first post.
So, if a fellow has networked SQLite databases, I appreciate his/her advice to address this latter problem (Unable to find table ...)
Thanks.
Bookmarks