2016-06-29 7 views
0

을 사용하여 Qgraphicsview에서 사각형을 동적으로 그리는 방법 (런타임).Qt Creator 버전 2.4.1을 사용하여 Qt4

저는 QT 플랫폼에서 시작하여 직사각형을 동적으로 그리려고합니다. 이 프로그램은 3 개 마우스 이벤트를 사용, 즉

  • mousePressEvent
  • mouseMoveEvent
  • mouseReleaseEvent

문제가 호출되지 않습니다 mouseMoveEvent입니다. 여기

,536,913,632 Dialog.h

#include "dialog.h" 
#include "ui_dialog.h" 
#include "mysquare.h" 

Dialog::Dialog(QWidget *parent) : 
    QDialog(parent), 
    ui(new Ui::Dialog) 
{ 
    ui->setupUi(this); 
    setMouseTracking(true); 
    scene = new QGraphicsScene(this); 
    ui->graphicsView->setScene(scene); 
    scene->setBackgroundBrush(Qt::black); 
    square = new mySquare; 
    scene->addItem(square); 
    square->mPix = QPixmap(200,200); 
    square->mPix.fill(Qt::white); 
    scene->addPixmap(square->mPix); 
} 

Dialog::~Dialog() 
{ 
    delete ui; 
} 

void Dialog::on_pushButton_clicked() 
{ 
    square->selectedTool = 1; 
} 

#include <QtGui/QApplication> 
#include "dialog.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    Dialog w; 
    w.show(); 

    return a.exec(); 
} 

Dialog.cpp 코드 조각

인 MAIN.CPP 10

#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QtCore> #include <QtGui> #include "mysquare.h" namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private slots: void on_pushButton_clicked(); private: Ui::Dialog *ui; QGraphicsScene *scene; mySquare *square; }; #endif // DIALOG_H 

mysquare.cpp

#include "mysquare.h" 

using namespace std; 


mySquare::mySquare() 
{ 
    pressed = false; 
    selectedTool = 1; 
    mPix = QPixmap(200,200); 
    mPix.fill(Qt::white); 
} 

void mySquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 
{ 
    painter->begin(this); 
    if(pressed) 
    { 
     painter->drawPixmap(0,0,mPix); 
     painter->drawRect(mRect); 
     drawStarted = true; 
    } 
    else if (drawStarted) 
    { 
     QPainter tempPainter(&mPix); 
     tempPainter.drawRect(mRect); 
     painter->drawPixmap(0,0,mPix); 
    } 

    painter->end(); 
} 

void mySquare::mousePressEvent(QGraphicsSceneMouseEvent *event) 
{ 
    pressed = true; 

    y = event->pos(); 
    cout<<"Pos X : "<<y.x() <<endl; 
    cout<<"Pos Y : "<<y.y() <<endl; 

    if(selectedTool == 1) 
    { 
     mRect.setTopLeft(event->pos()); 
     mRect.setBottomRight(event->pos()); 

     cout << "Value of x_start axis " << X_1 <<endl; 
     cout << "Value of y_start axis " << Y_1 <<endl; 
    } 
    else if (selectedTool == 2) 
    { 

    } 
    update(); 
} 

void mySquare::mouseMoveEvent(QGraphicsSceneMouseEvent *event) 
{ 
    cout<<"Inside mouseMoveEvent \n"; 
    if(event->type() == QEvent::MouseMove) 
    { 
     if(selectedTool == 1){ 
      mRect.setBottomRight(event->pos()); 
     } 
     else if (selectedTool == 2){ 
      //mLine.setP2(event->pos()); 
     } 
    } 
    update(); 
} 

void mySquare::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) 
{ 
    pressed = false; 
    update(); 
    cout<<"Inside mouseReleaseEvent \n"; 
} 

mysquare.h

#ifndef MYSQUARE_H 
#define MYSQUARE_H 

#include <QPainter> 
#include <QGraphicsItem> 
#include <QDebug> 
#include <QPainter> 
#include <iostream> 
#include <QPixmap> 
#include <QPaintEvent> 
#include <QGraphicsSceneMouseEvent> 

class mySquare: public QGraphicsItem 
{ 
public: 
    mySquare(); 
    int selectedTool; 
    QPixmap mPix; 
    QRectF boundingRect() const; 
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); 
    bool pressed; 
    bool drawStarted; 

protected: 
    void mousePressEvent(QGraphicsSceneMouseEvent *event); 
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event); 
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); 

private: 
    QRectF mRect; 
    QPointF y; 


private slots: 

}; 

#endif // MYSQUARE_H 

나를 인도 해주십시오, 감사합니다.

답변

1

mouseTracking 속성을 true로 설정하십시오. You can read it here

또한 코드에 몇 가지 문제가 있습니다. square = new mySquare;을 사용하여 mySquare 개체를 만드는 경우를 예로들 수 있습니다. 하지만 delete square으로 전화를 걸면 코드를 볼 수 없습니다.

+0

안녕하세요, 설명이 필요한 경우 안녕하세요. 당신이 논평 할 명성이 없으므로, 답변을 게시함으로써 설명이나 세부 사항을 요구하지 않아야합니다. – SilentMonk

+0

setMouseTracking 이벤트가 dialog.cpp에서 true로 설정됩니다. 그에 따라 코드를 편집했습니다. 그러나 문제는 여기서 끝나지 않는 것 같습니다. 이 프로그램의 개념은 직사각형을 dinamically 그리는 것이 었습니다. 3 개의 마우스 이벤트가 모두 작동하지만 Ui에서는 직사각형이 그려지지 않습니다. –