PDA

View Full Version : How to add Treewidget Items when clicking on PushButton



mkkguru
10th February 2010, 09:47
HI All,

i getting a problem, when iam clicking on pushbutton(OK) the TreeWidgetItems are not adding not only that ,the mainwindow is also closing when iam clicking on push button(OK).plz go throught the below code and let me know what the mistake i had done and how to solve this issues.



// newproject window is another window ,it has some functionalities like ok and canccel PushButton and other widgets
newproject::newproject(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.okButton,SIGNAL(clicked()),this,SLOT(on _okButton_clicked()));
connect(ui.cancelButton,SIGNAL(clicked()),this,SLO T(close()));
}

void newproject::on_okButton_clicked()
{
//ECLogic is the other window(mainwindow)
eclogic->createMainTree();
}



// ECLlogic is the main window
// below is the slot , it will work when i click on New menuitem
void ECLogic::on_action_New_triggered()
{
NewProject=new newproject(main_tab_widget);
NewProject->show();

}
void ECLogic::createMainTree()
{
//this item has to add in the treewidget
QTreeWidgetItem *lnx = new QTreeWidgetItem(ui->treeWidget);
lnx->setText(0,tr("LOGIC"));
ui->treeWidget->addTopLevelItem(lnx);
}


Plz understand the code and let me know further clarifications and how can i solve this problem.

Thanks & Regards,
Mkkguru.

Lykurg
10th February 2010, 10:10
Hi,

connect(ui.okButton,SIGNAL(clicked()),this,SLOT(on _okButton_clicked()));
is not necessary since, setupUi does it for you. Second, go and read some basic C++ book since you obviously don't know the basics.

Where do you initializise eclogic like
eclogic = new ECLogic;
and in your class eclogic there is no ui pointer! Why your app crashes is that you trying to access on method on a null pointer!

EDIT: Ok, see now that you documented some stuff and I just right know find the necessary information. So I assume that you might have initialized your members but therefore check that and use a debugger to see where your app crashes.

mkkguru
10th February 2010, 10:29
Hi,

connect(ui.okButton,SIGNAL(clicked()),this,SLOT(on _okButton_clicked()));
is not necessary since, setupUi does it for you. Second, go and read some basic C++ book since you obviously don't know the basics.

Where do you initializise eclogic like
eclogic = new ECLogic;
and in your class eclogic there is no ui pointer! Why your app crashes is that you trying to access on method on a null pointer!

EDIT: Ok, see now that you documented some stuff and I just right know find the necessary information. So I assume that you might have initialized your members but therefore check that and use a debugger to see where your app crashes.

// eclogic.h
In newproject.h and eclogic .h i initialize eclogic =new ECLogic; in public

i just posted the .cpp code




//below is the ECLogic window(mainwindow) .cpp
ECLogic::ECLogic(QWidget *parent)
: QMainWindow(parent) , ui(new Ui::ECLogicClass)
{
ui->setupUi(this);


If you find any mistakes let me know.

thank you.

Lykurg
10th February 2010, 10:57
Please also post the corresponding header file. And for a simple debug, try following in your code and show us the output:

void newproject::on_okButton_clicked()
{
qWarning() << "on_okButton_clicked";
if(!eclogic)
qWarning() << "no pointer!";
eclogic->createMainTree();
}
//---
void ECLogic::createMainTree()
{
qWarning() << "createMainTree";
QTreeWidgetItem *lnx = new QTreeWidgetItem(ui->treeWidget);
lnx->setText(0,tr("LOGIC"));
ui->treeWidget->addTopLevelItem(lnx);
}

mkkguru
11th February 2010, 05:12
Please also post the corresponding header file. And for a simple debug, try following in your code and show us the output:

void newproject::on_okButton_clicked()
{
qWarning() << "on_okButton_clicked";
if(!eclogic)
qWarning() << "no pointer!";
eclogic->createMainTree();
}
//---
void ECLogic::createMainTree()
{
qWarning() << "createMainTree";
QTreeWidgetItem *lnx = new QTreeWidgetItem(ui->treeWidget);
lnx->setText(0,tr("LOGIC"));
ui->treeWidget->addTopLevelItem(lnx);
}

Hi,

//below is the newproject.h file

class newproject : public QDialog
{
Q_OBJECT
public:
Ui::NewProject ui;
newproject(QWidget *parent = 0);
~newproject();
ECLogic *eclogic;

private:

newproject *NewPrjct;

public slots:
void on_cancelButton_clicked();
void on_okButton_clicked();


//below is the ECLogic.h


#include "newproject.h"

#include <QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QDialog>

#include <QAbstractButton>
#include <QModelIndex>

class ECLogic;
class NewProject;

/*************************
main window(eclogic) Ui name
*************************/
namespace Ui
{
class ECLogicClass;
}

class ECLogic : public QMainWindow
{
Q_OBJECT

public:
ECLogic(QWidget *parent = 0);
~ECLogic();
ECLogic *eclogic;
void createTreewidget();
void create4Bins();
void createMainTree();

/*************************
main window(eclogic) actions slots
*************************/
private slots:
//when i click on new menu,NewProject window will open.
void on_action_New_triggered();
void on_actionAdd_PLC_triggered();
private:
/*************************
main window objects
*************************/
Ui::ECLogicClass *ui;
newproject *NewProject;
ECLogic *eclogic;


Out put is still getting closing when i click on (Ok)pushbutton


Starting /home/th/Desktop/E.0.1/bin/EL...
on_okButton_clicked
no pointer!
createMainTree

The program has unexpectedly finished.

/home/sumith/Desktop/ECIL-2.0.1/bin/ECIL exited with code 0

Lykurg
11th February 2010, 09:55
The code could not be real! The message "no pointer!" indicates that you don't have initialized eclogic! Add to your constructor
eclogic = new ECLogic; And therefore I can not understand why "createMainTree" is printed out. So maybe just upload a sample project, that reproduce your problem.

faldzip
11th February 2010, 11:12
And therefore I can not understand why "createMainTree" is printed out.
I don't understand why this guy is trying to write an application if he does not know any programming language... He has two eclogic class members: 1 public and 1 private...

So maybe just upload a sample project, that reproduce your problem.
This won't help. I tried to understand the code of his project earlier when there was another topic, but it is impossible as this guy has incredible coding style: almost every line starts with different numbers of spaces from the left... he is using global, local and member variables with the same name which he expect to be the same one variable and so on. He has no idea about C++ programming and can't understand that using umbrella doesn't make sense if you don't know how to open it in rainy day!

mkkguru
11th February 2010, 13:48
I don't understand why this guy is trying to write an application if he does not know any programming language... He has two eclogic class members: 1 public and 1 private...

This won't help. I tried to understand the code of his project earlier when there was another topic, but it is impossible as this guy has incredible coding style: almost every line starts with different numbers of spaces from the left... he is using global, local and member variables with the same name which he expect to be the same one variable and so on. He has no idea about C++ programming and can't understand that using umbrella doesn't make sense if you don't know how to open it in rainy day!

Hi faldzip,

Thanks for the answering ,As iam a fresher iam in learning stage ,if you know about my issue plz help regrding this,and dont underestimate others..if i confuse you with my code l will post my project ,

thanks & regards,