Qt diary (3)-360 new feature interface implementation (3)

Source: Internet
Author: User

1. Draw the close button
Drag the image function and draw the close button first.
Now many software, like QQ, assistant ...... and so on. There are a lot of pretty buttons on the interface. When the mouse moves up, the button turns to highlighted. When the mouse clicks down, the button turns to dark concave. When the mouse moves away, the button is restored.
This effect is switched from multiple images.

New generates a qtoolbutton and sets the close button icon. Button images are generally provided in four states, and four images are combined into one PNG. Therefore, you only need to retrieve 1/4 of the image each time, and then move the close button to the upper right corner.

To implement these three effects in QT, you only need to install the Event Filter in the form.
Involved functions: eventfilter, installeventfilter.
Enum of several buttons:

Enum ebuttonmousestate {ebuttonmousedefault = 0, ebuttonmouseenter, ebuttonmousepress, success}; void preview360: createeventfilter () {// install Event Filter m_pbuttonclose-> installeventfilter (this );} // set the button icon void preview360: setbuttonicon (qtoolbutton * BTN, ebuttonmousestate) {qpixmap pixmap (":/images/btn_close.png"); int nwidth = pixmap. width ()/4; int nheight = pixmap. height (); BTN-> seticon (qicon (Pixmap. copy (qrect (State * nwidth, 0, nwidth, nheight); BTN-> seticonsize (qsize (nwidth, nheight);} bool preview360 :: eventfilter (qobject * target, qevent * event) {ebuttonmousestate state = ebuttonmousenone; // process several events. This download is clear about the QT Event Filter, practice integration theory =... if (target = m_pbuttonclose) {If (Event-> type () = qevent: Enter) {state = ebuttonmouseenter;} else if (Event-> type () = qevent: Leave) {state = EBU Ttonmousedefault;} else if (qmouseevent *) event)-> button () = QT: leftbutton) {If (Event-> type () = qevent :: mousebuttonpress) {state = ebuttonmousepress; // if you click the close button, do not drag the image m_mousepressflag = false;} else if (Event-> type () = qevent: mousebuttonrelease) {state = ebuttonmousedefault ;}} if (State! = Ebuttonmousenone) {setbuttonicon (qtoolbutton *) target, state) ;}// after processing the custom interception event, call the default qwidget Event Filter Function return qwidget :: eventfilter (target, event );}


2. Move the form

The movement of forms and images involves mouse events. You only need to move the event mouse to achieve the expected results. The main events are:

void mousePressEvent(QMouseEvent *);void mouseReleaseEvent(QMouseEvent *);void mouseMoveEvent(QMouseEvent *);


3. Move an image
In terms of operation, there are three pictures involved in moving:
(1). Drag the image with the mouse
(2) After dragging the image, release the mouse and restore the image to its original position.
(3) Right-click the image and move it right
(4). the keyboard arrow key controls the movement of images.
(5) Click the four buttons on the page to move them to the corresponding page.

4. Move the mouse
These mainly involve some logic Code and are directly posted:

Void preview360: mousepressevent (qmouseevent * E) {If (e-> button () = QT: leftbutton) {m_mousesrcpos = e-> pos (); // In the area with Y coordinates less than 40, move the mouse to move the form if (m_mousesrcpos.y () <= 40) {m_mousemovewindowflag = true;} else {// the area with a value greater than 40, drag the mouse to move the image m_currentfgxpos = m_plabelfgtotal-> X (); m_mousepressflag = true ;}// right-click the image and move it to the right. If (e-> button () = QT:: rightbutton) {If (getlabelmove () {If (m_currentfgindex> 0) {m_currentfgindex --; movecurrentpage (false); // right shift }}} void preview360 :: mousereleaseevent (qmouseevent * E) {int xpos = 0; If (m_mousepressflag) {If (getlabelmove () {m_mousedstpos = e-> pos (); xpos = m_mousedstpos.x () -m_mousesrcpos.x (); If (xpos> 0) // shift right {If (xpos> = window_onebutton_width) {If (m_currentfgindex> 0) {m_currentfgindex --; movecurrentpage (false ); // shift right} else {movecurrentpage (true); // shift left} else {movecurrentpage (true ); // shift left} else // shift left {If (xpos <=-window_onebutton_width) {If (m_currentfgindex <WINDOW_PAGE_COUNT-1) {m_currentfgindex ++; movecurrentpage (true ); // shift left} else {movecurrentpage (false); // shift right} m_mousepressflag = false;} else if (m_mousemovewindowflag) {m_mousemovewindowflag = false ;}} void preview360: mousemoveevent (qmouseevent * E) {int x = 0; If (m_mousepressflag) {If (getlabelmove ()) {m_mousedstpos = e-> pos (); X = m_mousedstpos.x ()-m_mousesrcpos.x (); setlabelmove (false); m_plabelfgtotal-> move (m_currentfgxpos + X, window_start_y ); setlabelmove (true) ;}} else if (m_mousemovewindowflag) {m_mousedstpos = e-> pos (); this-> move (this-> pos () + m_mousedstpos-m_mousesrcpos );}}


5. Click the four buttons on the page to move.

Void preview360: slotchangecurrentpage (clabel * label) {int Index = 0; For (INT I = 0; I <window_page_count; I ++) {If (Label = m_plabelbtnarray [I]) {// find the sequence number index = I; break ;}} of the current button, for X coordinate // Index = 0, move the label to-680*0 // Index = 1, and move the label to-680*1 // Index = 2, move the label to-680*2 // Index = 3, and move the label to-680*3 // click the button on the left to shift right if (index <m_currentfgindex) {While (index! = M_currentfgindex) {m_currentfgindex --; movecurrentpage (false) ;}} else if (index> m_currentfgindex) // click the button on the right to move it to the left {While (index! = M_currentfgindex) {m_currentfgindex ++; movecurrentpage (true) ;}} else {//...}}


6. Control of keyboard direction keys
To capture the corresponding key events, you only need to re-implement the Keyboard Events:
Void keypressevent (qkeyevent *);
Top left: Move the image to the right
Bottom right key: Move the image to the left

Void preview360: keypressevent (qkeyevent * E) {If (getlabelmove () {Switch (e-> key () {Case QT: key_left: Case QT: key_up: if (m_currentfgindex> 0) {m_currentfgindex --; movecurrentpage (false); // shift right} break; Case QT: key_right: Case QT: key_down: If (m_currentfgindex <WINDOW_PAGE_COUNT-1) {m_currentfgindex ++; movecurrentpage (true); // shift left} break; default: Break ;}}}


7. key functions of image Movement
The specific function of moving an image is to move the image slowly, that is, delay in the loop, and respond to the UI event to prevent interface freezing (not responding ).
The setlabelmove and getlabelmove functions appear in the program. These two functions are mainly used to prevent multiple events from moving the interface at the same time, resulting in errors. Therefore, it is equivalent to locking the movement of the image to ensure the uniqueness of the movement operation.

Void preview360: movecurrentpage (bool direction) {int currentxpos = 0; // The X coordinate of the current label int destxpos = 0; // target x coordinate // change the button changecurrentbutton () corresponding to the current page (); // image Split points // 0-680,680-1360,136 0-2040,204 0-2720 0-// true: Move left; false: Move right IF (direction) {// possible options for left shifting. For X coordinate // Index = 0, move the label to-680*0 // Index = 1, move the label to-680*1 // Index = 2, move the label to-680*2 // Index = 3, and move the label to-680*3 setlabelmove (false ); currentxpos = m_plabelfgtotal-> X (); destxpos =-window_width * m_currentfgindex; while (currentxpos> destxpos) {m_plabelfgtotal-> move (currentXpos-WINDOW_PAGE_MOVE, window_start_y ); currentxpos = m_plabelfgtotal-> X (); qapp-> processevents (qeventloop: allevents);} m_plabelfgtotal-> move (destxpos, window_start_y); setlabelmove (true );} else {// possible options for right shifting. For the X coordinate, it is consistent with the left shifting. // Index = 0. Move the label to-680*0 // Index = 1, move the label to-680*1 // Index = 2, move the label to-680*2 // Index = 3, and move the label to-680*3 setlabelmove (false ); currentxpos = rows-> X (); destxpos =-window_width * m_currentfgindex; while (currentxpos <destxpos) {m_plabelfgtotal-> move (currentxpos + window_page_move, window_start_y ); currentxpos = m_plabelfgtotal-> X (); qapp-> processevents (qeventloop: allevents);} m_plabelfgtotal-> move (destxpos, window_start_y); setlabelmove (true );}}

Prevent interface freezing:
Qapp-> processevents (qeventloop: allevents );
This function is used to process all events and ensure the normal operation of the interface.

8. Pin the control to the top
In the end, add the required control raise to the top of the stack:

//raise these widgetm_pLabelBg1->raise();m_pButtonClose->raise();for (int i = 0; i < WINDOW_BUTTON_COUNT; i++){    m_pLabelBtnArray[i]->raise();}

Multiple controls involve overlap. Only by re-adjusting the stack order can we achieve the desired effect.

Source code: 360 new feature page

Author: gzshun.
E-mail: gzshuns # 163.com (@)
Reprinted please indicate the source: http://blog.csdn.net/gzshun/article/details/7626756

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.