void MyTabBar::slotMoveLeft() { QString buttonName = this->sender()->objectName(); int index = buttonName.toInt(); if (index == 0) return; emit swapTabs(index - 1, index); }
void MyTabBar::slotMoveRight() { QString buttonName = this->sender()->objectName(); int index = buttonName.toInt(); if (index == count() - 1) return; emit swapTabs(index, index + 1); }
bool TabWidget::eventFilter(QObject *obj, QEvent *event) { if (obj==tabBar()) { // this code handles the tab movement if (event->type() == QEvent::Leave) { QPoint point = cross->mapToGlobal( QPoint(0, 0) ); QRect rect(point.x(), point.y(), cross->width(), cross->height() ); if ( !rect.contains( QCursor::pos() ) ) cross->hide(); } else if (event->type() == QEvent::HoverMove && m_closeButtonInTabs ) { QHoverEvent *mouseEvent = static_cast<QHoverEvent *>(event); mousePos = mouseEvent->pos(); for (int i=0; i<tabBar()->count(); i++) { if ( tabBar()->tabRect(i).contains( mouseEvent->pos() ) ) { m_clickedItem= i; break; } } cross->setGeometry(tabBar()->tabRect(m_clickedItem).x()+tabBar()->tabRect(m_clickedItem).width()-cross->width()-5, //tabBar()->height()-(tabBar()->tabRect(m_clickedItem).height()/2), 5, cross->width(), cross->height()); cross->show(); } else if (event->type() == QEvent::MouseButtonRelease ) { qApp->restoreOverrideCursor(); } else if (event->type() == QEvent::MouseButtonPress ) { QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); for (int i=0; i<tabBar()->count(); i++) { if ( tabBar()->tabRect(i).contains( mouseEvent->pos() ) ) { m_clickedItem = i; break; } } if ( mouseEvent->button() == Qt::LeftButton ) qApp->setOverrideCursor( Qt::OpenHandCursor ); if ( mouseEvent->button() == Qt::RightButton ) { QMenu *menu = new QMenu(this); connect(menu->addAction(QIcon(":/images/cross.png"), tr("Close Tab")), SIGNAL(triggered()), this, SLOT(slotCloseTab()) ); connect(menu->addAction(QIcon(":/images/fileclose.png"), tr("Close Other Tabs")), SIGNAL(triggered()), this, SLOT(slotCloseOtherTab()) ); connect(menu->addAction(QIcon(":/images/fileclose.png"), tr("Close All Tabs")), SIGNAL(triggered()), this, SLOT(slotCloseAllTab()) ); menu->exec(mouseEvent->globalPos()); delete menu; } } else if (event->type() == QEvent::MouseMove ) { QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); for (int i=0; i<tabBar()->count(); i++) { if ( tabBar()->tabRect(i).contains( mouseEvent->pos() ) ) { if ( swapTabs(i, m_clickedItem) ) { setCurrentWidget(widget(i)); update(); int x; if ( !tabBar()->tabRect(i).contains( mouseEvent->pos() ) ) { if ( tabBar()->tabRect(m_clickedItem).x() < tabBar()->tabRect(i).x() ) x = tabBar()->tabRect(i).x(); else x = tabBar()->tabRect(i).x()+(tabBar()->tabRect(i).width()-(qAbs(tabBar()->tabRect(i).width()-tabBar()->tabRect(m_clickedItem).width()))); QPoint point = QPoint( x, mouseEvent->pos().y() ); point = widget(i)->mapToGlobal( point ); m_clickedItem = i; QCursor::setPos ( point.x(), QCursor::pos().y() ); } m_clickedItem = i; break; } } } } } return QTabWidget::eventFilter( obj, event); }