1 Attachment(s)
The program has unexpectedly finished.
hi everybody
When I click on the "Click me" button , I get the message :
Quote:
The program has unexpectedly finished.
Then program is closed !
Attachment 8871
why ?!
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QString>
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include <QTreeWidget>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTreeWidget>
#include <QFont>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->treeWidget->setColumnCount(2);
addRoot("Hello","World");
}
MainWindow::~MainWindow()
{
delete ui;
}
{
ptr->setText(0,name);
ptr->setText(1,dec);
addChild(ptr,name,dec);
}
{
ptr1->setText(0,name);
ptr1->setText(1,dec);
parent->addChild(ptr1);
}
void MainWindow::on_pushButton_clicked()
{
for(int i=0 ; i<100 ;i++)
{
ui->progressBar->setValue(i);
}
if(ptr->isSelected())
{
ptr->setBackgroundColor(0,Qt::red);
ptr->setBackgroundColor(1,Qt::red);
}
}
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.
Re: The program has unexpectedly finished.
is it true ? :-?
Code:
void MainWindow::on_pushButton_clicked()
{
for(int i=0 ; i<100 ;i++)
{
ui->progressBar->setValue(i);
}
if(ui->treeWidget->currentItem()->isSelected())
{
ptr->setBackgroundColor(0,Qt::red);
ptr->setBackgroundColor(1,Qt::red);
}
}
Re: The program has unexpectedly finished.
Nop, rather:
Code:
void MainWindow::on_pushButton_clicked()
{
for(int i=0 ; i<100 ;i++)
{
ui->progressBar->setValue(i);
}
if(ptr && ptr->isSelected())
{
ptr->setBackgroundColor(0,Qt::red);
ptr->setBackgroundColor(1,Qt::red);
}
}
or use selectedItems().cout() ...
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?