2016-06-29 23 views
0

を使用してQgraphicsviewで矩形を動的に(ランタイム)描画する方法。Qt Creatorバージョン2.4.1を使用して、Qt4

私はちょうどQTプラットフォームの初心者で、矩形を動的に描画しようとしています。プログラムは3つのマウスイベントすなわち

  • mousePressEvent
  • mouseMoveEvent
  • mouseReleaseEvent

の問題を使用していますmouseMoveEventで呼び出されることはありません。ここ

は、コードスニペットは

main.cppに

#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

#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; 
} 

あるDialog.h

#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

プロパティ​​をtrueに設定してみてください。 You can read it here

また、コードにいくつかの問題があります。 例えば、mySquareオブジェクトを作成する場合は、square = new mySquare;を使用します。しかし、私はあなたがdelete squareと呼ぶコードを見ることができません。

+0

こんにちは、説明が必要な場合は、質問にコメントする必要があります。コメントする評判がないので、解答を投稿して明確化または詳細を求めないようにしてください。 – SilentMonk

+0

setMouseTrackingイベントがdialog.cppでtrueに設定されています。私はそれに応じてコードを編集しました。しかし問題はここで終わらないようです。このプログラムの私のコンセプトは、矩形をdinamically描画することでしたが、3つのマウスイベントはすべて動作していますが、uiでは長方形は描画されません。 –

関連する問題