如何使用Qt4在Qgraphicsview中动态(运行时)绘制矩形
How to draw a Rectangle in Qgraphicsview dynamically (runtime), using Qt4
使用QT Creator版本2.4.1。
我只是QT平台的初学者,正在尝试动态绘制矩形。该程序使用3个鼠标事件,即
- mousePress事件
- mouseMoveEvent
- 鼠标释放事件
问题是从来没有调用过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();
}
对话框.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;
}
对话框.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
请引导我,谢谢
尝试将属性mouseTracking
设置为true。你可以在这里阅读
此外,您的代码中也存在一些问题。例如,使用square = new mySquare;
创建mySquare
对象。但我看不到您调用delete square
的代码。
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 我在c++代码中生成了一个运行时#3异常
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 建议在运行时将带有类实例的列表从c++导入qml
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 如何在GTK程序运行时禁用屏幕保护程序/电源管理/屏幕消隐
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 读取文件时运行时的未知行为
- 函数在Windows或Linux上运行时表现不同
- OPENCV C .在for循环中在不同的图像像素上绘制一个圆圈(应在每个循环运行时将图像打开)
- 如何使用Qt在运行时从一个对象绘制到另一个对象的线条(或箭头)
- 我的窗口未在调试器中运行时无法正确绘制
- 如何使用Qt4在Qgraphicsview中动态(运行时)绘制矩形