Header file
# Ifndef shortcutsettingdlg_h # define shortcutsettingdlg_h # include <qwidget> # include <set> class qsingleshortcut; Class metadata: Public qdialog {q_objectpublic: partition (qwidget * parent );~ Qshortcutsettingdlg (); // description, and the corresponding shortcut key pointer. void Init (qstringlist & list, vector <qshortcut *> & P); Public slots: void slotfinish (); void slotcancel (); void slotenable (INT ); void slotkeywasset (const qstring & hotkey); Public: bool isenable () {return m_benableall ;}; void setenable (bool B) {m_pboxcheck-> setchecked (! B) ;}; PRIVATE: // vector <qstring> m_strdescription; vector <qshortcut *> * m_pshortcut; vector <qsingleshortcut *> m_vecpitem; // qpushbutton * m_pbtnok; // confirm the qpushbutton * m_pbtncancel button; // cancel the qcheckbox * m_pboxcheck; // single-member bool m_benableall; // bool m_bmodfied ;}; /* optional */class qsingleshortcut: publi C qwidget {q_objectpublic: qsingleshortcut (qwidget * parent, qstring, qshortcut *);~ Qsingleshortcut (); const qstring & text () {return m_strkey ;}; void reset () {m_plineedit-> settext (m_strkey) ;}; signals: void signalnewshortcut (const qstring &); // used to prevent duplication. protected: bool eventfilter (qobject * OBJ, qevent * eV); Private: qstring m_strkey; // Save the shortcut key that has been set on the interface. qstring m_newkey; // temporary shortcut key. when conditions are met, update the saved qshortcut * m_pshortcut; qlabel * m_plabel; qlineedit * m_plineedit;}; # endif // shortcutsettingdlg_h
Source File
# Include "stdafx. H "# include" shortcutsettingdlg. H "const int defalutwidth = 250; // default width const int defalutheight = 20; // default height const int defalutwspace = 25; // default interval const int defaluthspace = 5; // default interval: qshortcutsettingdlg (qwidget * parent): qdialog (parent), m_benableall (true), m_bmodfied (false), m_pshortcut (0) {setwindowflags (QT :: windowsystemmenuhint | QT: Window); setwindowtitle (qstring: fromlocal8bit (" Shortcut Key settings in the control bar... "); m_pboxcheck = new qcheckbox (qstring: fromlocal8bit (" Disable hotkey "), this); m_pboxcheck-> settristate (false); m_pboxcheck-> setcheckstate (QT :: unchecked); m_pbtnok = new qpushbutton (qstring: fromlocal8bit ("OK"), this); m_pbtncancel = new qpushbutton (qstring: fromlocal8bit ("canceled"), this ); int origin = 5; m_pboxcheck-> setgeometry (20, origin, 100); m_pbtnok-> setgeometry (, origin,); m_pbtncancel-> setgeom Etry (195, origin, 300); resize (, origin + 25); Connect (m_pbtnok, signal (clicked (), this, slot (slotfinish ())); connect (m_pbtncancel, signal (clicked (), this, slot (slotcancel (); Connect (m_pboxcheck, signal (statechanged (INT), this, slot (slotenable (INT); qpalette pal; pal. setbrush (qpalette: window, qbrush (qcolor (60, 60, 60); setpalette (PAL); setstylesheet ("qcheckbox, qlabel {color: White ;};");} qshortcutsettingdlg ::~ Callback () {} void qshortcutsettingdlg: slotenable (INT state) {// connect (m_pboxcheck, signal (statechanged (INT), this, slot (slotdisable (INT ))); // If (State = QT: unchecked & m_benableall) // | (State = QT: checked &&! M_benableall) // return; If (State = QT: unchecked) {m_benableall = true; For (INT I = 0; I <m_vecpitem.size (); I ++) {m_vecpitem [I]-> setenabled (m_benableall) ;}} else if (State = QT: checked) {// disable all hotkeys and set the flag at the same time. // It Seems convenient to add an intermediate layer. m_benableall = false; For (INT I = 0; I <m_vecpitem.size (); I ++) {m_vecpitem [I]-> setenabled (m_benableall );}}} void qshortcutsettingdlg: Init (qstringlist & list, vector <qshortcut *> & VEC) {// data ens Ure (list. size () = Vec. size (); m_p1_cut = & VEC; For (INT I = 0; I <Vec. size (); I ++) {m_vecpitem.push_back (New qsingleshortcut (this, list. at (I), VEC. at (I); Connect (m_vecpitem [I], signal (signalnewshortcut (qstring), this, slot (slotkeywasset (qstring )));} // layout int origin = defaluthspace; For (INT I = 0; I <m_vecpitem.size (); I ++) {// shortcut m_vecpitem [I]-> setgeometry (defalutwspace, origin, defalutwidth, defalutheight); origi N + = defalutheight + defaluthspace;} // button m_pboxcheck-> setgeometry (20, origin, 100); m_pbtnok-> setgeometry (, origin, 80, 20 ); m_pbtncancel-> setgeometry (195, origin, 300); setfixedsize (, origin + 25);} void qshortcutsettingdlg: slotfinish () {If (m_benableall) {qstring STR; for (INT I = 0; I <m_vecpitem.size (); I ++) {STR = m_vecpitem [I]-> text (); m_p0000cut-> at (I) -> setkey (qkeysequence (STR) ;}} else {qstring STR; (Int I = 0; I <m_vecpitem.size (); I ++) {// STR = m_vecpitem [I]-> newtext (); m_p0000cut-> at (I) -> setkey (qkeysequence (STR) ;}} done (true);} void qshortcutsettingdlg: slotcancel () {If (m_bmodfied) {int ret = qmessagebox :: warning (this, qstring: fromlocal8bit ("warning! "), Qstring: fromlocal8bit (" do you want to save the existing changes? "), Qmessagebox: Save | qmessagebox: discard | qmessagebox: Cancel, qmessagebox: Save); If (ret = qmessagebox: Save) slotfinish (); if (ret = qmessagebox: Cancel) return;} done (false);} void qshortcutsettingdlg: slotkeywasset (const qstring & hotkey) {qstring STR; qsingleshortcut * P = (qsingleshortcut *) sender (); For (INT I = 0; I <m_vecpitem.size (); I ++) {STR = m_vecpitem [I]-> text (); // m_strkey; If (Str. compare (hotkey) = 0) {If (P = m_vecpitem [I]) // It helps a lot... continue; // if it is the same control, P-> Reset (); qtooltip: showtext (maptoglobal (p-> pos () + qpoint (), qstring:: fromlocal8bit ("overlapping shortcut keys! "), This); break ;}}/ * consumed ** partition */qsingleshortcut: qsingleshortcut (qwidget * parent, qstring qstr, qshortcut * pshortcut ): qwidget (parent), m_pshortcut (pshortcut) {m_plabel = new qlabel (qstr, this); m_plineedit = new qlineedit (this); m_strkey = m_pshortcut-> key (). tostring (); m_newk Ey = m_strkey; keys-> settext (m_strkey); this-> setfixedsize (, 20); m_plabel-> setgeometry (,); m_plineedit-> setgeometry ); m_plineedit-> installeventfilter (this);} qsingleshortcut ::~ Qsingleshortcut () {} bool qsingleshortcut: eventfilter (qobject * OBJ, qevent * eV) {qlineedit * pedit = static_cast <qlineedit *> (OBJ ); if (ev-> type () = qevent: keypress) {qkeyevent * E = static_cast <qkeyevent *> (EV); m_newkey.clear (); qdebug ()