bool MainWindow::eventFilter( QObject *watched, QEvent *event ) { std::cerr << "\nIn MainWindow::eventFilter()\n"; if ( event->type() == QEvent::MouseButtonPress ) { std::cerr << "event->type()==MouseButtonPress\n"; if ( QApplication::mouseButtons() & Qt::LeftButton ) { if ( is_over_tabBar ) { int index = -1; QTabBar *tab_bar = ui->tabWidget->findChild<QTabBar*>(); if ( tab_bar ) { QPoint tab_pos = tab_bar->mapToGlobal( QPoint(0,0) ); index = tab_bar->tabAt( QCursor::pos() - tab_pos ); } emit mouseClickedOverTabBar( index ); } } } QTabBar *tab_bar = ui->tabWidget->findChild<QTabBar*>(); if ( tab_bar ) { QPoint tab_pos = tab_bar->mapToGlobal( QPoint(0,0) ); int tab_x = tab_pos.x(); int tab_y = tab_pos.y(); int tab_width = tab_bar->width(); int tab_height = tab_bar->height(); QRect tab_rect( tab_x, tab_y, tab_width, tab_height ); if ( tab_rect.contains( QCursor::pos() ) ) { is_over_tabBar = true; } else { is_over_tabBar = false; } } return QMainWindow::eventFilter( watched, event );}
C++ (Qt)bool MyAppFilter::eventFilter( QObject * object, QEvent * event ) { static int count = 0; if (!CapsLockPreessed()) return false; // желательно switch (event->type()) { case QEvent::Timer: case QEvent::MouseMove: // чтобы слишком не сорить в консоли break; default: qDebug() << ++count << object << event->type(); } return false; }
C++ (Qt)void QTabBar::mousePressEvent(QMouseEvent *event){ Q_D(QTabBar); const QPoint pos = event->pos(); const bool isEventInCornerButtons = (!d->leftB->isHidden() && d->leftB->geometry().contains(pos)) || (!d->rightB->isHidden() && d->rightB->geometry().contains(pos)); if (!isEventInCornerButtons) { const int index = d->indexAtPos(pos); emit tabBarClicked(index); }
C++ (Qt)class MyAppFilter : public QObject { public: MyAppFilter( QObject * parent ) : QObject(parent) { } bool eventFilter( QObject * object, QEvent * event ) { ... // код выше }}; // ИспользованиеqApp->installEvenrFilter(new MyEventFilter(qApp));
QTabBar *tab_bar = ui->tabWidget->findChild<QTabBar*>(); tab_bar->installEventFilter( this );
bool MainWindow::eventFilter( QObject *watched, QEvent *event ) { QTabBar *tab_bar = ui->tabWidget->findChild<QTabBar*>(); if ( watched == tab_bar && event->type() == QEvent::MouseButtonPress ) { if ( QApplication::mouseButtons() == Qt::LeftButton ) { QPoint tab_pos = tab_bar->mapToGlobal( QPoint(0,0) ); int tab_x = tab_pos.x(); int tab_y = tab_pos.y(); int tab_width = tab_bar->width(); int tab_height = tab_bar->height(); QRect tab_rect( tab_x, tab_y, tab_width, tab_height ); if ( tab_rect.contains( QCursor::pos() ) ) { int index = tab_bar->tabAt( QCursor::pos() - tab_pos ); tabBarClicked( index ); } } } if ( moving ) { if ( QApplication::mouseButtons() & Qt::LeftButton ) { dragCurrentPosition = QCursor::pos(); if ( already_need_to_drag_the_tab() ) { perform_drag_operation(); moving = false; } } else { moving = false; } } return QMainWindow::eventFilter( watched, event );}
MyTabWidget::MyTabWidget(QWidget *parent) : QTabWidget( parent ){ MyTabBar *tab_bar = new MyTabBar( parent ); tab_bar->addTab( "One" ); tab_bar->addTab( "Two" ); tab_bar->addTab( "Three" ); this->setTabBar( tab_bar ); connect( tab_bar, SIGNAL( tabBar_mouseLeftButtonClicked(int) ), parent, SLOT( tabBarClicked(int) ) );}