PDA

View Full Version : Reference Code over and over again (Solved)



ShapeShiftme
18th May 2011, 15:38
Ok i dont know how to best put this but this is what i want to do.

I would like to write a bunch of connect singnals and insert them into a file aor something of the sort then reference them in my code again.


For example i have multiple functions that create the following connects



connect(this, SIGNAL(setwebview(QString)), child, SLOT(reloadme(QString)));
connect(this, SIGNAL(backpressed()), child, SLOT(backispressed()));
connect(child, SIGNAL(openInvoice(QString,QString)),this, SLOT(openInvoice(QString,QString))); connect(child, SIGNAL(setBackStack(QString)), this, SLOT(setstack(QString)));


Now lets say i would like to do a change in that code i currently have to change the connects in about 45 places. So what im hoping to di is write the connects someplace once then whenever i need the code i can just refernce the file of sorts

in coldfusion i would use


<cfinclude template="code.cfm">
Or something of that sort.

So is it possible.

Regards

wysota
18th May 2011, 15:59
In C/C++ this is called a "function", possibly a template function (C++) and you can implement it as such. Or as a macro, as a matter of fact.

ShapeShiftme
18th May 2011, 16:10
Would you be willing to give me more info on how to implement it

or is it as simple as creating a funcion and then calling it. eg



void MainWindow::createconnect()
{
connect(this, SIGNAL(setwebview(QString)), child, SLOT(reloadme(QString)));
connect(this, SIGNAL(backpressed()), child, SLOT(backispressed()));
connect(child, SIGNAL(openInvoice(QString,QString)),this, SLOT(openInvoice(QString,QString)));
connect(child, SIGNAL(setBackStack(QString)), this, SLOT(setstack(QString)));
}

void MainWindow::openWebUrl()
{
frm_web *child = new frm_web;
ui->mdi1->addSubWindow(child);
//create connecction
createconnect();
}




Any help would be appreciated

wysota
18th May 2011, 16:18
void createConnect(QObject *src, QObject *dst) {
connect(src, SIGNAL(setwebview(QString)), dst, SLOT(reloadme(QString)));
connect(src, SIGNAL(backpressed()), dst, SLOT(backispressed()));
connect(dst, SIGNAL(openInvoice(QString,QString)), src, SLOT(openInvoice(QString,QString)));
connect(dst, SIGNAL(setBackStack(QString)), src, SLOT(setstack(QString)));
}

void MainWindow::openWebUrl()
{
frm_web *child = new frm_web;
ui->mdi1->addSubWindow(child);
//create connecction
createConnect(this, child);
}

Or using a macro:


#define createConnect(src, dst) \
connect(src, SIGNAL(setwebview(QString)), dst, SLOT(reloadme(QString))); \
connect(src, SIGNAL(backpressed()), dst, SLOT(backispressed())); \
connect(dst, SIGNAL(openInvoice(QString,QString)), src, SLOT(openInvoice(QString,QString))); \
connect(dst, SIGNAL(setBackStack(QString)), src, SLOT(setstack(QString)));

void MainWindow::openWebUrl()
{
frm_web *child = new frm_web;
ui->mdi1->addSubWindow(child);
//create connecction
createConnect(this, child);
}

ShapeShiftme
18th May 2011, 17:00
Thank you so much for your help.
The macro option worked perfectly.

Have A great day