QT Animation effect Display

Source: Internet
Author: User

The program uses the Application Single window, the main window inherits from the Qmainwindow; the main window has 5 Qtoolbutton parts (four at the bottom of the window and one in the center of the window). Click the Qtoolbutton part at the bottom of the window to animate the Qtoolbutton in the center of the window, so try it yourself.

1. Generating parts and locating parts
Create a part object in the constructor of the main window, and then position the part in the window resizing event, as shown in the following code:

Generate Toolbutton
m_pbtn1 = new Qtoolbutton (this);
Window size Change event
void Mainwindow::resizeevent (qresizeevent *event)
{
    m_pbtn1->move (0,height ()-h_widget) ;
    M_pbtn2->move (W_widget+space_widget,height ()-h_widget);
    M_pbtn3->move (Width ()-w_widget*2-space_widget,height ()-h_widget);
    M_pbtn4->move (Width ()-w_widget,height ()-h_widget);
    M_pbtnanima Center
    m_pbtnanima->move (width ()/2-w_widget/2,height ()/2-H_WIDGET/2);
    Qmainwindow::resizeevent (event);
}

2. Draw the window background
The window background is linear gradient, that is, the window on the left is white, has been gradient to the bottom right of the black, so use qt linear gradient brush; QT has three gradient effects, namely: linear gradient, circular gradient and tapered gradient.

Redraw event
void MainWindow::p aintevent (qpaintevent *event)
{
    qpainter painter (this);
    Draw a gradient background--linear gradient
    qlineargradient objlinear (Rect (). TopLeft (), Rect (). BottomRight ());
    Objlinear.setcolorat (0,qt::white);
    Objlinear.setcolorat (1,qt::black);
    Painter.fillrect (Rect (), objlinear);
}

3. Qtoolbutton Display Image
The Setmask function is used primarily to generate a mask bitmap, which filters out parts that are not in the image.

Pbtn->setstylesheet ("qtoolbutton{border:0px;}");
Pbtn->resize (w_widget,h_widget);
Pbtn->seticonsize (Qsize (W_widget,h_widget));
Qpixmap Objpixmap (str);
Pbtn->seticon (Qicon (Objpixmap));
Pbtn->setmask (Objpixmap.mask ());

4. Set the Qtoolbutton animation in the center of the window
Of course, the main use of Qpropertyanimation object, look at the following lines of code, very simple, the main setting duration, and then set the Qtoolbutton at the beginning and end of the geometry information, and finally set the animation effect, QT built a lot of animation effect for us to choose.

Slot Function--animate set
void mainwindow::setanimation (int ncurvetype)
{
    //If the status is running, stop the animation if
    (m_pproanima- >state () ==qpropertyanimation::running)
    {
        m_pproanima->stop ();
    }
    Set up a new animation
    m_pproanima->setduration (+);
    M_pproanima->setstartvalue (qrect (0, 0, W_widget, h_widget)); 
    M_pproanima->setendvalue (Qrect (Width ()-w_widget,height ()-h_widget*2,w_widget,h_widget));
    M_pproanima->seteasingcurve (Qeasingcurve::type (Ncurvetype));
    M_pproanima->start ();
}

The setanimation (int ncurvetype) parameter represents which animation effect, because clicking on the four Qtoolbutton at the bottom of the window implements a different animation effect, but the Qtoolbutton clicked Signal is parameterless. Then how to identify which one came, it is necessary to use the Qsignalmapper class, using this class can be transmitted to the signal and parameters, additional parameters can be integer, string or widget parameters and Qobject.

M_psignalmapper = new Qsignalmapper (this);
Connect (M_PBTN1, SIGNAL (clicked ()), M_psignalmapper, SLOT (Map ()));
M_psignalmapper->setmapping (M_PBTN1, qeasingcurve::outinquad);
Connect (M_psignalmapper, SIGNAL (mapped (int)), this, SLOT (setanimation (int)));

SOURCE Link: http://download.csdn.net/detail/caoshangpa/9726468

Original link: http://www.cnblogs.com/appsucc/archive/2012/03/06/2382316.html

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.