PDA

View Full Version : How to set the QAction's action



aatwo
18th March 2011, 11:08
Hey guys :)

I am writing a QT application and I have hit a bit of a wall.

I have a file containing an undetermined number of data entries (blocks of 512 bytes). Each data entry contains a collection of variables as well as a data entry name, and a data entry category.

I have created the code to look at this file and generate a QMenu. Basically the code creates sub-menus for each unique category found in the file, and then obtains the name of each data entry and places it in the correct category.

This works fine and the menu appears as expected however the next step is where I am struggling. I have a function that requires arguments extracted from the data entry.

So my question is this :) How can I add the required function calls to the QActions?

Here is an extract of the relevant part of my code! :)



// Create the menu structure

// *********************
// ** CREATE BASE MENU
QMenu *menu = new QMenu((QWidget*)ui->button);
// *********************
for(int i = 0; i < numberOfCategories; i++){
// *******************
// ** CREATE SUB MENU
QMenu *submenu = new QMenu(dataEntryCategoryNameList[i], (QWidget*)menu);
// *******************
// ** FIND ALL DATA ENTRIES THAT ARE PART OF THE CURRENT CATEGORY
for(int j = 0; j < dataEntryCount; j++){
if(memcmp(dataEntryCategoryNameList[i], &dataEntryRawDataList[j][220], 30)==0){
// **********************
// ** ADD ACTION TO MENU
char temp[30];
memset(temp, 0x00, 30);
memcpy(temp, &dataEntryRawDataList[j][0], 30);
// OBTAIN RELEVANT COMMAND OPTIONS FOR THE CURRENT DATA ENTRY
int var1 = /*value obtained from file*/
int var2 = /*value obtained from file*/
int var3 = /*value obtained from file*/
int var4 = /*value obtained from file*/
int var5 = /*value obtained from file*/

//Function I want to call for the current data entry:
// myFunction(var1, var2, var3, var4, var5)
QAction *action = new QAction(temp, (QObject*)submenu);
submenu->addAction(action);
// **********************
}
}
menu->addMenu(submenu);
}
menu->exec(QCursor::pos());


Thanks for reading and I appreciate any help! :D

MarekR22
18th March 2011, 11:57
store custom data somewhere for example QAction::setData
if you what fetch those data when action is invoked then in slot connected to this action do:

void SomeQObject::someSlot()
{
QAction* action = qobject_cast<QAction*>(sender());
if (!action) {
return;
}
if (!action->data().isNull) {
yourDataType data = qvariant_cast<yourDataType>(action->data());
....
}
}

aatwo
18th March 2011, 13:40
Nice. Cunning idea, thanks very much! :D

I do have one problem with it though. Since I have been using QT creator this is the first time I have had to manually code the signal+slot process so I am not sure what to do.


I presume I am meant to use the connect() function just after I have created the action to send a triggered() signal to my mainWindow widget, but I am a little confused by the functions arguments.




// Class mainWindow...

void mainWindow::createMenu(){
...
// Set action as int ID of current command
int value = getValue();
QAction myaction = new QAction(temp, (QObject*)subMenu);
myaction->setData(QVariant(value));

connect(myAction, triggered(), (QWidget*)this, /*NO IDEA WHAT TO PUT HERE :P*/
...
}


void mainWindow::mySlot()
{
QAction* action = qobject_cast<QAction*>(sender());
if (!action) {
return;
}
if (!action->data().isNull) {
yourDataType data = qvariant_cast<yourDataType>(action->data());
char data[400];
memset(data, 0x00, 400);
memcpy
}
}


Am I on the right lines?

Thanks again :)

pan
18th March 2011, 13:54
You probably have already figured this out:

connect(myAction, SIGNAL(triggered()), this, SLOT(mySlot()));

The help has a good example of how to use connect.

aatwo
18th March 2011, 14:49
Brilliant I got it working thank you. I never realised the slot system could be so simple. The function prototype definitely looks 10000 times more intimidating than it actually is :D thanks for all your help!


I actually have one last question if it is not too much trouble :P In my code I have used several new operators however I have not used any matching delete operators (memory leak?). Do they self destruct?

Added after 9 minutes:

Never mind I assume that YES i was causing a memory leak and fixed it by adding an array of pointers for the QAction and QMenu objects and simply ran through them at the end and deleted them :)

<3