Solved Why can't I draw a line with mouse moving?
-
I'm new to Qt and here is my code.
I want to draw a line with mouse moving, wish you can help me figure out why it doesn't work!//minidraw.h #ifndef MINIDRAW_H #define MINIDRAW_H #include <QtGui/QMainWindow> #include "ui_minidraw.h" #include "viewwidget.h" class MiniDraw : public QMainWindow { Q_OBJECT public: MiniDraw(QWidget *parent = 0, Qt::WFlags flags = 0); ~MiniDraw(); private: Ui::MiniDrawClass ui; ViewWidget *view_widget_; QMenu *pMenu; QToolBar *pToolbar1; QToolBar *pToolbar2; QToolBar *pToolbar3; QToolBar *pToolbar4; QToolBar *pToolbar5; QToolBar *pToolbar6; QAction *pAction1; QAction *pAction2; QAction *pAction3; QAction *pAction4; QAction *pAction5; QAction *pAction6; void Init(); }; #endif // MINIDRAW_H
//minidraw.cpp #include "minidraw.h" MiniDraw::MiniDraw(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { ui.setupUi(this); Init(); } MiniDraw::~MiniDraw() { } void MiniDraw::Init() { pMenu = menuBar()->addMenu(tr("&Figure Tool")); pAction1 = new QAction(tr("&About"),this); pAction2 = new QAction(tr("&Line"),this); pAction3 = new QAction(tr("&Rectangle"), this); pAction4 = new QAction(tr("&Ellipse"), this); pAction5 = new QAction(tr("&Polygon"), this); pAction6 = new QAction(tr("&Delete"), this); pToolbar1 = addToolBar(tr("&About")); pToolbar2 = addToolBar(tr("&Line")); pToolbar3 = addToolBar(tr("&Rectangle")); pToolbar4 = addToolBar(tr("&Ellipse")); pToolbar5 = addToolBar(tr("&Polygon")); pToolbar6 = addToolBar(tr("&Delete")); pToolbar1->addAction(pAction1); pToolbar2->addAction(pAction2); pToolbar3->addAction(pAction3); pToolbar4->addAction(pAction4); pToolbar5->addAction(pAction5); pToolbar6->addAction(pAction6); view_widget_=new ViewWidget(); setCentralWidget(view_widget_); }
//viewwidget.h #ifndef VIEWWIDGET_H #define VIEWWIDGET_H #include <QWidget> #include "ui_viewwidget.h" #include <QMouseEvent> #include <QPainter> class ViewWidget : public QWidget { Q_OBJECT public: ViewWidget(QWidget *parent = 0); ~ViewWidget(); void paintEvent(QPaintEvent *); void mouthPressEvent(QMouseEvent *event); void mouthMoveEvent(QMouseEvent *event); void mouthReleaseEvent(QMouseEvent *event); private: Ui::ViewWidget ui; bool draw_status_; QPoint start_point_; QPoint end_point_; }; #endif // VIEWWIDGET_H
//viewwidget.cpp #include "viewwidget.h" #include <iostream> ViewWidget::ViewWidget(QWidget *parent) : QWidget(parent) { ui.setupUi(this); draw_status_=true; } ViewWidget::~ViewWidget() { } void ViewWidget::mouthPressEvent(QMouseEvent *event) { if (Qt::LeftButton==event->button()) { draw_status_=true; start_point_=end_point_=event->pos(); std::cout<<event->pos().x()<<' '<<event->pos().y()<<std::endl; } } void ViewWidget::mouthMoveEvent(QMouseEvent *event) { if (draw_status_) { end_point_=event->pos(); } } void ViewWidget::mouthReleaseEvent(QMouseEvent *event) { draw_status_=false; } void ViewWidget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.begin(this); painter.drawLine(start_point_,end_point_); painter.end(); update(); }
//main.cpp #include "minidraw.h" #include "viewwidget.h" #include <QtGui/QApplication> #include <iostream> int main(int argc, char *argv[]) { QApplication a(argc, argv); MiniDraw w; w.show(); return a.exec(); }
-
@Geng.Y
I think you need to repaint the widget when you release the mouse. -
Excuse me, I'm not sure what you mean. Could you please help me modify the code ?
-
@Geng.Y
addrepaint()
in theViewWidget::mouseReleaseEvent()
changemouthPressEvent
tomousePressEvent
, so doesmouthMoveEvent
andmouthReleaseEvent
to see if it works -
As @Flotisable say, you are not overriding the functions as they are named
something elsevoid mouthPressEvent(QMouseEvent *event);
void mouthMoveEvent(QMouseEvent *event);
void mouthReleaseEvent(QMouseEvent *event);That is actually mouse and not mouth in front.
-
@Flotisable Thank you very much and it works!
-
I have mistaken mouth and mouse and it works now. But I'm still puzzled why I can't see the output of coordinates of mouse.
-
@Geng.Y
You mean
std::cout<<event->pos().x()<<' '<<event->pos().y()<<std::endl;they should show in Creator ?
In Application output panel -
@Geng.Y
You may use qDebug()<<event->pos() to show them on qt-creator.
If you want to show the coordinates of mouse on view, you may use QTooltip to show them. I found that it was quite useful to show them. -
@mrjj Thank you!
-
@mrjj I‘m using vs2010, how to show the coordinates?
-
@Geng.Y
Hi
Sorry I dont know visual Studio the IDE well enough.
I use only the compiler in Creator. -
@mrjj Thank you very much!
-
@Geng.Y
As suggested you can use the tooltip string
http://doc.qt.io/qt-5/qwidget.html#toolTip-prop
you can then see it directly on the app.