Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A problem whit QToolButton?
Forum Updated to NodeBB v4.3 + New Features

A problem whit QToolButton?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yunxiaodong
    wrote on last edited by
    #1

    @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.

    I love peace.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yunxiaodong
        wrote on last edited by
        #3

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

        I love peace.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            yunxiaodong
            wrote on last edited by
            #5

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

            I love peace.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved