A problem whit QToolButton?
-
@QToolButton* toolButton = new QToolButton;
toolButton->setIcon(QIcon(tr("f:\temp\icon\icon0.ico")));
toolButton->setText(tr("undo"));
toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolButton->setPopupMode(QToolButton::MenuButtonPopup);QMenu *menu = new QMenu;
menu->addAction("test 1");
menu->addAction("test 2");
toolButton->setMenu(menu);@The menu displayed when the arrow is pressed,it worked well. The question is : can anything else displayed when the arrow is pressed? how to do that? for example: a custom widget displayed when the arrow is pressed.
-
But QWidgetAction have no arrow option. I find a way to solve the problem just now. Derived from QToolButton, and implement the virtual fuction.
@void QToolButton::mousePressEvent(QMouseEvent e)
{
Q_D(QToolButton);
#ifndef QT_NO_MENU
QStyleOptionToolButton opt;
initStyleOption(&opt);
if (e->button() == Qt::LeftButton && (d->popupMode == MenuButtonPopup)) {
QRect popupr = style()->subControlRect(QStyle::CC_ToolButton, &opt,
QStyle::SC_ToolButtonMenu, this);
if (popupr.isValid() && popupr.contains(e->pos())) {
d->buttonPressed = QToolButtonPrivate::MenuButtonPressed;
showMenu();
return;
}
}
#endif
d->buttonPressed = QToolButtonPrivate::ToolButtonPressed;
QAbstractButton::mousePressEvent(e);
}@
*
reimplement it__@void gwToolArrowButton::mousePressEvent(QMouseEvent *e)
{
QStyleOptionToolButton opt;
initStyleOption(&opt);
if (e->button() == Qt::LeftButton)
{
QRect popupr = style()->subControlRect(QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButtonMenu, this);
if (popupr.isValid() && popupr.contains(e->pos())) //press the arrow rect,not the main button
{
//showMenu();//***********dont show menu show other widget ********************
QComboBox p = new QComboBox;
p->addItem("111");
p->addItem("222");
p->addItem("333");
p->addItem("444");p->show();
return;
}
}QAbstractButton::mousePressEvent(e);
}
@ -
oh, I see. thank you very much. It is useful for me!