In Qt, inherits is used to determine the origin of the drag and drop event. inheritsdrag

Source: Internet
Author: User

In Qt, inherits is used to determine the origin of the drag and drop event. inheritsdrag

Sometimes we needDragMoveEvent,DropEventSuch a function filters out invalid drag or distinguishes different types of drag for different processing. For example, the Code is roughly as follows:

void Scene::dragMoveEvent(QGraphicsSceneDragDropEvent *event){    if(event->source()********)    {        event->accept();        event->setDropAction(Qt::CopyAction);    }    else    {        QGraphicsScene::dragMoveEvent(event);    }}


Here, event-> source returns a QWidget pointer, which does not have much type information. In this case, the common practice is to judge based on the pointer address.

    if(event->source() == static_cast<QWidget *>(xxx))    {        event->accept();        event->setDropAction(Qt::CopyAction);    }

Okay, let's assume that drag is made on xxx. It's okay. If there are many xxx files like this, you can also store them in a list.

    //QList<QWidget *> widgetLists;    if(widgetLists.contains(<span style="font-family: Arial, Helvetica, sans-serif;">event->source()</span><span style="font-family: Arial, Helvetica, sans-serif;">))</span>    {        event->accept();        event->setDropAction(Qt::CopyAction);    }


If you want to determine the type, you can install different types in different lists and then judge them accordingly. however, it is troublesome to save the pointer passed from other places. is there a better way?

There are actually some, you can use the inherits method:

    if(event->source()->inherits("QToolButton"))
    {
        event->accept();
        event->setDropAction(Qt::CopyAction);
    }
In this way, it is very easy to determine the type. For example, pushButton drag is used for processing and ToolButton is used for processing.


Help QT inherits usage

Bool QObject: inherits (const char * lname) const
If this object is an instance of the class that inherits the clname and the lname inherits the QObject, the system returns true. Otherwise, the system returns false.
A class can be considered to inherit itself.

Instance:
QTimer * t = new QTimer; // QTimer inherits QObject
T-> inherits ("QTimer"); // return TRUE
T-> inherits ("QObject"); // return TRUE
T-> inherits ("QButton"); // return FALSE

// QScrollBar inherits QWidget and QRangeControl
QScrollBar * s = new QScrollBar (0 );
S-> inherits ("QWidget"); // return TRUE
S-> inherits ("QRangeControl"); // return FALSE
(Note: QRangeControl is not a QObject .)

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.