QT Implementation drag tabwidget tab to form a separate window, double-click the title bar to restore the function dock function __qt

Source: Internet
Author: User
Tags emit

Recently do QT, I am also a novice, this is mainly used to reconstruct the parent-class event, that is, subclasses.

This article is seen on the http://blog.csdn.net/zmm19861210/article/details/9036779, but I made a certain change [note: The code below is not reflected, or the old code], I did a destructor modification, Is the heap of those new out of the private variable to delete, because the code is debugged on the host, the company uses Remote Desktop way to the Internet, so there is no authority to upload Remote Desktop to the entire demo is really a pity.

This feature seems simple, and it's really a bit difficult to implement.
Explain in detail in the code. CTabWidget.h #ifndef ctabwidget_h #define CTABWIDGET_H #include <QTabWidget> #include <QtGui> #include "
	CTabBar.h "class Ctabwidget:p ublic qtabwidget {q_object public:ctabwidget (qwidget*);
Virtual ~ctabwidget () {};
Public:ctabbar *tabbar;
}; #endif//Ctabwidget_h//ctabwidget.cpp #include "CTabWidget.h" Ctabwidget::ctabwidget (Qwidget *parent): Qtabwidget (
	Parent) {Tabbar = new Ctabbar;     Settabbar (Tabbar); Here is the key to replacing the original Qtabbar}//ctabbar.h with our custom Ctabbar #ifndef ctabbar_h #define CTABBAR_H #include <QTabBar>
	Include <QtGui> class Ctabbar:p ublic qtabbar {q_object public:ctabbar (qwidget* =0);
Virtual ~ctabbar () {};
	Protected:void mousepressevent (qmouseevent *);   void Mousereleaseevent (Qmouseevent *);
Through two events to simulate the tab is dragged by the action 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 () ==q
    T::leftbutton) {Pressflag = 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 implement the window title bar Double-click Action//cwidget.h #ifndef cwidget_h #define CWIDGET_H #include <QtGui/QWidget> class Cwidget:publ
	IC Qwidget {q_object public:cwidget (qwidget* = 0);
~cwidget ();
Protected:bool Event (qevent *);  Signals:void Sig_doubleclickedtitlebar ();
Signal to be fired when 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) {//title bar Click No response, click on the edge of the window to replace the bar emit Sig_doubleclickedtitlebar ();
    return true; #endif if (event->type () = = Qevent::nonclientareamousebuttondblclick)//This event is not triggered under Linux, it should be a bug in qt {em
		it Sig_doubleclickedtitlebar ();
	return true;
Return Qwidget::event (event); //cmainwindow.h #ifndef mainwindow_h #define MAINWINDOW_H #include <QMainWindow> #include <QTextEdit> #inc  Lude "CTabWidget.h" #include "CTabBar.h" class Cmainwindow:public Qmainwindow {q_object Public:cmainwindow (qwidget* =  
0);
Private:ctabwidget *tabwidget;            Private Slots:void slot_tabbardoubleclicked ();  Response to double-click the bounce slot function void Slot_tabdrag (int index,qpoint point);                    slot function void Slot_closetab (int) in response to the drag action;

Closes tab's slot function}; #endif//Mainwindow_h//cmainwindow.cpp #include "CMainWindow.h" #include "CTabWidget.h" #include "CWidget.h" #include & Lt Qtgui> Cmainwindow::cmainwindow (QWidget *parent): Qmainwindow (parent) {tabwidget = new Ctabwidget (this);
    Tabwidget->setmovable (TRUE);
    Tabwidget->settabsclosable (TRUE);
    Tabwidget->settabshape (Qtabwidget::triangular);
    Add 4 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 (slot_tabbardoubleclicked ()));
    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 = Qobj
            Ect_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.exec (); }

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.