Results 1 to 5 of 5

Thread: Program Crashes while creating a LineItem dinamically

  1. #1
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1

    Question Program Crashes while creating a LineItem dinamically

    Guys, I need somehelp.

    I want to create a line kinda like it works in MS Paint. It's suposse to show it moving around b4 fixing its final size.

    anywyas. Here is the code, hope you guys can catch something I'm not.

    The slot FinalPosition works just fine. It grabs mouseMoveEvent from QGraphicsView and replaces it. When I Trigger New Life in my MainWindow the program chrases. I'm sure the while function is helpin this crash happens, but I simply cant think of anyway to implement this.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QGraphicsScene>
    4. #include <QGraphicsLineItem>
    5. #include <QEvent>
    6. #include <QDropEvent>
    7. #include <QSignalMapper>
    8. #include <QMouseEvent>
    9. #include <QtDebug>
    10.  
    11.  
    12. MainWindow::MainWindow(QWidget *parent) :
    13. QMainWindow(parent),
    14. ui(new Ui::MainWindow)
    15. {
    16. ui->setupUi(this);
    17.  
    18.  
    19. view = new QGV;
    20. MainWindow::setCentralWidget(view);
    21. view->setMouseTracking(true);
    22. QObject::connect(ui->action_Line,SIGNAL(triggered()),this,SLOT(criarLinha()));
    23. QObject::connect(view,SIGNAL(changeFinalPosition(qreal,qreal)),this,SLOT(FinalPosition(qreal,qreal)));
    24.  
    25.  
    26.  
    27. i = 0;
    28. x1 = 0;
    29. y1 = 0;
    30. x2 = 0;
    31. y2 = 0;
    32.  
    33. scene = new QGraphicsScene;
    34. //scene->installEventFilter(view);
    35. view->setScene(scene);
    36.  
    37.  
    38.  
    39.  
    40. }
    41.  
    42. MainWindow::~MainWindow()
    43. {
    44. delete ui;
    45. }
    46.  
    47. // Pulbic Functions
    48. void MainWindow::createLine()
    49. {
    50. int chave = 1;
    51. QGraphicsLineItem *linha = new QGraphicsLineItem(x1,y1,x2,y2);
    52. scene->addItem(linha);
    53.  
    54. qDebug() << "Entrei" << i;
    55.  
    56. while((chave == 1))
    57. {
    58. if(i == 1)
    59. {
    60. linha->setLine(x1,y1,x2,y2);
    61. }
    62. else if (i == 2)
    63. {
    64. linha->setLine(x1,y1,x2,y2);
    65. chave = false;
    66. i=0;
    67. }
    68. }
    69.  
    70. qDebug() << "saÃ*";
    71.  
    72. }
    73.  
    74. void MainWindow::mousePressEvent(QMouseEvent *qme1)
    75. {
    76. QMouseEvent *event = static_cast<QMouseEvent *>(qme1);
    77.  
    78.  
    79. if((event->button() == Qt::LeftButton))
    80. {
    81. if(i == 0)
    82. {
    83. x1 = dynamic_cast<QMouseEvent*>(event)->x();
    84. y1 = dynamic_cast<QMouseEvent*>(event)->y();
    85. i=1;
    86.  
    87. }
    88. else if (i == 1)
    89. {
    90. x2 = dynamic_cast<QMouseEvent*>(event)->x();
    91. y2 = dynamic_cast<QMouseEvent*>(event)->y();
    92. i=2;
    93. }
    94.  
    95. }
    96. }
    97.  
    98.  
    99.  
    100.  
    101. //Public Slots
    102. void MainWindow::criarLinha()
    103. {
    104. x1 = 0;
    105. y1 = 0;
    106. x2 = 0;
    107. y2 = 0;
    108. i = 0;
    109. qDebug() << "pronto pra começar:" << x1 << y1 << x2 << y2;
    110. createLine();
    111. }
    112.  
    113. void MainWindow::FinalPosition(qreal x, qreal y)
    114. {
    115. if (!(x2 == x))
    116. x2 = x;
    117. else if (!(y2 == y))
    118. y2 = y;
    119.  
    120. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Program Crashes while creating a LineItem dinamically

    insufficient information.

    which line is it crashing?
    post the stack dump?

  3. #3
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1

    Default Re: Program Crashes while creating a LineItem dinamically

    It doesnt specify a line. The program just collapses when I execute it.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Program Crashes while creating a LineItem dinamically

    Quote Originally Posted by 21altf View Post
    It doesnt specify a line. The program just collapses when I execute it.
    You need to run it in the debugger.

    You are drawing on the scene so why not handle the mouse events in the view? Something like this:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class GraphicsView : public QGraphicsView
    4. {
    5. Q_OBJECT
    6. public:
    7. GraphicsView(): drawFlag(false), start(0,0), item(0){}
    8. void mousePressEvent(QMouseEvent *event){
    9. if(event->button()!= Qt::LeftButton) return;
    10. drawFlag=true;
    11. start.setX(mapToScene(event->pos()).x());
    12. start.setY(mapToScene(event->pos()).y());
    13. }
    14. void mouseMoveEvent(QMouseEvent *event){
    15. if(!drawFlag) return;
    16. if(item) scene()->removeItem(item);
    17. item =scene()->addLine(start.x(),start.y(),mapToScene(event->pos()).x(),mapToScene(event->pos()).y(),QPen(Qt::red));
    18. }
    19. void mouseReleaseEvent(QMouseEvent *event){
    20. if(drawFlag) {
    21. drawFlag=false;
    22. scene()->addLine(start.x(),start.y(),mapToScene(event->pos()).x(),mapToScene(event->pos()).y(),QPen(Qt::red));
    23. }
    24. }
    25. private:
    26. bool drawFlag;
    27. QPointF start;
    28. };
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QApplication a(argc, argv);
    33. GraphicsView view;
    34. view.setScene(&scene);
    35. scene.setSceneRect(0,0,view.size().width(),view.size().height());
    36. view.show();
    37. return a.exec();
    38. }
    39. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to norobro for this useful post:

    21altf (31st October 2011)

  6. #5
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1

    Default Re: Program Crashes while creating a LineItem dinamically

    Thanks!

    It works now ;D!

Similar Threads

  1. Program crashes
    By Fallen_ in forum Qt Programming
    Replies: 49
    Last Post: 20th September 2010, 01:41
  2. Phonon crashes application when creating a VideoPlayer
    By core2000 in forum Qt Programming
    Replies: 0
    Last Post: 29th September 2009, 08:58
  3. Program crashes on creating new dialog
    By eekhoorn12 in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 11:52
  4. program crashes (QtTestRunner)
    By fmariusd in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2008, 09:27
  5. Program crashes (SIGSEGV)
    By Voldemort in forum Qt Programming
    Replies: 47
    Last Post: 21st May 2007, 20:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.