c++ - How to draw a Rectangle in Qgraphicsview dynamically (runtime), using Qt4 -
using qt creator version 2.4.1.
i'm starter on qt platform , trying draw rectangle dynamically. program uses 3 mouse events namely
- mousepressevent
- mousemoveevent
- mousereleaseevent
problems mousemoveevent never called.
here code snippet
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
please guide me, thanks.
try setting property mousetracking
true. you can read here
also have problems in code. example creating mysquare
object using square = new mysquare;
. can't see code call delete square
.
Comments
Post a Comment