PDA

View Full Version : Qt fiedls to postgresql tables



LeHibou
8th February 2011, 09:34
Hi there,

I am going crazy about a simple thing I guess. I seeked broadly all over the QSQL pages to find the answer; but..

My goal :
Linking my Qt interface fields to my postgresql database.
I read about QDataWidgetMapper but it doesn't fills my needs.
------------------------------------------------------------------------------
Context :

We will use my postgresql 's testing table.

There is
- a schema zero
- a table one
- a column two
------------------------------------------------------------------------------
The code I have :
I use Qt Creator and below is the code :

Gestion.pro


QT += core gui
QT += sql
TARGET = Gestion
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui



My main.cpp


#include <QtGui/QApplication>
#include <QCoreApplication>
#include <QtSql/QtSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <iostream>
#include "mainwindow.h"

#define q2c(string) string.toStdString()


int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("localhost");
db.setPort(5433);
db.setDatabaseName("totof");
db.setUserName("postgres");
db.setPassword("password");

db.open();

MainWindow w;
w.show();

return a.exec();
}



and the mainwindows.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

mapper = new QDataWidgetMapper(this);

mapper->addMapping(ui->lineEdit,0);
}


MainWindow::~MainWindow()
{
delete ui;
}


How could I tell the lineEdit to insert what i type in, into zero.un of my postgresql database ?

I rely on you, since i am not good enough to make it on my own,

LeHibou

wysota
9th February 2011, 23:14
Did you connect the widget mapper to a model?