Qt implements the drag-out and double-click bounce function of the window tabwidget

Source: Internet
Author: User
Tags emit
This function seems simple and is indeed difficult to implement. In the code. // Ctabwidget. h # ifndef ctabwidget_h # define ctabwidget_h # include <qtabwidget> # include <qtgui> # include "ctabbar. H "class ctabwidget: Public qtabwidget {q_objectpublic: ctabwidget (qwidget * = 0); Virtual ~ Ctabwidget () {}; public: ctabbar * tabbar;}; # endif // ctabwidget_h // ctabwidget. CPP # include "ctabwidget. H "ctabwidget: ctabwidget (qwidget * parent): qtabwidget (parent) {tabbar = new ctabbar; settabbar (tabbar); // This is the key, in this way, use our custom ctabbar to replace the original qtabbar} // ctabbar. h # ifndef ctabbar_h # define ctabbar_h # include <qtabbar> # include <qtgui> class ctabbar: Public qtabbar {q_objectpublic: ctabbar (qwidget * = 0); Virtual ~ Ctabbar () {}; protected: void mousepressevent (qmouseevent *); void mousereleaseevent (qmouseevent *); // use two events to simulate the private: bool pressflag; signals: void sig_tabdrag (INT, qpoint) ;};# endif // ctabbar. CPP # include "ctabbar. H "# include <qtgui> ctabbar: ctabbar (qwidget * parent): qtabbar (parent), pressflag (false) {} void ctabbar: mousepressevent (qmouseevent * event) {If (Event-> button () = QT: leftbutton) {pressf Lag = true;} qtabbar: mousepressevent (event);} void ctabbar: mousereleaseevent (qmouseevent * event) {If (Event-> button () = QT :: leftbutton & pressflag) {pressflag = false; If (tabrect (currentindex ()). contains (Event-> pos () return; emit sig_tabdrag (currentindex (), event-> pos () ;}} now implements the double-click action on the title bar of the window. // cwidget. h # ifndef cwidget_h # define cwidget_h # include <qtgui/qwidget> class cwidget: Public qwidget {q_obj Ectpublic: cwidget (qwidget * = 0 );~ Cwidget (); protected: bool event (qevent *); signals: void sig_doubleclickedtitlebar (); // signal sent when being double-clicked}; # endif // cwidget. CPP # include "cwidget. H "# include <qtgui> cwidget: cwidget (qwidget * parent): qwidget (parent) {} cwidget ::~ Cwidget () {} bool cwidget: event (qevent * event) {# ifdef UNIX if (Event-> type () = qevent: mousebuttondblclick) {// when the title bar is clicked, click the window edge to replace it. emit sig_doubleclickedtitlebar (); Return true ;}# endif if (Event-> type () = qevent: nonclientareamousebuttondblclick) // This event is not triggered in Linux. It should be considered a bug in QT {emit sig_doubleclickedtitlebar (); Return true;} return qwidget: event (event );} // cmainwindow. h # ifndef mainwindow_h # define mainwindow_h # include <qmainwindow> # include <qtextedit> # include "ctabwidget. H "# include" ctabbar. H "class cmainwindow: Public qmainwindow {q_objectpublic: cmainwindow (qwidget * = 0); Private: ctabwidget * tabwidget; private slots: void slot_tabbardoubleclicked (); // response double-click the poll function void slot_tabdrag (INT index, qpoint point); // The Void slot_closetab (INT) that responds to the drag action ); // close the tab slot function}; # endif // mainwindow_h // cmainwindow. CPP # include "cmainwindow. H "# include" ctabwidget. H "# include" cwidget. H "# include <qtgui> cmainwindow: cmainwindow (qwidget * parent): qmainwindow (parent) {tabwidget = new ctabwidget (this); tabwidget-> setmovable (true ); tabwidget-> settabsclosable (true); tabwidget-> settabshape (qtabwidget: triangular); // Add four tab pages, tabwidget-> addtab (New qtextedit, "eidt 1 "); tabwidget-> addtab (New qtextedit, "eidt 2"); tabwidget-> addtab (New qtextedit, "eidt 3"); tabwidget-> addtab (New qtextedit, "eidt 4"); setcentralwidget (tabwidget); Connect (tabwidget-> tabbar, signal (sig_tabdrag (INT, qpoint), this, slot (slot_tabdrag (INT, qpoint); Connect (tabwidget, signal (tabcloserequested (INT), this, slot (slot_closetab (INT); Connect (tabwidget, signal (currentchanged (INT )), tabwidget, slot (setcurrentindex (INT); resize (800,600);} void cmainwindow: slot_tabdrag (INT index, qpoint point) {cwidget * widget = new cwidget; qwidget * draged = tabwidget-> widget (INDEX); qstring windowname = tabwidget-> tabtext (INDEX); tabwidget-> removetab (INDEX); Connect (widget, signal (sig_doubleclickedtitlebar (), this, slot (evaluate (); qgridlayout * layout = new qgridlayout; Layout-> addwidget (draged); widget-> setlayout (layout ); widget-> resize (600,400); widget-> move (point + pos () + tabwidget-> pos (); widget-> setwindowtitle (windowname ); widget-> show (); draged-> show ();} void cmainwindow: slot_tabbardoubleclicked () {cwidget * widget = qobject_cast <cwidget *> (sender ()); qobjectlist list = widget-> Children (); qtextedit * edit = NULL; For (INT I = 0; I <list. count (); ++ I) {If (list [I]-> inherits ("qtextedit") {edit = qobject_cast <qtextedit *> (list [I]); break ;}}if (edit = NULL) {return;} edit-> setparent (tabwidget); tabwidget-> addtab (edit, widget-> windowtitle ()); delete widget;} void cmainwindow: slot_closetab (INT index) {qwidget * draged = tabwidget-> widget (INDEX); tabwidget-> removetab (INDEX); Delete draged ;} // main. CPP # include "cmainwindow. H "# include <qapplication> # include <qcleanlooksstyle> int main (INT argc, char ** argv) {qapplication app (argc, argv); qapplication :: setstyle (New qcleanlooksstyle); qtextcodec: setcodecfortr (qtextcodec: codecforname ("gb2312"); cmainwindow mainwindow; mainwindow. show (); Return app.exe C ();}

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.