passing an object from one form to the other (in qt applications using c++ ).
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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());
}
#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();
}
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.
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:
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.