Results 1 to 5 of 5

Thread: The program has unexpectedly finished.

  1. #1
    Join Date
    Mar 2013
    Posts
    45
    Thanks
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default The program has unexpectedly finished.

    hi everybody

    When I click on the "Click me" button , I get the message :

    The program has unexpectedly finished.
    Then program is closed !

    TreeWidget.gif

    why ?!

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include <QString>
    4. #include <QMainWindow>
    5. #include <QtCore>
    6. #include <QtGui>
    7. #include <QTreeWidget>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16. void addRoot(QString name,QString dec);
    17. void addChild(QTreeWidgetItem *parent,QString name,QString dec);
    18. public:
    19. explicit MainWindow(QWidget *parent = 0);
    20. ~MainWindow();
    21.  
    22.  
    23. private slots:
    24. void on_pushButton_clicked();
    25.  
    26. private:
    27. Ui::MainWindow *ui;
    28. };
    29.  
    30. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QTreeWidget>
    4. #include <QFont>
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. ui->treeWidget->setColumnCount(2);
    11. addRoot("Hello","World");
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::addRoot(QString name,QString dec)
    20. {
    21. QTreeWidgetItem *ptr = new QTreeWidgetItem(ui->treeWidget) ;
    22. ptr->setText(0,name);
    23. ptr->setText(1,dec);
    24. addChild(ptr,name,dec);
    25. }
    26. void MainWindow::addChild(QTreeWidgetItem *parent,QString name,QString dec)
    27. {
    28. ptr1->setText(0,name);
    29. ptr1->setText(1,dec);
    30. parent->addChild(ptr1);
    31. }
    32.  
    33. void MainWindow::on_pushButton_clicked()
    34. {
    35. QTreeWidgetItem *ptr = ui->treeWidget->currentItem();
    36.  
    37. for(int i=0 ; i<100 ;i++)
    38. {
    39. ui->progressBar->setValue(i);
    40. QThread::msleep(2);
    41. }
    42. if(ptr->isSelected())
    43. {
    44. ptr->setBackgroundColor(0,Qt::red);
    45. ptr->setBackgroundColor(1,Qt::red);
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2012
    Location
    Paris
    Posts
    11
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: The program has unexpectedly finished.

    If you click in the button and no items are selected, ui->treeWidget->currentItem() return NULL.
    And when the line ptr->isSelected() is executed, your program crash...
    You need to add robustness.

  3. The following user says thank you to Guiom for this useful post:

    smemamian (31st March 2013)

  4. #3
    Join Date
    Mar 2013
    Posts
    45
    Thanks
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: The program has unexpectedly finished.

    is it true ? :-?
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3.  
    4.  
    5. for(int i=0 ; i<100 ;i++)
    6. {
    7. ui->progressBar->setValue(i);
    8. QThread::msleep(2);
    9. }
    10. if(ui->treeWidget->currentItem()->isSelected())
    11. {
    12. QTreeWidgetItem *ptr = ui->treeWidget->currentItem();
    13. ptr->setBackgroundColor(0,Qt::red);
    14. ptr->setBackgroundColor(1,Qt::red);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Apr 2012
    Location
    Paris
    Posts
    11
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: The program has unexpectedly finished.

    Nop, rather:

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QTreeWidgetItem *ptr = ui->treeWidget->currentItem();
    4.  
    5. for(int i=0 ; i<100 ;i++)
    6. {
    7. ui->progressBar->setValue(i);
    8. QThread::msleep(2);
    9. }
    10. if(ptr && ptr->isSelected())
    11. {
    12. ptr->setBackgroundColor(0,Qt::red);
    13. ptr->setBackgroundColor(1,Qt::red);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    or use selectedItems().cout() ...

  6. The following user says thank you to Guiom for this useful post:

    smemamian (31st March 2013)

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: The program has unexpectedly finished.

    Seems a rather pointless progress bar. Empty to full in 100 steps over 200 milliseconds... and then do the 'work' in fractions of a millisecond?

Similar Threads

  1. Replies: 2
    Last Post: 28th March 2013, 22:28
  2. Replies: 4
    Last Post: 17th September 2012, 15:23
  3. the program has unexpectedly finished
    By narlapavan in forum Qt Programming
    Replies: 9
    Last Post: 9th July 2012, 09:04
  4. Replies: 6
    Last Post: 24th June 2012, 18:32
  5. Program has unexpectedly finished
    By Maluko_Da_Tola in forum Newbie
    Replies: 5
    Last Post: 1st December 2010, 09:54

Tags for this Thread

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.