LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2008, 04:44 PM   #1
cbarmpar
LQ Newbie
 
Registered: May 2008
Distribution: Ubuntu 9.04
Posts: 7

Rep: Reputation: 0
passing an object from one form to the other (in qt applications using c++ ).


Hi all,

I am new to both Linux and c++.

I just made the next step to start making applications that make sense but it seems that i get confused easy.

I have a main window(kentriko class). I have a dialogue used to initiate a database connection (class database). This dialogue is called from the mainwindow to create the database connection. The dialogue return the database ( *d.get_db() ).

I want to use this connection to make queries from another form called kentriko (kentriko class). This class is subwindowed. This allows me to instate many instances of this window all of the using the same database.

The problem is that I am making a mistake in the constructors somehow and the pointers to the database are not passed. the code is listed bellow.

Any help will be much appreciated.

parathiro implementation:
Code:
#include "parathiro.h"
#include <iostream>
#include <QString>
#include "../diafores_sinartisis/sinartisis.h"
#include "../database/database.h"

using namespace std;
parathiro::parathiro(QWidget *parent,database  d)//<-- here i pass the oject (the database connection dialog)//
    : QMainWindow(parent)
{
	ui.setupUi(this);
	connect(ui.anixe, SIGNAL(clicked()) , this , SLOT(anixe()));
	connect(ui.pare, SIGNAL(clicked()) , this , SLOT(pare()));
	connect(ui.proto,SIGNAL(textChanged(const QString&)),this,SLOT(alagi()));
}

void parathiro::alagi(){
  QSqlQuery erotisi(*d.get_db());//<- here i attempt to use it//
  proto = sinartisi_SELECT( "onoma,epitheto" ,"onomata", "onoma", ui.proto->text());
  ui.deytero->setText(proto);
  erotisi.exec(proto);
  variant = erotisi.size();
  ui.trito->setText(variant.toString());
}
parathiro header:

Code:
#ifndef PARATHIRO_H
#define PARATHIRO_H

#include <QtGui/QMainWindow>
#include "ui_parathiro.h"
#include "../database/database.h"

class parathiro : public QMainWindow
{
    Q_OBJECT
public:
    parathiro(QWidget *parent = 0, database d );
    ~parathiro();
private slots:
     void anixe();
     void pare();
     void alagi();
private:
    Ui::parathiroClass ui;
    QString  proto;
    QString  deytero;
    QVariant variant;
};
kentriko implementation:
Code:
#include "kentriko.h"
#include <iostream>
#include <QString>
#include "../diafores_sinartisis/sinartisis.h"
#include <QMdiArea>
#include <QMdiSubWindow>
#include "../kentriko/parathiro/parathiro.h"

using namespace std;

kentriko::kentriko(QWidget *parent)
    : QMainWindow(parent)
{
	ui.setupUi(this);
	connect(ui.neo_parathiro, SIGNAL(triggered()), this, SLOT(anixe()));
	connect(ui.sindesi,SIGNAL(triggered()),this,SLOT(sindesi()));
}
void kentriko::anixe(){
    parathiro *para = new parathiro(0,d);//<- here i am trying to pass it to the new parathiro//
	QMdiSubWindow *subwindow = ui.mdiArea->addSubWindow(para);
	subwindow->setAttribute(Qt::WA_DeleteOnClose);
	subwindow->resize(sizeHint());
	subwindow->show();
}

void kentriko::sindesi(){
	database* d;//<<<<- here i am creating the pointer to the database class//
	d->show();
}
database implementation:
Code:
#include "database.h"

database::database(QWidget *parent) :
	QDialog(parent) {
	ui.setupUi(this);
	db = QSqlDatabase::addDatabase("QMYSQL");
	connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
	connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
	connect(ui.pliroforia_k, SIGNAL(clicked()), this, SLOT(pliroforia()));
}
void database::sindesou() {
	ena.pinakas = ui.pinakas->text();
	ena.onoma = ui.onoma->text();
	ena.kodikos = ui.kodikos->text();
	if (!db.isOpen()) {
		db.setDatabaseName(ena.pinakas);
		db.setUserName(ena.onoma);
		db.setPassword(ena.kodikos);
		db.setHostName("127.0.0.1");
		db.setPort( 3306);
		if (!db.open()) {
			ui.katastasi->setText("Not connected");
		} else {
			ui.katastasi->setText("connected");
		}
	} else {
		ui.katastasi->setText("It has already been connected");
	}
}
void database::aposindesou() {
	db.close();
}
void database::pliroforia() {
	ena.pliroforia = ui.pliroforia->text();
}
A * database::get_alpha() {
	return &ena;
}
QSqlDatabase * database::get_db() {
	return &db;//<<<<---- here i am returning the database connection from the database class in order to use it on the parathiro.
}
database::~database() {
	db.close();
}

Many thanks in advance.

Last edited by cbarmpar; 09-22-2008 at 04:45 PM.
 
Old 10-08-2008, 12:12 AM   #2
rlhawk
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Rep: Reputation: 0
Quote:
parathiro *para = new parathiro(0,d);//<- here i am trying to pass it to the new parathiro//
The problem I see here is that nowhere have you initialized database *d. When creating this parathiro object, you haven't initialized d. I would probably suggest getting some books and reading up on pointers.

Parathiro is supposed to be passed a QWidget pointer, and a database. (Should be a database pointer actually.) I'm not familiar with QT, but I'm guessing you need to do something like this:
Code:
QWidget * myQWidget = new QWidget([params?]);
database *d = new database(myQWidget);
parathiro *para = new parathiro(myQWidget,d);
Since d isn't defined in your class header, parathiro::alagi() doesn't have access to it. So, in your parathiro class, add something like:
Code:
database *paraDb;
in the private section, and then make your parathiro constructor assign d to paraDb. Then you can use paraDb wherever in your parathiro class.

If the database is created like in my example (database *d = new database(myQWidget) then you can just put that in your constructor instead of passing the pointer to the constructor.

Also, make sure you follow all those new's with corresponding delete's when you're done using them.

I'm sorry that this probably isn't very clear. I hope it will help point you in the right direction at least somewhat. Also, feel free to ask me more questions. I'm no expert, but I can share what I do know.

Last edited by rlhawk; 10-08-2008 at 12:13 AM.
 
Old 10-08-2008, 01:33 PM   #3
cbarmpar
LQ Newbie
 
Registered: May 2008
Distribution: Ubuntu 9.04
Posts: 7

Original Poster
Rep: Reputation: 0
Many thanks for the reply. I will have a look now

Regards.
 
Old 10-08-2008, 03:12 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Just some advice: I find it best to segregate GUI components from functional components. For one, because you might want to change GUIs later or modify the one you have.

I generally program on three levels with GUIs:
  1. GUI. For Qt, I prefer to code just enough in the GUI classes to call a function further back. For GTK, I prefer to isolate GUI operations in a fairly generic interface, e.g. "regenerate," "update," etc. within a class.
  2. Functional front-end. It becomes much simpler to debug and make changes when you put your "real" code behind the GUI itself. The functional front-end (not a formal term as far as I know) should essentially appear to be an API of a shared library, allowing the GUI to perform everything non-GUI just using its functions.
  3. Functional back-end. The back-end is obviously behind the API above. Hiding it behind an API keeps it from getting intermingled with the GUI. Often times you'll still need to extern some functions for the GUI portion to define. Normally my goal is to write this so that it could just as easily serve a command-line UI merely by creating a main function that interprets arguments and makes API calls accordingly.
The major problem with integrating your GUI with your functional code is that GUIs and the "ideal" method of performing a process are generally fundamentally different. The best way to assemble and operate a GUI is rarely the best way to perform the operations behind them.

It might sound like I'm recommending you over-think and over-design simple things, but I can't tell you how many times I've searched for a bug for hours and hours, then just rewrote things in a "cleaner" way only to have the bug completely resolved. Now I just design everything from a high level initially and I don't have nearly the problems I used to with tracking down bugs.
ta0kira

PS With Linux people generally expect to do the same things on the command line as they can with a GUI (unlike Windows/Mac where the command line is a rare exception.) In other words, the "Linux way" is to only provide GUIs as a convenience for what could otherwise be done in a terminal. This inadvertently helps compartmentalize programs, which in my opinion increases reliability. This isn't everyone's preference, but Linux tends to be very script-intensive.

Last edited by ta0kira; 10-08-2008 at 03:18 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stop applications run in terminal form opening in X Oxagast Linux - Software 2 08-05-2008 09:05 AM
Bad form for an object to delete itself (C++)? ta0kira Programming 31 09-06-2006 12:49 AM
passing a QPainter object to widgets vratojr Programming 2 12-21-2005 02:46 AM
Passing form values between multiple forms!! AskMe Programming 5 09-07-2005 07:44 PM
form "post" not passing var niehls Programming 10 10-14-2003 05:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration