Detailed layout of pallets

Source: Internet
Author: User

Reprinted address:Http://blog.163.com/ak_19860216/blog/static/128071932009231115142757/

Tip. let everyone laugh. therefore, the things written about QT are very basic things. hope the experts can see it, don't BS, quietly walk away, or, flash fast. ^ o ^

* Before I start, I will explain that the concept of pallets is different from that of the taskbar, And I want to accurately search for the desired information. be sure to understand the concept accurately. in addition, you can refer to qt_assistant when you are free. in this article, you can go to qtdemo.exe --> desktop --> system tray to find the official example. its content is perfect. the usage of the qsystemtray class is very transparent. ^ o ^

Phase 1:

With the simple code of Hello world, it is easier to understand the specific implementation of the tray and some details, such as why the message is not displayed on the tray and how to add an icon to the tip message.

# Include <qtgui>

Class myclass: Public qwidget

{

Public:

Myclass ();

PRIVATE:

Qpushbutton * B1;

Qsystemtrayicon * trayicon;

};

Myclass: myclass ()

{

Qtextcodec: setcodecfortr (qtextcodec: codecforname ("gb2312 "));

Setminimumsize (200,100 );

Setmaximumsize (200,100 );

B1 = new qpushbutton ("Hello World", this );

B1-> setgeometry );

Qicon icon = qicon ("./images/bad. SVG ");

Setmediawicon (icon );

Trayicon = new qsystemtrayicon (this );

Trayicon-> seticon (icon );

Trayicon-> settooltip ("A trayicon example ");

Qstring titlec = tr ("hello ");

Qstring textc = qstring: fromlocal8bit ("Hello QT? ");

Trayicon-> show ();

Trayicon-> showmessage (titlec, textc, qsystemtrayicon: information, 5000 );

Setwindowtitle (TR ("Ray "));

}

Int main (INT argc, char ** argv)

{

Qapplication testc (argc, argv );

Myclass NEWC;

NEWC. Show ();

Return testc.exe C ();

}

Phase II:

For further research, add the response of clicking the tray so that the prompt information is displayed on the tray, right-click the tray menu.

Experience 1: The compiling signal and slot must be in the format of. H. cpp. Otherwise, the corresponding signal and slot will not be generated;

Experience 2: parameters can be passed between the signal and the slot, but the function is not allowed to directly provide parameters to the slot.
Hello. h

# Include <qtgui>
Class myclass: Public qwidget
{
Q_object
Public:
Myclass ();
PRIVATE:
Qpushbutton * B1;
Qsystemtrayicon * trayicon;
Char * MSG;
Void showmessage (char * MSG );
Private slots:
Void showm ();
Void iconactivated (qsystemtrayicon: activationreason reason );
};

Hello. cpp

# Include <qtgui>
# Include "Hello. H"
Myclass: myclass ()
{
Qtextcodec: setcodecfortr (qtextcodec: codecforname ("gb2312 "));
Setminimumsize (200,100 );
Setmaximumsize (200,100 );
B1 = new qpushbutton ("Hello World", this );
B1-> setgeometry );
Qicon icon = qicon ("./images/bad. SVG ");
Setmediawicon (icon );
Trayicon = new qsystemtrayicon (this );
Trayicon-> seticon (icon );
Trayicon-> settooltip ("A trayicon example ");
Trayicon-> show ();
Setwindowtitle (TR ("Ray "));

Connect (trayicon, signal (activated (qsystemtrayicon: activationreason )),
This, slot (iconactivated (qsystemtrayicon: activationreason )));
Connect (B1, signal (clicked (), this, slot (showm ()));
}

Void myclass: showm ()
{
Qstring titlec = tr ("slot must be passed using the same signal parameter ");
Qstring textc = qstring: fromlocal8bit ("Test content click, double-click, middle-key, button ");
Trayicon-> showmessage (titlec, textc, qsystemtrayicon: information, 5000 );
}

Void myclass: showmessage (char * MSG)
{
Qstring titlec = tr (MSG );
Qstring textc = qstring: fromlocal8bit ("Test content click, double-click, middle-key, button ");
Trayicon-> showmessage (titlec, textc, qsystemtrayicon: information, 5000 );
}

Void myclass: iconactivated (qsystemtrayicon: activationreason reason)
{
Switch (reason ){
Case qsystemtrayicon: trigger:
Showmessage ("click ");
Break;
Case qsystemtrayicon: DoubleClick:
Showmessage ("double-click ");
Break;
Case qsystemtrayicon: middleclick:
Showmessage ("are you using three-wheeled or scroll mouse ");
Break;
Default:
;
}
}

Int main (INT argc, char ** argv)
{
Qapplication testc (argc, argv );
Myclass NEWC;
NEWC. Show ();
Return testc.exe C ();
}

Phase III:

Finally, we combined all the elements to implement such a pallet program. Compared with the previous example, we added the right-click menu and made it manage the window size.
Hello. h

# Include <qtgui>
Class myclass: Public qwidget
{
Q_object
Public:
Myclass ();
PRIVATE:
Qpushbutton * B1;
Qsystemtrayicon * trayicon;
Char * MSG;
Void showmessage (char * MSG );
Void createactions ();
Void createtrayicon ();
Qaction * minimizeaction;
Qaction * maximizeaction;
Qaction * restoreaction;
Qaction * quitaction;
Qmenu * trayiconmenu;
Private slots:
Void showm ();
Void iconactivated (qsystemtrayicon: activationreason reason );
};

Hello. cpp

# Include <qtgui>
# Include "Hello. H"
Myclass: myclass ()
{
Qtextcodec: setcodecfortr (qtextcodec: codecforname ("gb2312 "));
Setminimumsize (200,100 );
B1 = new qpushbutton ("Hello World", this );
B1-> setgeometry );
Qicon icon = qicon ("./images/bad. SVG ");
Setmediawicon (icon );
Trayicon = new qsystemtrayicon (this );
Trayicon-> seticon (icon );
Trayicon-> settooltip ("A trayicon example ");
Createactions ();
Createtrayicon ();
Trayicon-> show ();
Setwindowtitle (TR ("Ray "));
Connect (trayicon, signal (activated (qsystemtrayicon: activationreason), this, slot (iconactivated (qsystemtrayicon: activationreason )));
Connect (B1, signal (clicked (), this, slot (showm ()));
}

Void myclass: showm ()
{
Qstring titlec = tr ("slot must be passed using the same signal parameter ");
Qstring textc = qstring: fromlocal8bit ("Test content click, double-click, middle-key, button ");
Trayicon-> showmessage (titlec, textc, qsystemtrayicon: information, 5000 );
}

Void myclass: showmessage (char * MSG)
{
Qstring titlec = tr (MSG );
Qstring textc = qstring: fromlocal8bit ("Test content click, double-click, middle-key, button ");
Trayicon-> showmessage (titlec, textc, qsystemtrayicon: information, 5000 );
}

Void myclass: iconactivated (qsystemtrayicon: activationreason reason)
{
Switch (reason ){
Case qsystemtrayicon: trigger:
Showmessage ("click ");
Break;
Case qsystemtrayicon: DoubleClick:
Showmessage ("double-click ");
Break;
Case qsystemtrayicon: middleclick:
Showmessage ("are you using three-wheeled or scroll mouse ");
Break;
Default:
;
}
}

Void myclass: createactions ()
{
Minimizeaction = new qaction (TR ("minimal (& I)"), this );
Connect (minimizeaction, signal (triggered (), this, slot (hide ()));

Maximizeaction = new qaction (TR ("maximize (& X)"), this );
Connect (maximizeaction, signal (triggered (), this, slot (showmaximized ()));

Restoreaction = new qaction (TR ("Restore (& R)"), this );
Connect (restoreaction, signal (triggered (), this, slot (shownormal ()));

Quitaction = new qaction (TR ("Quit (& Q)"), this );
Connect (quitaction, signal (triggered (), qapp, slot (quit ()));
}

Void myclass: createtrayicon ()
{
Trayiconmenu = new qmenu (this );
Trayiconmenu-> addaction (minimizeaction );
Trayiconmenu-> addaction (maximizeaction );
Trayiconmenu-> addaction (restoreaction );
Trayiconmenu-> addseparator ();
Trayiconmenu-> addaction (quitaction );
Trayicon-> setcontextmenu (trayiconmenu );
}

Int main (INT argc, char ** argv)
{
Qapplication testc (argc, argv );
Myclass NEWC;
NEWC. Show ();
Return testc.exe C ();
}

(The above code can be directly copied to run... vs2005 + qt4.4.3 @ WINXP Professional SP3)

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.