Tracking separators in a menu (insertSeparator)
(Qt 3)
In the QMenuData class there is an "int" return value when using the insertSeparator method but the text doesn't explain what exactly it is returning ...?
Here is the text:
int QMenuData::insertSeparator ( int index = -1 )
Inserts a separator at position index. The separator becomes the last menu item if index is negative.
In a popup menu a ......etc
What is this int value? The reason I ask is that I need to track separators and remove them if they are side by side in the menu (later on in the code I hide certain menu items according to the user logged in). I thought I could use this int value to track the separators but the values are odd (like -36 for example).
Anyone try to track separators in a menu (I need to be able to track them like regular menu items)?
Thanks
Re: Tracking separators in a menu (insertSeparator)
Maybe success == 0, failure < 0?
You should check the sources of Qt. You'll probably find your answer easily there.
Re: Tracking separators in a menu (insertSeparator)
I believe it returns a menu id, like
int QMenuData::insertItem().
You can use int QMenuData::removeItem(). To remove separators using this id.
Re: Tracking separators in a menu (insertSeparator)
Quote:
Originally Posted by Chicken Blood Machine
I believe it returns a menu id, like
int QMenuData::insertItem().
That was my first thought too, but the poster said these numbers are negative, which is unlikely for an id.
Re: Tracking separators in a menu (insertSeparator)
I don't know why separators are treated so differently than other menu objects but I am having difficulty removing separators that are side by side. Maybe someone can check my logic found below.
PreCondition: Menus and separators have already been added, the weird number that adding a separator returns is stored in a vector called separatorVec, some menu's have been hidden per the users login permissions.
What I want to do: Remove spare separators that are now stacked on top of each other so that the logged in user has no indication that menu items are missing. The code below is JUST going to check if the separator is the first visible item in them menu and remove it if it is. (it doesn't work, the code never removes a separator even if 2 separators are at the very top of the menu)
Code:
void MainScreenView::SeparatorRemovalHelper(QPopupMenu *theMenu)
{
//check the menu for separator weirdness
for (int i = 0; i < theMenu->count(); i++)
{
int currId = theMenu->idAt(i);
//loop the separatorVec and compare the funky ID's
for (int k = 0; k < separatorVec.size(); k++)
{
int sepId = separatorVec[k];
if (currId == sepId)
{
//found a separator
//make sure its not the first visible item in the menu
//work backwards from i position to test for menu
//items that are visible
bool foundVisible = false;
int j=0;
for (j = i; j != 0; j--)
{
if (theMenu->isItemVisible(j-1) == true)
{
foundVisible = true;
break;
}
}
//if didn't find a visible item from i position back
//then remove it
if (foundVisible == false)
{
theMenu->removeItem(sepId);
break;
}
}//end IF currId == sepId
}//end for int k = 0
}
}