PDA

View Full Version : Learning Qt with C++



Philip_Anselmo
4th May 2006, 16:10
Hello
I'm a n00b on C++ and Qt I'm working on a Fedora Core 1 with Qt 3.1.2 on the designer
here I uploaded my app it's a very simple one just for learning... as I must lear to program on c++ and qt as quick as I can for my work.. I'm against the clock and I have almost no time.. as I'm studing too :(

well you can download the app HERE (http://philipsoft.dajoob.com/downs/usr/pro1.zip) with the source code

My actual problem is filling a QDataTable on Form (the mainForm. the only form realy)
I'm having some problems as I can't introduce code directly so I made the conection, disconection and qsqlcursor to the database on conexion.cpp and I don't real know how to fill the qdatatable named tabla, on form.ui.h :(
help please

I hope this also help some other n00bs as me :p

Philip_Anselmo
4th May 2006, 22:30
The conexion.cpp modified so the QDataTable tabla on the mainwindow Form uses the cursor created on the conexion.cpp is the next code:



#include <qsqldatabase.h>
#include <qmessagebox.h>
#include <qdatatable.h>
#include <qsqlcursor.h>
#include "conexion.h"
#include "form.h"

QSqlDatabase *defaultDB;
bool creaConexion()
{

defaultDB = QSqlDatabase::addDatabase( DBDRIVER );
defaultDB->setDatabaseName( DB_NOM );
defaultDB->setUserName( DB_USR );
defaultDB->setPassword( DB_PASS );
defaultDB->setHostName( DB_HOSTNAME );

if ( ! defaultDB->open() ) {
QMessageBox::warning(0, "Fallo de conexión", "No se pudo conectar con la base de datos: " + DB_NOM + "tabla: " + DB_TABLA + "\n" + defaultDB->lastError().driverText());
return FALSE;
}
QMessageBox::information(0,"Conexión estabecida","Conectado a: " + DB_HOSTNAME + " " + "tabla: " + DB_TABLA + " "+ DB_NOM);
return TRUE;
}

bool cierraConexion()
{
if (defaultDB->open()){
defaultDB->close();
QMessageBox::information(0,"Cerrando conexión","Desconectando: " + DB_HOSTNAME + " " + DB_NOM);
return TRUE;
}
else{
return FALSE;
}
}

void llena()
{
qWarning("Inicio del poblado....");
QSqlCursor cursor( DB_TABLA ); // Especifica la tabla
qWarning("Cursor creado apuntando a la tabla" + DB_TABLA);
cursor.select(); // Devuelve todos los registros de la tabla
qWarning("Cursor llenado ");
while ( cursor.next() ) {}
Form().tabla( &cursor );
}


as you could tell on first view.. the last line won't work because it treats the tabla widget it were a function, but I guess it isn't because the compiler shows an error on that.. :rolleyes:
but on one of th exaples provided by QT 3.1.2 the sqltable to be more specific.. it inicialices the datatable as this:


QDataTable tabla( &cursor ); /* data table uses our cursor */

So I thought (and I guess theres the error xD), that if the widget was already created I maybe could force it to use my cursor from conexion.cpp on the Form.... and then I could fill the... f*** table... help plz

I'm already out of ideas

jacek
4th May 2006, 22:37
Download this book: http://www.phptr.com/title/0131240722 and read (at least) chapter 12.