2013-08-30 4 views
0

나는 마우스로 영역을 선택할 수 있도록하고 싶다. 거의 모든 곳에서 할 수있다. 더 명확하게하기 위해 Windows에서 바탕 화면을 상상해보고 왼쪽 버튼을 클릭하고 구멍이있는 버튼으로 마우스를 움직인다. 다음과 같은 일이 발생합니다 : 마우스가 통과 한 영역이 직사각형으로 강조 표시되는 방식을 볼 수 있습니다. 그것이 바로 내가하고 싶은 일입니다.마우스 이벤트 QT

p.s. 수학적으로 계산하는 방법을 알고 있으며 마우스 버튼을 눌렀을 때 마우스 위치를 추적하여 사각형을 그리는 방법을 알고 있습니다.

1 : 마우스 위치를 추적하는 방법은 무엇입니까? Q2 : 내가 원하는 것을 수행 할 수있는 다른 방법은 없을까요?

답변

4

가장 간단한 방법은 그래픽보기 프레임 워크를 사용하는 것입니다. 항목 선택, 고무 밴드 사각형 표시, 고무 밴드와 아이템의 교차 감지 등을 제공합니다. 아래에는 자체 포함 된 예가 나와 있습니다. Ctrl/Cmd 클릭하여 토글 선택 또는 고무 밴딩을 사용하여 여러 항목을 선택하고 드래그 할 수 있습니다.

OpenGL은 배경을 렌더링하는 데 사용되며 임의의 OpenGL 콘텐츠를 그 배경에 배치 할 수 있습니다.

enter image description here

main.cpp

#include <QApplication> 
#include <QGraphicsView> 
#include <QGraphicsScene> 
#include <QGraphicsRectItem> 
#include <QGLWidget> 

static qreal rnd(qreal max) { return (qrand()/static_cast<qreal>(RAND_MAX)) * max; } 

class View : public QGraphicsView { 
public: 
    View(QGraphicsScene *scene, QWidget *parent = 0) : QGraphicsView(scene, parent) { 
     setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); 
     setViewportUpdateMode(QGraphicsView::FullViewportUpdate); 
    } 
    void drawBackground(QPainter *, const QRectF &) { 
     QColor bg(Qt::blue); 
     glClearColor(bg.redF(), bg.greenF(), bg.blueF(), 1.0f); 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    } 
}; 

void setupScene(QGraphicsScene &s) 
{ 
    for (int i = 0; i < 10; i++) { 
     qreal x = rnd(1), y = rnd(1); 
     QAbstractGraphicsShapeItem * item = new QGraphicsRectItem(x, y, rnd(1-x), rnd(1-y)); 
     item->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable); 
     item->setPen(QPen(Qt::red, 0)); 
     item->setBrush(Qt::lightGray); 
     s.addItem(item); 
    } 
} 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    QGraphicsScene s; 
    setupScene(s); 
    View v(&s); 
    v.fitInView(0, 0, 1, 1); 
    v.show(); 
    v.setDragMode(QGraphicsView::RubberBandDrag); 
    v.setRenderHint(QPainter::Antialiasing); 
    return a.exec(); 
}