QT Development (vi)--mouse, keyboard events

Source: Internet
Author: User

We can monitor the various events of the mouse and the various events of the keyboard in Qt, in Qt, he has achieved this series of functions for us, this we can refer to Qevent and other event classes, I will do some simple practical action to lead you to understand these interesting functions; mouse Events

There are too many mouse events, I am speaking

The left mouse and right key of the distinction, as well as the click of the axis value, and so on, in fact, the action is clicked, so we have a function mousepressevent

So we can rewrite this function in the header file.

Protected:
    void Mousepressevent (Qmouseevent * event);

Then it's in the source file to implement the specific logic

Mouse click
void mainwindow::mousepressevent (Qmouseevent * e)
{
    //Get clicked Subscript
    qdebug () << e->x () << ":" << e->y ();
    if (E->button () = = Qt::leftbutton)
    {
        qdebug () << "left Key";
    }
    else if (e->button () = = Qt::rightbutton)
    {
        qdebug () << "right Key";
    }
}

So that we can get the axis coordinates you clicked and whether you left or right, then we have those interesting functions. Mousemoveevent mousereleaseevent

The two one is moving one is lifting

Mouse mobile
void mainwindow::mousemoveevent (Qmouseevent * e)
{
    qdebug () << "Move" << e->x () < < ":" << e->y ();
}
Mouse loosens
void mainwindow::mousereleaseevent (Qmouseevent * e)
{
    qdebug () << "release" << e->x () << ":" << e->y ();
}

Everyone is interested to try

All of the above are clicked events, let's try a double click event mousedoubleclickevent

void Mousedoubleclickevent (Qmouseevent *event);

Double-click the
void Mainwindow::mousedoubleclickevent (qmouseevent *event)
{
    qdebug () << "double-click";
}

OK, let's learn a wheel event wheelevent

Mouse wheel
void mainwindow::wheelevent (qwheelevent *event)
{
    if (Event->delta () >0)
    {
        Qdebug () << "roll Up";
    }
    else
    {
        qdebug () << roll down;
    }
}

You can see, it's pretty simple, and that's basically enough for two. Keyboard Events

The keyboard event, we temporarily first learn a good, that is to press the void Keypressevent (Qkeyevent *event);

As for him, I'll show you a piece of code.

Keyboard event
void Mainwindow::keypressevent (Qkeyevent * Event)
{
    switch (Event->key ())
    {case
    Qt:: Key_escape:
        qdebug () << "ESC";
        break;
    Case QT::KEY_F1:
        qdebug () << "F1";
        break;
    Case QT::KEY_F2:
        qdebug () << "F2";
        break;
    Case Qt::key_5:
        qdebug () << "5";
        break;
    Case Qt::key_back:
        qdebug () << "Back";
        break;
    Case Qt::key_enter:
        qdebug () << "Enter";
        break;
    Case QT::KEY_A:
        qdebug () << "A";
        break;
    Case Qt::key_b:
        qdebug () << "B";
        break;
    Case Qt::key_c:
        qdebug () << "C";
        break;
    Default:
        qdebug () << event->key ();
        break;
    }
}

The corresponding key values are stored in the Qt class, but also very good judgment, of course, we can also judge some special key combinations, this I hope that everyone to understand

In the end, you still need an assistant to QT.

Well, you said you didn't know where. Qt Assistant, you search on this machine. interested can add group: 690351511

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.