PDA

View Full Version : Load menu from database



daica2003
10th May 2008, 04:09
Hi everybody !

I have a database which contains all Menu Items ,and I set up a connection to load all these items to Menu The code is :


.....
for(int x = 0; x < myResults->record().count(); x++)
{
QString fieldName=myResults->value(x).toString();
mAction= new QAction(QIcon(myIconPath+"/mCollectPatrol.png"), tr(fieldName), this);
...
if(x==3)
connect(mAction, SIGNAL(triggered()), this, SLOT(fieldName));
....

It's successfully loaded item into my menu ,but the Slot doesnot work ! I also looked for QMETAOBJECT but i don't understand it .I'm using QT 4.3.1 .Can anyone help me to find the solution of this problem ?

Thank for help!

Le Son
------------
Software Developer -Hanoi University of Science

wysota
10th May 2008, 08:42
What does "fieldName" contain? SLOT expects a regular function signature - for example "someName()" and not "somename". Furthermore you have to pass a char * and not a QString - you should use qPrintable() or some other method to get a const char* out of the string.

janus
10th May 2008, 15:01
I am doing something like this to get a menu from the database. Actually i am a newbie as well .-) so i don't know if it is the best way. But it works.

agFilter is an actionGroup
meListe is a menu



QString sTypName;
QSqlQuery q;
int i = 0;
q.exec("SELECT typ_name FROM typ");
while (q.next()){
sTypName = q.value(0).toString();
acList[i] = new QAction(this);
acList[i]->setText(sTypName);
acList[i]->setCheckable(true);
agFilter->addAction(acList[i]);
meListe->addAction(acList[i]);
connect(acList[i], SIGNAL (triggered(bool)), this, SLOT (typFilter()));
i++; }

daica2003
11th May 2008, 12:00
Hi Janus,

I've checked your code ,but what is the slot 'typFilter()' ? Is it the same with all the action in actionGroup .If yes, it's not good because you still write Slot function in your code .My purpose is to design a simple CMS (Content Management System) in QT .You don't need to know coding ,just only work with CMS System. So, I design a Table like this :

tableMenu (MenuName,ActionName,ActionContent,..)

with the meaning is: Load 'MenuName' from tableMenu and assign ActionName ,ActionContent to this menuName .

So when i run program ,if i want to add a new menu Item, i just add new item to database without coding and recompiling project.Do you have any solution for this problem?

Many Thanks for help.

Le Son
---------------
Software Developer -Hanoi University of Science

janus
11th May 2008, 15:40
I did not understand, that you want to load the actions/slots as well from the database. Sry, I have no idea how to do that.

wysota
11th May 2008, 18:18
To make a slot name connectable, you need to build a string containing a "2", a slot name and slot parameters and pass it instead of the SLOT() macro. So if you want a slot "execute" with a parameter of type "int", you need to create a string "2execute(int)".


connect(sender, SIGNAL(triggered()), receiver, "2execute(int)");