Double-click to close the QTabWidget label and double-click the qtabwidget label.

Source: Internet
Author: User

Double-click to close the QTabWidget label and double-click the qtabwidget label.

Using Qt for ARM, we found that the native close button (X) of QTabWidget in Qt4 is too small, and it is difficult to press it on the touchpad. The double-click function of the browser is similar to that of the browser. because I used the C # resource manager before, I thought I could bind DoubleClick directly, but I did not find the corresponding SLOT after I searched it, the result is to capture the signal in QWidget, that is, the mouse event without the QTabWidget tag! Then, I searched for various questions on the Internet. The results found various questions, and each one answered exactly ..... finally, let's think about it. If it doesn't work, use the dumbest method to implement it Like Hock!

So, I have reloaded the QTabWidget (because tabBar () is protected, ah !), In this way, the tag can be obtained.

1 class Tab : public QTabWidget2 {3     Q_OBJECT4 public:5     Tab(QWidget *parent = 0);6     QTabBar* GetBar();7 protected:8     void mousePressEvent(QMouseEvent *event);9 };

Then, when implementing an Event Filter, first determine whether the event is a double-click event, and then determine whether the event is a tag location. If yes, the current tab is deleted. Because the double-click event is required to trigger a click, that is, the selected tab event. Therefore, you do not need to double-click another tab to change the index.

1 # ifndef MYEVENTFILTER_H 2 # define MYEVENTFILTER_H 3 # include <QMainWindow> 4 # include <QMouseEvent> 5 # include "tab. h "6 7 extern int tabindex_current; 8 extern int tabindex_old; 9 extern Tab * tabWidget; 10 extern QPoint tableft; 11 extern int tabwidth; 12 extern int tabheight; 13 14 // double-click to close the Tab label. 15 class myEventFilter: public QObject16 {17 public: 18 myEventFilter (): QObject () 19 {}; 20 ~ MyEventFilter () {}; 21 22 bool eventFilter (QObject * object, QEvent * event) 23 {24 if (event-> type () = QEvent: MouseButtonDblClick) 25 {26 QMouseEvent * e = static_cast <QMouseEvent *> (event); 27 QPoint pos = e-> pos (); 28 int x1 = tableft. x (); 29 int x2 = tableft. x () + tabwidth; 30 int y1 = tableft. y (); 31 int y2 = tableft. y () + tabheight; 32 if (pos. x ()> = x1 & pos. y ()> = y1 & pos. x () <= x2 & pos. y () <= y2) 33 tabWidget-> removeTab (tabindex_current); 34} 35 return QObject: eventFilter (object, event); 36}; 37 }; 38 39 # endif // MYEVENTFILTER_H

Finally, bind it to the main function to capture all the events:

1 qApp->installEventFilter(new myEventFilter());

In addition, you need to update the width when switching the tab (the height does not need to be updated ):

1 void MainWindow::updateBar()2 {3     tabindex_current = tabWidget->currentIndex();4     tabindex_old = tabindex_current;5     QTabBar *bar = tabWidget->GetBar();6     if (bar->size().width() > 0)7          tabwidth = bar->size().width();8 }


 

  

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.