返回介绍

Drag and Drop (Simple)

发布于 2019-10-04 15:04:21 字数 6220 浏览 939 评论 0 收藏 0

This provides a very simple example of Qt's drag and drop functionality.

For a more complete example see the Drag and Drop example.


Header file:

/****************************************************************************
** $Id$
**
** Copyright (C) 1992-2001 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <qapplication.h>
#include <qcursor.h>
#include <qsplitter.h>
#include <qlistbox.h>
#include <qiconview.h>
#include <qpixmap.h>

class QDragEnterEvent;
class QDragDropEvent;


class DDListBox : public QListBox
{
    Q_OBJECT
public:
    DDListBox( QWidget * parent = 0, const char * name = 0, WFlags f = 0 );
    // Low-level drag and drop
    void dragEnterEvent( QDragEnterEvent *evt );
    void dropEvent( QDropEvent *evt );
    void mousePressEvent( QMouseEvent *evt );
    void mouseMoveEvent( QMouseEvent * );
private:
    int dragging;
};


class DDIconViewItem : public QIconViewItem
{
public:
    DDIconViewItem( QIconView *parent, const QString& text, const QPixmap& icon ) :
        QIconViewItem( parent, text, icon ) {}
    DDIconViewItem( QIconView *parent, const QString &text ) :
        QIconViewItem( parent, text ) {}
    // High-level drag and drop
    bool acceptDrop( const QMimeSource *mime ) const;
    void dropped( QDropEvent *evt, const QValueList<QIconDragItem>& );
};


class DDIconView : public QIconView
{
    Q_OBJECT
public:
    DDIconView( QWidget * parent = 0, const char * name = 0, WFlags f = 0 ) :
        QIconView( parent, name, f ) {}
    // High-level drag and drop
    QDragObject *dragObject();
public slots:
    void slotNewItem( QDropEvent *evt, const QValueList<QIconDragItem>& list );
};


Implementation:

/****************************************************************************
** $Id$
**
** Copyright (C) 1992-2001 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "main.h"

const char* red_icon[]={
"16 16 2 1",
"r c red",
". c None",
"................",
"................",
"..rrrrrrrrrrrr..",
"..rrrrrrrrrrrr..",
"..rrrrrrrrrrrr..",
"..rrr......rrr..",
"..rrr......rrr..",
"..rrr......rrr..",
"..rrr......rrr..",
"..rrr......rrr..",
"..rrr......rrr..",
"..rrrrrrrrrrrr..",
"..rrrrrrrrrrrr..",
"..rrrrrrrrrrrr..",
"................",
"................"};

const char* blue_icon[]={
"16 16 2 1",
"b c blue",
". c None",
"................",
"................",
"..bbbbbbbbbbbb..",
"..bbbbbbbbbbbb..",
"..bbbbbbbbbbbb..",
"..bbb......bbb..",
"..bbb......bbb..",
"..bbb......bbb..",
"..bbb......bbb..",
"..bbb......bbb..",
"..bbb......bbb..",
"..bbbbbbbbbbbb..",
"..bbbbbbbbbbbb..",
"..bbbbbbbbbbbb..",
"................",
"................"};

const char* green_icon[]={
"16 16 2 1",
"g c green",
". c None",
"................",
"................",
"..gggggggggggg..",
"..gggggggggggg..",
"..gggggggggggg..",
"..ggg......ggg..",
"..ggg......ggg..",
"..ggg......ggg..",
"..ggg......ggg..",
"..ggg......ggg..",
"..ggg......ggg..",
"..gggggggggggg..",
"..gggggggggggg..",
"..gggggggggggg..",
"................",
"................"};


// ListBox -- low level drag and drop

DDListBox::DDListBox( QWidget * parent, const char * name, WFlags f ) :
    QListBox( parent, name, f )
{
    setAcceptDrops( TRUE );
    dragging = FALSE;
}


void DDListBox::dragEnterEvent( QDragEnterEvent *evt )
{
    if ( QTextDrag::canDecode( evt ) )
        evt->accept();
}


void DDListBox::dropEvent( QDropEvent *evt )
{
    QString text;

    if ( QTextDrag::decode( evt, text ) )
        insertItem( text );
}


void DDListBox::mousePressEvent( QMouseEvent *evt )
{
    QListBox::mousePressEvent( evt );
    dragging = TRUE;
}


void DDListBox::mouseMoveEvent( QMouseEvent * )
{
    if ( dragging ) {
        QDragObject *d = new QTextDrag( currentText(), this );
        d->dragCopy(); // do NOT delete d.
        dragging = FALSE;
    }
}


// IconViewIcon -- high level drag and drop


bool DDIconViewItem::acceptDrop( const QMimeSource *mime ) const
{
    if ( mime->provides( "text/plain" ) )
        return TRUE;
    return FALSE;
}


void DDIconViewItem::dropped( QDropEvent *evt, const QValueList<QIconDragItem>& )
{
    QString label;

    if ( QTextDrag::decode( evt, label ) )
        setText( label );
}


// IconView -- high level drag and drop

QDragObject *DDIconView::dragObject()
{
  return new QTextDrag( currentItem()->text(), this );
}

void DDIconView::slotNewItem( QDropEvent *evt, const QValueList<QIconDragItem>& )
{
    QString label;

    if ( QTextDrag::decode( evt, label ) ) {
        DDIconViewItem *item = new DDIconViewItem( this, label );
        item->setRenameEnabled( TRUE );
    }
}



int main( int argc, char *argv[] )
{
    QApplication app( argc, argv );

    // Create and show the widgets
    QSplitter *split = new QSplitter();
    DDIconView *iv   = new DDIconView( split );
    (void)             new DDListBox( split );
    app.setMainWidget( split );
    split->resize( 600, 400 );
    split->show();

    // Set up the connection so that we can drop items into the icon view
    QObject::connect(
        iv, SIGNAL(dropped(QDropEvent*, const QValueList<QIconDragItem>&)),
        iv, SLOT(slotNewItem(QDropEvent*, const QValueList<QIconDragItem>&)));

    // Populate the QIconView with icons
    DDIconViewItem *item;
    item = new DDIconViewItem( iv, "Red",   QPixmap( red_icon ) );
    item->setRenameEnabled( TRUE );
    item = new DDIconViewItem( iv, "Green", QPixmap( green_icon ) );
    item->setRenameEnabled( TRUE );
    item = new DDIconViewItem( iv, "Blue",  QPixmap( blue_icon ) );
    item->setRenameEnabled( TRUE );

    return app.exec();
}


See also Examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文