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 ?!
#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
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QString>
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include <QTreeWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
void addRoot(QString name,QString dec);
void addChild(QTreeWidgetItem *parent,QString name,QString dec);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
#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);
}
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTreeWidget>
#include <QFont>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->treeWidget->setColumnCount(2);
addRoot("Hello","World");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addRoot(QString name,QString dec)
{
QTreeWidgetItem *ptr = new QTreeWidgetItem(ui->treeWidget) ;
ptr->setText(0,name);
ptr->setText(1,dec);
addChild(ptr,name,dec);
}
void MainWindow::addChild(QTreeWidgetItem *parent,QString name,QString dec)
{
QTreeWidgetItem *ptr1 = new QTreeWidgetItem;
ptr1->setText(0,name);
ptr1->setText(1,dec);
parent->addChild(ptr1);
}
void MainWindow::on_pushButton_clicked()
{
QTreeWidgetItem *ptr = ui->treeWidget->currentItem();
for(int i=0 ; i<100 ;i++)
{
ui->progressBar->setValue(i);
QThread::msleep(2);
}
if(ptr->isSelected())
{
ptr->setBackgroundColor(0,Qt::red);
ptr->setBackgroundColor(1,Qt::red);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks