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.



  • So, really, your question is not about QToolButton, but about QMenu.
    Did you try adding a [[Doc:QWidgetAction]] to your menu?



  • 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);
    }
    @



  • No, I meant that you use a QWidgetAction to add to your QMenu in your orignal code section on lines 8 and 9. Not that you change how you construct the tool button.



  • oh, I see. thank you very much. It is useful for me!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.