contextmenu - Dynamically create context menu in QT associated to a toolbutton -


in application have qtoolbutton related presence of usb pen drive. when pen drive inserted show qtoolbutton , create context menu associated content of pen drive.i have different menu dynamically created assigned button.

my code works first time, when create new menu doesn't appear. in last version of code, when show button second time the previous menu (dismount item present) , when click on item doesn't anything.

edit: if use qaction instead of qwidgetaction code works fine. seems related qwidgetaction of qlabel used inside of it.

the following simplified version of code:

/* member variables */ qmenu *m_pqmconmenusb; qlabel m_menuitem;  /* costructor */     ui->tbdriveusb->setcontextmenupolicy(qt::customcontextmenu); m_pqmconmenusb = null; qobject::connect(ui->tbdriveusb, signal(customcontextmenurequested(const qpoint&)),this, slot(showusbcm(const qpoint&))); m_menuitem.setstylesheet("qlabel { background-color : black; color : white; }"); m_menuitem.settext("dismount"); qfont fonte = m_menuitem.font(); fonte.setpixelsize(16); m_menuitem.setfont(fonte); qpalette chepalette = m_menuitem.palette(); m_menuitem.setminimumsize(0,32); chepalette.setcolor(m_menuitem.backgroundrole(), qt::black); chepalette.setcolor(m_menuitem.foregroundrole(), qt::white); m_menuitem.setpalette(chepalette);  /*member functions*/ void  mainwindow::showusbcm(const qpoint& pos) {     // copied example     if (pos != qpoint(0,0)) {         // execute context menu         if (m_pqmconmenusb!=null) m_pqmconmenusb->exec(pos);     } }  void mainwindow::onusbmounted() {     /* static boolean used simulate change in menu content */     static bool trytochange = false;     ui->tbdriveusb->show();     m_pqmconmenusb = new qmenu(this);     qwidgetaction *menuitemw = new qwidgetaction(this);     menuitemw->setdefaultwidget(&m_menuitem);     menuitemw->settext("dismount");     connect(menuitemw,signal(triggered()), this, slot(dodismount()));     m_pqmconmenusb->addaction(menuitemw);     if (trytochange)     {         menuitemw = new qwidgetaction(this);         menuitemw->setdefaultwidget(&m_menuitem);         menuitemw->settext("update");         connect(menuitemw,signal(triggered()), this, slot(update()));         m_pqmconmenusb->addaction(menuitemw);     }     trytochange = !trytochange;     ui->tbdriveusb->setmenu(m_pqmconmenusb); }  void mainwindow::onusbdismounted() {    ui->tbdriveusb->hide();     /* first version of code tries destroy menu following code, doesn't work    /*ui->tbdriveusb->setmenu(null);    qaction *paction;    foreach (paction, m_pqmconmenusb->actions())        paction->disconnect(this);        delete(m_pqmconmenusb);     m_pqmconmenusb = null;*/ 

}

a useful pattern dynamically populate menu associated qtoolbutton first create menu , attach button, not populate it. connect slot qmenu::abouttoshow() signal. in slot implementation, clear contents of menu , populate needed current state of application.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -