Some windows need to display a small buoy on the desktop when they are minimized, allowing users to perform various operations using this small Buoy (such as thunder's floating window ), I tried to use QT to implement this function.
PS: I thought this function was very simple, but it took me two nights, tears...
Ideas:
1: Create a widget consisting of a small icon, which can be clicked, dragged, and transparent in the background.
2: Reload the changeevent of the main window (qlabel in this example) (for details, see the code)
(The light green part is the desktop background)
Main form:
Small buoy:
Code:
# Include <qtgui> # include <windows. h> // class miniconwidget: Public qwidget {q_object PRIVATE: qpoint m_currentpos; qpixmap m_pixmap; protected: void mousepressevent (qmouseevent * event); void mousemoveevent (qmouseevent * event ); void paintevent (qpaintevent * event); void enterevent (qevent * event); void leaveevent (qevent * event); void mousedoubleclickevent (qmouseevent * event); public: miniconwidget (qwidget * parent = 0); void setpixmap (const qpixmap & pixmap); signals: void doubleclickedsignal () ;}; miniconwidget: miniconwidget (qwidget * parent ): qwidget (parent) {// set the slider widget to a top-level window; no title bar; toolbar mode; transparent background; setwindowflags (QT: frameless=whint | QT :: windowstaysontophint | QT: tool); setattribute (QT: wa_translucentbackground, true); setfocuspolicy (QT: nofocus);} void miniconwidget: mousepressevent (qmouseevent * event) {// press and hold the left button to move the window. You can drag the window if (Event-> button () = QT: leftbutton) {m_currentpos = event-> globalpos ()-framegeometry (). topleft (); event-> Accept () ;}} void miniconwidget: mousemoveevent (qmouseevent * event) {If (Event-> buttons () & QT: leftbutton) {move (Event-> globalpos ()-m_currentpos); event-> Accept () ;}} void miniconwidget: paintevent (qpaintevent * event) {qpainter painter (this ); painter. drawpixmap (0, 0, m_pixmap);} void miniconwidget: leaveevent (qevent * event) {// The common pointer setcursor (QT: arrowcursor) when the mouse leaves the window );} void miniconwidget: enterevent (qevent * event) {// when the mouse stays on the window, it is a finger setcursor (QT: pointinghandcursor);} void miniconwidget: mousedoubleclickevent (qmouseevent * event) {emit doubleclickedsignal ();} void miniconwidget: setpixmap (const qpixmap & pixmap) {m_pixmap = pixmap; Update ();} class mylabel: Public qlabel {q_objectprivate: miniconwidget * m_minicon; protected: void changeevent (qevent * event); Public: mylabel (qwidget * parent = 0); Public slots: void showmainwindow (); void hidemainwindow () ;}; mylabel :: mylabel (qwidget * parent/* = 0 */): qlabel (parent) {m_minicon = new miniconwidget; m_minicon-> setpixmap (qpixmap ("delete.png"); setwindowflags (QT:: windowstaysontophint); // double-click the float to hide the slider and restore the main window (a label here) connect (m_minicon, signal (doubleclickedsignal (), this, slot (showmainwindow ();} void mylabel: changeevent (qevent * event) {// focus, minimize the window (nonsense) when the window is minimized ), show the slider // hide the slider when the window is restored. Restore the main window (here is a label) if (Event-> type () = qevent: windowstatechange) {// changeevent will be called before the window is minimized. If no qtimer is added, // we hide the window, but QT thinks the window is about to be minimized, in this case, if (windowstate () & QT: windowminimized) qtimer: singleshot (0, this, slot (hidemainwindow (); else if (windowstate () & QT: windownostate) qtimer: singleshot (0, this, slot (showmainwindow ();} qlabel: changeevent (event);} void mylabel: showmainwindow () {m_minicon-> hide (); move (m_minicon-> pos (); show (); setwindowstate (QT: windownostate);} void mylabel: hidemainwindow () {hide (); m_minicon-> move (Pos (); m_minicon-> show (); setwindowstate (QT: windowminimized);} # include "Main. MOC "int main (INT argc, char ** argv) {qapplication app (argc, argv); mylabel label; label. setstylesheet ("qlabel {font-size: 30px; color: red;}"); label. resize (400,200); label. settext ("this is a simple demo. "); label. show (); Return app.exe C ();}