C++ (Qt)#ifndef MENUBAR_H#define MENUBAR_H//version 0.1.2#include <QtGui>#include <QToolBar>#include <QWidget>#include <QString>#include <QAction>#include <QMenu>#include <QToolButton> class menubar: public QToolBar{Q_OBJECTpublic: menubar(QWidget * parent=0); QMenu * menuOptions; QMenu * menuViews; QMenu * menuFavorits; QMenu * menuHelp; QToolButton * options; QToolButton * views; QToolButton * favorits; QToolButton * help;private slots:};#endif
C++ (Qt)#include "menubar.h"//menubar::menubar(QWidget * parent) { setWindowTitle(QString::fromUtf8("Меню")); //Кнопка "Опции" options=new QToolButton(this);// options->setIcon(QIcon(":/4vert/4vert.png")); options->setPopupMode(QToolButton::InstantPopup); QObject::connect(options,SIGNAL(clicked()),views,SLOT(pressed())); menuOptions=new QMenu; menuOptions->addAction(QString::fromUtf8("В разработке")); options->setMenu(menuOptions); //Кнопка "Вид" views=new QToolButton(this);// views->setIcon(QIcon(":/4vert/4vert.png")); views->setPopupMode(QToolButton::InstantPopup); QObject::connect(views,SIGNAL(clicked()),views,SLOT(pressed())); menuViews=new QMenu; menuViews->addAction(QString::fromUtf8("В разработке")); views->setMenu(menuViews); //Кнопка "Избранное" favorits=new QToolButton(this);// favorits->setIcon(QIcon(":/4vert/4vert.png")); favorits->setPopupMode(QToolButton::InstantPopup); QObject::connect(favorits,SIGNAL(clicked()),favorits,SLOT(pressed())); menuFavorits=new QMenu; menuFavorits->addAction(QString::fromUtf8("В разработке")); favorits->setMenu(menuFavorits); //Кнопка "Справка" help=new QToolButton(this);// help->setIcon(QIcon(":/4vert/4vert.png")); help->setPopupMode(QToolButton::InstantPopup); QObject::connect(help,SIGNAL(clicked()),help,SLOT(pressed())); menuHelp=new QMenu; menuHelp->addAction(QString::fromUtf8("В разработке")); help->setMenu(menuHelp); menubar::addWidget(options); menubar::addWidget(views); menubar::addWidget(favorits); menubar::addWidget(help);}
C++ (Qt)options=new QToolButton(this);options->installEventFilter(this); ... favorits=new QToolButton(this);favorits->installEventFilter(this);...
C++ (Qt)virtual bool eventFilter ( QObject * watched, QEvent * event );
C++ (Qt)bool menubar::eventFilter ( QObject * watched, QEvent * event ){ if (watched->inherits("QToolButton")) { if (event->type() == QEvent::HoverEnter) { //кнопка получила "фокус" мыши return true; } if (event->type() == QEvent::HoverLeave) { //кнопка потеряла "фокус" мыши return true; } return false; } return QToolBar::eventFilter(watched, event);}