You are not logged in.

tDuma

Beginner

  • "tDuma" is male
  • "tDuma" started this thread

Posts: 5

Location: ireland

  • Send private message

1

Saturday, October 25th 2003, 11:43pm

Newbie... with problems (As ussual)

Hi all,

maybe someone can help me with this.
I have a project with 1 mainWindow and 1 modal dialog.
What I am trying to do is to pass to the client a variable from the main Window. The code looks like this:

1. Parent:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <qfiledialog.h>
#include <qmessagebox.h>
#include <qsqldatabase.h>
#include "dlgdbconnection.h"
/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename functions or slots use
** Qt Designer which will update this file, preserving your code. Create an
** init() function in place of a constructor, and a destroy() function in
** place of a destructor.
*****************************************************************************/


void mainReport::fileNew()
{
    frmMain->setPaletteBackgroundColor(QColor(255,255,255));
}

void mainReport::fileSave()
{
    QString fName=QFileDialog::getSaveFileName("/home/oduma","XML Files(*.xml)",this,"save file dialog","Save");
    if(fName.find(".xml",0,FALSE)==-1)
	fName +=".xml";
    QMessageBox::information(this,"Reports","File to be saved: "+fName);
}

void mainReport::fileSaveAs()
{
    QString fName=QFileDialog::getSaveFileName("/home/oduma","XML Files(*.xml)",this,"save file dialog","Save");
    if(fName.find(".xml",0,FALSE)==-1)
	fName +=".xml";
    QMessageBox::information(this,"Reports","File to be saved: "+fName);
}

void mainReport::toolBarConnect()
{
    QSqlDatabase *curdbConnection;
    dlgDBConnection repDBC;
    repDBC.communicate(curdbConnection);
    repDBC.exec();
    
}


2. child:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <qmessagebox.h>
#include <qsqldatabase.h>
#define DB_DRIVER "QMYSQL3"

/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename functions or slots use
** Qt Designer which will update this file, preserving your code. Create an
** init() function in place of a constructor, and a destroy() function in
** place of a destructor.
*****************************************************************************/


void dlgDBConnection::dbConnect()
{
        QSqlDatabase *curDB=QSqlDatabase::addDatabase(DB_DRIVER);
    if(!curDB)
    {
	txtConnectionStatus->setPaletteForegroundColor(QColor(255,0,0));
	txtConnectionStatus->setText("Failed to connect!");
	this->_isConnected=-1;
    }
    else
    {
	curDB->setDatabaseName(edtDatabase->text());
	curDB->setUserName(edtUser->text());
	curDB->setPassword(edtPassword->text());
	curDB->setHostName(edtServer->text());
	if(curDB->open())
	{
	    txtConnectionStatus->setPaletteForegroundColor(QColor(0,0,255));
	    txtConnectionStatus->setText("Connected");
	    this->_isConnected=1;
	}
	else
	{
	    txtConnectionStatus->setPaletteForegroundColor(QColor(255,0,0));
	    txtConnectionStatus->setText("Failed to connect!");
	    this->_isConnected=-1;
	}
   }
}


void dlgDBConnection::communicate( QSqlDatabase *par1)
{

}


When I am compiling the project (make) I am getting this error:

In file included from .ui/dlgdbconnection.cpp:10:
.ui/dlgdbconnection.h:45: error: `QSqlDatabase' was not declared in this scope
.ui/dlgdbconnection.h:45: error: `par1' was not declared in this scope
.ui/dlgdbconnection.h:45: error: invalid data member initialization
.ui/dlgdbconnection.h:45: error: (use `=' to initialize static data members)
.ui/dlgdbconnection.h:45: error: variable or field `communicate' declared void
.ui/dlgdbconnection.h:45: error: `communicate' declared as a `virtual' field

line 45 is the line:
void dlgDBConnection::communicate( QSqlDatabase *par1)

If I am replacing the QSqlDatabase pointer with a pointer to an int it works OK.
Can anyone help me with this?
Thanks alot,
tDuma

admin edit:
please use the code tags. tuxipuxi

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

2

Sunday, October 26th 2003, 11:15am

Open the Dialog in Designer, switch the object explorer to Members and add

Source code

1
class QSqlDatabase;

to the Forward declarations.

Btw, instead of "/home/oduma" you should use QDir::homeDirPath()

Cheers,
_
Qt/KDE Developer
Debian User

tDuma

Beginner

  • "tDuma" is male
  • "tDuma" started this thread

Posts: 5

Location: ireland

  • Send private message

3

Sunday, October 26th 2003, 11:41am

It works!!!

Thanks alot! It works now.

Can you point me to some documentation regarding what exactly means Forward Declarations. Somehow the search functionality of my QT Assistant is not working so I have to go chapter by chapter and do an "eyeometric" :)) search. :D

tuxipuxi

Unregistered

4

Sunday, October 26th 2003, 11:49am

hi,

forward declarations are used to make classes accessible which are defined later.
for example you want a pointer to a QPushButton in your class, but you include qpushbutton.h in your implementation file. afaik it makes the compile process faster.. but not sure.

ciao,

tuxipuxi.

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

5

Sunday, October 26th 2003, 12:10pm

forward declaration is a C++ feature, so you won't find it in Qt's docs.

Its similar to a normal declaration but only introduces the name of a type.

For example a full declaration of a class looks like this

Source code

1
2
3
4
5
class MyClass
{
public:
    void setNumber(int number);
};

It tells the compiler there is a class called MyClass and that it has a method setNumber which takes an integer argument and has no return type.

As said above, a forward declaration only introduces the type's name

Source code

1
class MyClass;

The ompiler now knows that there is a class named MyClass, but does not know anything other about it.
But this is enough if it is only used to describe the type of a pointer or reference.

Cheers,
_
Qt/KDE Developer
Debian User