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 
        QT       += core gui
QT       += sql
TARGET = Gestion
TEMPLATE = app
SOURCES += main.cpp\
        mainwindow.cpp
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui
To copy to clipboard, switch view to plain text mode 
  
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[]) 
- { 
-   
-             db.setHostName("localhost"); 
-             db.setPort(5433); 
-             db.setDatabaseName("totof"); 
-             db.setUserName("postgres"); 
-             db.setPassword("password"); 
-   
-             db.open(); 
-   
-     MainWindow w; 
-     w.show(); 
-   
-     return a.exec(); 
- } 
        #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();
}
To copy to clipboard, switch view to plain text mode 
  
and the mainwindows.cpp
	
	- #include "mainwindow.h" 
- #include "ui_mainwindow.h" 
-   
- MainWindow ::MainWindow(QWidget *- parent ) :
-     ui(new Ui::MainWindow) 
- { 
-     ui->setupUi(this); 
-   
-   
-       mapper->addMapping(ui->lineEdit,0); 
- } 
-   
-   
- MainWindow::~MainWindow() 
- { 
-     delete ui; 
- } 
        #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;
}
To copy to clipboard, switch view to plain text mode 
  
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
				
			
Bookmarks