I used Qt as ARM and found that the native close button (X) of QTabWidget in Qt4 is too small, and it is difficult to press with the touchpad. So I thought of a double-click-off function similar to the browser. Because I had worked on the C# resource manager before, I thought I could directly bind DoubleClick, but after searching through it, I couldn't find the corresponding SLOT. As a result, I caught the signal in the QWidget, which was a mouse event without the QTabWidget tag. That's a trick! Then, I searched various questions online, and found various questions, and each one answered accurately... Finally, after thinking about it, it really didn't work, so I used the stupidest method to imitate Hock!
So, I reloaded the QTabWidget (because tabBar() is protected, it's a scam!), so that I can get the tag.
class Tab : public QTabWidget { Q_OBJECT public: Tab(QWidget *parent = ); QTabBar* GetBar(); protected: void mousePressEvent(QMouseEvent *event); }; Then, when implementing an event filter, first determine whether the event is a double-click event, and then determine whether it is the label position. If so, delete the current tab page. Since the click must be triggered in the double-click event, that is, the tab page is selected, so there is no need to consider the index change problem caused by double-clicking other tab pages.
#ifndef MYEVENTFILTER_H #define MYEVENTFILTER_H #include <QMainWindow> #include <QMouseEvent> #include "tab.h" extern int tabindex_current; extern int tabindex_old; extern Tab *tabWidget; extern QPoint tableft; extern int tabwidth; extern int tabheight; //Implement double-click to close the Tab tag class myEventFilter: public QObject { public: myEventFilter():QObject() {}; ~myEventFilter(){}; bool eventFilter(QObject* object,QEvent* event) { if (event->type()==QEvent::MouseButtonDblClick) { QMouseEvent *e = static_cast<QMouseEvent*>(event); QPoint pos = e->pos(); int x = tableft.x(); int x = tableft.x()+tabwidth; int y = tableft.y(); int y = tableft.y()+tabheight; if (pos.x() >= x && pos.y() >= y && pos.x() <= x && pos.y() <= y) tabWidget->removeTab(tabindex_current); } return QObject::eventFilter(object,event); }; }; #endif // MYEVENTFILTER_H Finally, bind to the main function main, so that all events can be captured:
qApp->installEventFilter(new myEventFilter());
In addition, the width information needs to be updated when the tab page is switched (height is not required to be updated):
void MainWindow::updateBar() { tabindex_current = tabWidget->currentIndex(); tabindex_old = tabindex_current; QTabBar *bar = tabWidget->GetBar(); if (bar->size().width() > ) tabwidth = bar->size().width(); } The above is the method of double-click closing of the QTabWidget tag introduced to you by the editor (recommended). I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!