Qtabwidget Implement similar QQ chat window (drag to detach a new window)

Source: Internet
Author: User

New version of the QQ Chat window can be implemented to drag, separate a new window. Browsers and other software can also achieve similar operations. So a whim wanted to use QT to achieve similar functions. Want to use Qtabwidget direct realization is very difficult, carefully read the source code, found that Qtabwidget internal is composed of Qstackedwidget and qtabbar combination of implementation. So it's natural to think that as long as the subclass inheriting the Qtabbar is re-implementing Qtabbar's

[CPP]View PlainCopy
    1. void Mousepressevent (Qmouseevent *e);
    2. void Mousemoveevent (Qmouseevent *e);
    3. void Mousereleaseevent (Qmouseevent *e);

These three methods, and then the definition of the Tabbar set to Qtabwidget is almost. However, there is a problem, Qtabwidget Settabbar method is protected, so we have to customize a qtabwidget subclass, in the subclass called Settabbar. Here's my simple implementation:

[CPP]View PlainCopy
  1. Class Mytabbar: PublicQtabbar
  2. {
  3. Q_object
  4. Public
  5. Mytabbar (Qwidget *parent = NULL);
  6. ~mytabbar ();
  7. Protected
  8. void Mousepressevent (Qmouseevent *e);
  9. void Mousemoveevent (Qmouseevent *e);
  10. void Mousereleaseevent (Qmouseevent *e);
  11. Signals:
  12. void Stardragtab (int index);
  13. void Enddragtab ();
  14. Private
  15. void drag ();
  16. bool M_isdrag;
  17. Qpoint M_mousepresspoint;
  18. Qpoint M_mousereleasepoint;
  19. };

[CPP]View PlainCopy
    1. void Mytabbar::mousemoveevent (Qmouseevent *e)
    2. {
    3. int index = this->currentindex ();
    4. if (M_isdrag)
    5. {
    6. Qpoint point = E->pos ();
[CPP]View PlainCopy
  1. //Drag the vertical distance greater than bar height, then create a new window, this should be better understood AH
  2. if (Qabs (M_mousepresspoint.y ()-Point.y ()) > this->height ())
  3. {
  4. Emit Stardragtab (index);
  5. }
  6. }
  7. Qtabbar::mousemoveevent (e);
  8. }
  9. void Mytabbar::mousepressevent (Qmouseevent *e)
  10. {
  11. if (e->button () = = Qt::leftbutton)
  12. {
  13. M_isdrag = true;
  14. M_mousepresspoint = E->pos ();
  15. }
  16. Qtabbar::mousepressevent (e);
  17. }
  18. void Mytabbar::mousereleaseevent (Qmouseevent *e)
  19. {
  20. if (e->button () = = Qt::leftbutton)
  21. {
  22. M_mousereleasepoint = E->pos ();
  23. if (Qabs (M_mousepresspoint.y ()-M_mousereleasepoint.y ()) > this->height ())
  24. {
  25. Emit Enddragtab ();
  26. }
  27. }
  28. Qtabbar::mousereleaseevent (e);
  29. }

[CPP]View PlainCopy
  1. Class Mytabwidget: Publicqtabwidget
  2. {
  3. Q_object
  4. Public
  5. Mytabwidget (Qwidget *parent = NULL);
  6. ~mytabwidget ();
  7. Private Slots:
  8. void Stardrag (int index);
  9. void EndDrag ();
  10. Private
  11. Mytabbar *m_ptabbar;
  12. QString M_dragtablabel;
  13. Qwidget *m_pdragwidget;
  14. int m_dragindex;
  15. };

[CPP]View PlainCopy
  1. Mytabwidget::mytabwidget (qwidget *parent/* = null*/): Qtabwidget (parent)
  2. {
  3. This->setacceptdrops (true);
  4. Setmousetracking (true);
  5. M_ptabbar = New Mytabbar (this);
  6. M_ptabbar->setmovable (true);
  7. Settabbar (M_ptabbar); //Set the custom bar to Tabwidget
  8. M_pdragwidget = NULL;
  9. Connect (m_ptabbar,signal (stardragtab (int)),This,slot (Stardrag (int)));
  10. Connect (m_ptabbar,signal (Enddragtab ()),This,slot (EndDrag ()));
  11. }
  12. void Mytabwidget::stardrag (int index)
  13. {
  14. M_dragtablabel = This->tabtext (index);
  15. M_pdragwidget = this->widget (index);
  16. M_dragindex = index;
  17. QPIXMAP pix;
  18. //pix = Qpixmap::grabwidget (m_pdragwidget);
  19. PIX = Qpixmap::grabwindow (this->winid ());
  20. if (Pix.isnull ())
  21. {
  22. int i;
  23. }
  24. Qmimedata *mimedata = new Qmimedata;
  25. //mimedata->settext ("drag tab");
  26. Qdrag *drag = new Qdrag (this);
  27. Drag->setmimedata (Mimedata);
  28. Drag->setpixmap (pix.scaled (Qsize (200,200))); //Here is mainly to think about QQ, you can drag the process to display the picture is dragged window. But the effect is not very good.
  29. Drag->exec ();
  30. }
  31. void Mytabwidget::enddrag ()
  32. {
  33. Mytabwidget *pwidget = new Mytabwidget (NULL);
  34. if (m_pdragwidget)
  35. {
[CPP]View PlainCopy
      1. //Create a separate window
      2. RemoveTab (M_dragindex);
      3. Pwidget->addtab (M_pdragwidget,m_dragtablabel);
      4. Pwidget->show ();
      5. }

http://blog.csdn.net/hai200501019/article/details/8987379

Qtabwidget Implement similar QQ chat window (drag to detach a new window)

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.