0

Due to some reasons I need to implement specific menu in QToolBar. My target is to save the default design of a button with drop-down menu (icon + triangle), but I want to show my own QDialog (it looks like a tree) when I press this triangle.
So, now I have something like this:

class CustomMenu : public QMenu
{
  Q_OBJECT;

public:

  CustomMenu(QWidget *parent);

signals:
  void showMyMenu();
};

This is how I create this menu and connects it with needed slots:

CustomMenu *menu = new CustomMenu(tb);         
connect(menu, SIGNAL(aboutToShow()), menu, SIGNAL(showMyMenu()));
connect(menu, SIGNAL(showMyMenu()), this, SLOT(execMyMenu()));       
tbButton->setMenu(menu);
tb->addAction(tbButton);

Here's the slot:

void MyProjectClass::execMyMenu()
{

  CustomMenu *cm = (CustomMenu*)sender();

  CustomMenuWidget* myMenu = new CustomMenuWidget();        
  myMenu->exec();

  cm->hide();

  delete myMenu;
}

The problem here is that calling hide() for CustomMenu doesn't make triangle become unpressed and it still holds a focus on the button. That makes impossible to click something else until you don't get rid of the focus from that button.
Everything I want is to substitute default drop-down menu with my custom one.
It seems to be very easy, but I probably missed something. What am I doing wrong?
I'm using Qt 4.8.6

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
tema
  • 1,115
  • 14
  • 33
  • is there any way to programmatically tell QMenu to close, because `close()` and `hide()` doesn't do that? – tema Aug 31 '15 at 14:33
  • You can change a focus after destroying a menu – Dmitry Sazonov Aug 31 '15 at 15:02
  • @SaZ how can I do it? – tema Aug 31 '15 at 18:32
  • @SaZ actually, I'm not pretty sure about connecting to `aboutToShow()` signal, but I didn't find the way to reimplement how default menu appears. I've tried to reimplement `show()`, `exec()`, `popup()` but none of them are called when the default menu appears on the screen. – tema Sep 01 '15 at 08:18
  • I misunderstood something. Why do you need to inherit `QMenu`? – Dmitry Sazonov Sep 01 '15 at 08:44
  • If you need a custom content inside any menu you may use `QWidgetAction` class – Dmitry Sazonov Sep 01 '15 at 08:45
  • @SaZ the problem is quiet different: I have specific QDialog which I need to show when I press on QAction in toolbar. This dialog contents specific presentation of data and I can't change it. I have already tried to do this with `QWidgetAction`, but this didn't help. Now, I'm trying to cheat with `QMenu` by catching its signal before opening to show my dialog. Everything works great, but when I press 'apply' button which causes dialog to close I can't force `QMenu` to close and the small down-arrow on the right side remains pressed. That's why I'm asking about way to close it programmatically – tema Sep 01 '15 at 09:12

0 Answers0