CCMenuItemImage isn't showing the selected state - cocos2d-iphone

The documentation for CCMenuItemImage doesn't actually say what it does.
There are quite a few subclass CCMenuItem.
I've inherited a project that's using it as a button.
CCMenuItem *start;
start = [CCMenuItemImage itemFromNormalImage:[self prefixedImage:#"start button.png"]
selectedImage:[self prefixedImage:#"start button selected.png"]
target:myTarget
selector:#selector(start:)];
It was using the same button for both states.
I modified it to have a different image for the selected state.
I was expecting/hoping that when I touch the item it will be highlighted, and when I release the button it will send my target action (which it does).
(aside: in iOS parlance, i know that highlighted and selected are two different things. But this library does not seem to have that difference.)
So:
Is it intended to use this "menu item" as a button?
When is the selected image of this menu item displayed?
How should I go about making it display as selected?

CCMenuItem is an abstract class from which all of the other menu items inherit so what you did there in the code is technically wrong.
On the other hand you could subclass CCMenuItem to make your own custom class (for example: you can't use a button and a label on it as a menu item, you have to use either the button itself and the label is on top..just for show, or use the label and the button below is...pointless)
Subclassing CCMenuItem and making your own class would fix that problem (i mean you could make a method that would take an image and a string and returns a button)
What you want to do there is this:
CCMenuItemImage *button= [CCMenuItemImage itemFromNormalImage:#"start button.png"
selectedImage:#"start button selected.png"
target:self
selector:#selector(start:)];
CCMenu *start=[CCMenu menuWithItems:button,nil];
start.position=ccp(200,200);
[self addChild:start];
When you put your finger on the menu it will replace the normal image with the selected one, but will only activate of you release it in the boundingbox of the button (aka..you can press on the button, move your finger away from the button and it wont activate).
So in a sence the button is highlighted untill you release your finger, then its selected.
Did that answer your question?

Try this code...
CCMenuItemImage *backbtn = [CCMenuItemImage itemFromNormalImage:#"backbtn.png" selectedImage:#"backbtn_selected.png" target:self selector:#selector(LBback)];
CCMenu *Menu1 = [CCMenu menuWithItems:backbtn,nil];
[Menu1 alignItemsVerticallyWithPadding:15];
Menu1.position = ccp(160, 240);
[self addChild:Menu1];
By the help of this..when you touch on image is shows selected image other wise normal image...:)
and later when your function get called and you want to change its image then you can set like this..
[backbtn setNormalImage:[CCSprite spriteWithFile:#"backbtn_selected.png"]];

The code above is correct.
The image resource for selection was not added to the project, so was not being displayed. It may have output an error message on creation (buried in other output), but did not output error message when tapped.
The silent/safe failure made the user error harder to track down.

Related

QTableView in a popup on a QPushButton click

Click the button, a table control appears next to the button, click the blank space or the content in the control to close.
I have two ideas:
With the popmenu, can click the blank space or the content in the control to close the control, but it does not meet my needs for a table-type control
New a tableview, but I don’t know how to accurately appear under the button and realize the function of clicking the blank space or the content in the control to close the control
Update:
I need to use Qt to implement a function, but I don't know how to implement it.
The functions are as follows:
Click a QPushButton, and a QTableView is displayed below the button. I can click anywhere outside the QTableView to close this QTableView
There are two things I cannot achieve:
1.How to display the QTableView below the QPushButton (must be immediately below, such as:
(source: upload.cc)
)
2.How to close the QTableView by clicking the blank space outside the QTableView
If you can make all of this in the same window, it's simple...
To position the QTableView below the QPushButton you will use Qt Designer, and setup the QTableView to hide();. And when the QPushButton is clicked, he setup the QTableView to show();.
An show(); example:
QTableView *nametable = ui->tableView;
nametable->show(); //will appears
But to close or hide(); this when clicked in a blank space I don't know how to help you...

QAction with icon displays checkbox on top

I have a menu with checkable QActions in it: (ed: these calls are within a QWidget subclass, so 'this' points to the associated widget).
QAction* action = new QAction(icon, actionName, this);
action->setCheckable(true);
action->setActionGroup(m_actionGroup);
menu->addAction(action);
This menu is attached to a tool button, which is in a QWidget with a few other tool buttons. Previously, it was part of a larger widget, but I've been refactoring to confine some things together.
When it was previously part of the larger widget, these actions and their icons displayed fine. Now, however, the icons are drawn in a dotted pattern and a box for a check box is drawn on top. When I hover over an action, the dotted action goes away entirely, and it's just the check box. When I select an action, the icon appears normally.
I've tried doing the same in a little toy program, and the menu displays correctly then, so I'm having trouble reproducing the issue outside of my rather large program. Has anyone seen behaviour like this before, or know how to fix it?
Qt 5.5 on Ubuntu 16.04 is the platform information.
Edit: It gets even stranger. The original implementation created the QMenus without parents. Which I would have avoided because of memory leak concerns. But if you create the menu with the widget holding the button as a parent, then you get this behaviour. It's something about the QMenu being constructed with a parent that is creating issues, not the QAction.

How to avoid conflit between MenuItemImage & MenuItemSprite in Cocos2d-x 3.15.1

First I'll explain what I want to do before seeing the code, the user can click to the PLAY Button and after click, a popup Menu is displayed, the Menu contain 2 MenuItemSprite for play with bot or friend, for the MenuItemImage it's to display a small background around 2 MenuItemSprite.
The problem is when I want to click on a MenuItemSprite inside the Menu, nothing is happening, but when I set enable the Background to false all is okay without any problem, But I don't want this solution because I have inside another background (almost transparent). If the user click to this background, he can hide automatically the Menu, so I said set enable the background to false it'll give another problem conflit between Background (Window) and background (Menu).
Code :
//Background (Scene)
background=Sprite::create(BACKGROUND);
background->setPosition(SonarCocosHelper::UI::GetScreenCenter());
background->setOpacity(0);
this->addChild(background,1);
//Background Menu
MenuItemImage * overlayWindowItem=MenuItemImage::create(GAME_OVER_WINDOW,GAME_OVER_WINDOW,GAME_OVER_WINDOW,NULL);
//overlayWindowItem->setEnabled(false);
//FRIEND ITEM
MenuItemSprite * friendItem=MenuItemSprite::create(Sprite::create(FRIEND_BUTTON), Sprite::create(FRIEND_BUTTON),CC_CALLBACK_1(MenuScene::goToPlay,this));
friendItem->setTag(PLAY_WITH_FRIEND);
friendItem->setPosition(Vec2(-overlayWindowItem->getContentSize().width/4,friendItem->getPositionY()));
//BOT ITEM
MenuItemSprite * botItem=MenuItemSprite::create(Sprite::create(BOT_BUTTON), Sprite::create(BOT_BUTTON),CC_CALLBACK_1(MenuScene::goToPlay,this));
botItem->setTag(PLAY_WITH_BOT);
botItem->setPosition(Vec2(overlayWindowItem->getContentSize().width/4,botItem->getPositionY()));
//menu
menu=Menu::create(overlayWindowItem,friendItem,botItem,NULL);
menu->setPosition(Vec2(SonarCocosHelper::UI::GetScreenCenter().x,SonarCocosHelper::UI::GetScreenCenter().y+screenSize.height));
this->addChild(menu,1);
How I can avoid conflit between MenuItemImage & MenuItemSprite, I want to click on MenuItemSprite, MenuItemImage is just a background.
Thank you,
As I can see in your code, why are you taking overlayWindowItem as MenuItemImage? You are not calling any function (Set to NULL in your code).
Simply take GAME_OVER_WINDOW as an sprite. Please correct me if I am telling anything wrong.

Cocos2d CCMenuItem not responding

I'm having some trouble with have a CCMenuItem button work. I've followed all instructions/advice given in previous examples and questions, but nothing seems to work. Whenever I click the button, the image doesn't even change to the new image.
CCMenuItem *startButton = [CCMenuItemImage itemWithNormalImage:#"Start Button.png" selectedImage:#"Start Button Selected.png" target:self selector:#selector(startGame:)];
startButton.position = ccp(0, 0);
CCMenu *menu = [CCMenu menuWithItems:startButton, nil];
menu.position = ccp(winSize.width/2, winSize.height/6);
[self addChild:menu];
menu.touchEnabled = YES;
you probably want to use the
menu.enabled=YES;
that property will control whether touch events are processed. The property you are using (touchEnabled) is a basic property of the CCLayer object (CCMenu extends CCLayer), and that controls whether the layer will receive touch events or not.
Although, the 'enabled' property is set to YES during initialization (its default state). It is possible that by using the other method, you are altering the propagation of touch events. Try first to comment out the line.

Qt: Disable a Push Button without turning it gray

I need to make a checkable Push Button in Qt 4.8 that when is checked it becomes disabled.
The problem that I have is that the button turns gray and I need to keep it with the same color always. I have two questions for two possibles paths to follow:
1) Is there a way to disable the gray out effect when I use button.setEnabled(false)?
2) Is there a way to hook the click event so I can "simulate" the disabled property?
Thanks in advance!
[Edit] To give a little context, I have two push buttons that should toggle each other and that's why I need to prevent clicking on a pressed button.
Try button.blockSignals(true). You could also overwrite the look of the button when its disabled with a Qt style sheet.
To give a little context, I have two push buttons that should toggle
each other and that's why I need to prevent clicking on a pressed
button.
You should use QButtonGroup instead.
The only way that the button should be unchecked is when the user
checks the other button...
From doc:
In an exclusive group, the user cannot uncheck the currently checked
button by clicking on it; instead, another button in the group must be
clicked to set the new checked button for that group.
For disabling toolbutton and put icon of your choice instead of turning into grey
you have to load icon creating QPixmap object say qpm,
create QIcon object say icon
icon.addPixmap(qpm,QIcon::Disabled,QIcon::On) this line will show icon
setIcon on toolbutton
setStyleSheet
When enabling Toolbutton just change
3. icon.addPixmap(qpm,QIcon::Normal,QIcon::On)