|
代码是OSG例子的基础上改的只简单测试拖拽效果,结果鼠标进入到场景后捕捉不到QDrop事件。
#include <QtGui/QListWidget>
#include <QtGui/QMainWindow>
#include <QtGui/QDockWidget>
#include <QtGui/QDragMoveEvent>
#include <QtGui/QDragEnterEvent>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include <osgQt/GraphicsWindowQt>
#include <iostream>
class CustormList:public QListWidget
{
public:
CustormList(QWidget* parent)
{
this->setAcceptDrops(false);
this->setDragEnabled(true);
QStringList itemlist;
itemlist<<tr("ddd")<<tr("xxx")<<tr("gggg");
this->addItems(itemlist);
}
};
class ViewerWidget : public QWidget, public osgViewer::CompositeViewer
{
public:
ViewerWidget(osgViewer::ViewerBase::ThreadingModel threadingModel=osgViewer::CompositeViewer::SingleThreaded) : QWidget()
{
setMinimumSize(100,100);
setThreadingModel(threadingModel);
setAcceptDrops(true);
QWidget* widget1 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("cow.osgt") );
widget1->setParent(this);
widget1->setAcceptDrops(false);
QGridLayout* grid = new QGridLayout;
setLayout( grid );
connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
_timer.start( 10 );
}
QWidget* addViewWidget( osg::Camera* camera, osg::Node* scene )
{
osgViewer::View* view = new osgViewer::View;
view->setCamera( camera );
addView( view );
view->setSceneData( scene );
view->addEventHandler( new osgViewer::StatsHandler );
view->setCameraManipulator( new osgGA::TrackballManipulator );
osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>( camera->getGraphicsContext() );
return gw ? gw->getGLWidget() : NULL;
}
osg::Camera* createCamera( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
{
osg:isplaySettings* ds = osg::DisplaySettings::instance().get();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->windowName = name;
traits->windowDecoration = windowDecoration;
traits->x = x;
traits->y = y;
traits->width = w;
traits->height = h;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) );
camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
camera->setProjectionMatrixAsPerspective(
30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
return camera.release();
}
virtual void paintEvent( QPaintEvent* event )
{ frame(); }
protected:
void dragEnterEvent(QDragEnterEvent *event)
{
if (event->source() != this)
{
event->accept();
}
else
{
event->ignore();
}
std::cout<<"dragenter"<<std::endl;
}
void dragMoveEvent(QDragMoveEvent *event)
{
if (event->source() != this)
{
event->setDropAction(Qt::MoveAction);
event->accept();
}
else
{
event->ignore();
}
std::cout<<"dragmove"<<std::endl;
}
void dropEvent(QDropEvent *event)
{
std::cout<<"drop"<<std::endl;
}
protected:
QTimer _timer;
};
class CustormMainWindow:public QMainWindow
{
public:
CustormMainWindow()
{
CustormList* list = new CustormList(this);
QDockWidget *dockWidget = new QDockWidget(tr("Dock Widget"), this);
dockWidget->setAllowedAreas(Qt:eftDockWidgetArea |
Qt::RightDockWidgetArea);
dockWidget->setWidget(list);
addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
ViewerWidget* widget = new ViewerWidget();
setCentralWidget(widget);
}
};
int main( int argc, char** argv )
{
osg::ArgumentParser arguments(&argc, argv);
QApplication app(argc, argv);
CustormMainWindow* mainWindow = new CustormMainWindow();
mainWindow->show();
return app.exec();
}
|
-
|