Qtabwidget tab Implementation Double-click the Closed method (recommended) _java

Source: Internet
Author: User

Using QT to do arm, found in the QT4 qtabwidget the original closing button (X) is too small, with a touch pad difficult to press. As a result, like a browser to double-click the shutdown function, because before the C # of the resource manager, thought can be directly binding DoubleClick, but later did not find the corresponding slot, the results in Qwidget capture signal, is not qtabwidget tag mouse events, Hang on, Dad! Then, the search on the Internet, the results found is a variety of problems, each one accurately answered ... Finally reasoning, really not, use the most stupid method, imitation hock realize!

As a result, I overloaded the qtabwidget (because Tabbar () is protected, Pit Dad! ) so that you can get the label.

Class Tab:public Qtabwidget
 {
 q_object public
 :
 Tab (Qwidget *parent =);
 qtabbar* Getbar ();
 Protected:
 void Mousepressevent (Qmouseevent *event);
 

Then, in implementing an event filter, you first decide that the event is a double-click event, and then determine whether it is the label position, and if so, delete the current tab, because the click in the double-click Event will trigger, that is, the label page selects the event, so there is no need to consider the index change caused by

 #ifndef myeventfilter_h #define MYEVENTFILTER_H #include <QMainWindow> #include & Lt
 qmouseevent> #include "tab.h" extern int tabindex_current;
 extern int tabindex_old;
 extern Tab *tabwidget;
 extern Qpoint Tableft;
 extern int tabwidth;
 extern int tabheight;
 Double-click to close tab 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->remo
 Vetab (tabindex_current);
 Return Qobject::eventfilter (object,event);
 };
 }; #endif//Myeventfilter_h 

Finally, bind to the main function main, so that you can catch all the events:

Qapp->installeventfilter (New Myeventfilter ());

In addition, you need to update the width information (height is not required) when the tab page is switched:

 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 a small set to introduce the qtabwidget tag to achieve double click off the method (recommended), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.