PDA

View Full Version : QTreeWidget clicked signal



^NyAw^
14th May 2007, 18:39
Hi,

I'm having problems to get a signal when I click on a QTreeWidget. The problem is that if no item is selected there is no signal emmited.

I need to know if I clicked on an empy region of the Tree because I have actions that I have to enable or diable depending on the item that is clicked. But if I click out of any item I would all the actions disabled.

Thanks,

wysota
14th May 2007, 18:45
Try this:

connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(yourSlotGoesHere()));
Just make sure clicking outside any item clears the selection.

^NyAw^
14th May 2007, 18:50
Hi,

I have tryied it. It connects but the signal is only emmited when I click on a item.

wysota
14th May 2007, 20:05
Do it lowlevel then. Reimplement the mouseReleaseEvent of the widget and use itemAt() to check the item under cursor.

^NyAw^
15th May 2007, 10:09
Hi,

So, have I to inherit from QTreeWidget and reimplement the "mouseReleaseEvent" function?

Thanks,

^NyAw^
21st May 2007, 16:34
Hi,

Anyone knows how to make it? Have I to inherit from QTreeWidget and reimplement "mouseReleaseEvent" function or there is another way?

Thanks,

wysota
21st May 2007, 16:44
Hmm.... Could you read my last post again? :)

^NyAw^
21st May 2007, 16:50
Hi,

Ok, Ok, so ther isn't another way.

Thanks,

mkkguru
22nd January 2010, 07:04
i too have a getting same problem related to treewidget item signals ,


QTreeWidgetItem *plc = new QTreeWidgetItem(ui->treeWidget);
plc->setText(0,QString("PLC %1").arg(i));

if(plc)
{
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(plcconfig()));
}

QTreeWidgetItem *plcItem = new QTreeWidgetItem(plc);
plcItem->setText(0, tr("CONFIGURATION"));
plc->addChild(plcItem);

QTreeWidgetItem *cpuconfig = new QTreeWidgetItem(plcItem);
cpuconfig->setText(0, tr("CPU CONFIGURATION"));
plcItem->addChild(cpuconfig);

// if(cpuconfig)
// {
// connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showCPU()));
// }

QTreeWidgetItem *varconfig = new QTreeWidgetItem(plcItem);
varconfig->setText(0, tr("VARIABLE CONFIGURATION"));
plcItem->addChild(varconfig);

QTreeWidgetItem *progconfig = new QTreeWidgetItem(varconfig);
progconfig->setText(0, tr("PROGRAM VARIABLE CONFIGURATION"));
varconfig->addChild(progconfig);

if(varconfig)
{
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showpvc()));
}

As iam using above signal when iam cliicking on PROGRAM VARIABLE CONF..... the required window has to opened as below.


void ECLogic::showpvc( )
{
QTabBar *tab_4=new QTabBar(ui->widgetConf);
PCD= new programvariableconfiguration(tab_4);
PCD->setWindowTitle(QString("PVC"));
tabwidget3->insertTab(0,tab_4,QString("PVC"));
tab_4->setCurrentIndex(tabwidget3->indexOf(ui->widgetConf));
tab_4->setTabsClosable(true);
tab_4->setEnabled(true);
PCD->show();
}

but my problem is getting the connections to all the treewidggetitems not for the particular....is there any way to get my condtion ....so please suggests me the way to write the code........

wysota
22nd January 2010, 11:06
You get the pointer to the item in the signal so you can check its contents in the slot.

mkkguru
23rd January 2010, 08:23
how can i set the pointer to the signal, we can't set values in connect statements.we should use types there.

Try this:

connect(ui.tree, SIGNAL(itemClicked (QTreeWidgetItem*,int)), this, SLOT(showplc()))

wysota
23rd January 2010, 09:40
how can i set the pointer to the signal, we can't set values in connect statements.we should use types there.

I didn't say anything about "pointers to signals" nor did I say about setting a value in a connect statement.


connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(maybeShowpvc(QTreeWidgetItem*))) ;
// ...
void X::maybeShowpvc(QTreeWidgetItem *item){
if(item) {
// ...
} else {
// ...
}
}

mkkguru
23rd January 2010, 12:03
actually i have to write many signals to QTreewidgetitems so when i clikcing on item other signals also working ,so i need to stop getting other windows...only particular window when we click on Qtreewidgetitem has to opened.......

wysota
23rd January 2010, 14:05
I don't understand what you mean.

mkkguru
23rd January 2010, 14:25
i have to get connections to particular item when we clik.......and no other connections has to established to treewidgetitems

wysota
23rd January 2010, 15:11
So? What's the problem? Can't you check if the item that you want was clicked and if not then simply do nothing?

mkkguru
25th January 2010, 06:05
// connections written in constructor

connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(maybeShowpvc(QTreeWidgetItem*))) ;

connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(maybeShowplc(QTreeWidgetItem*))) ;

connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(maybeShowioc(QTreeWidgetItem*))) ;

connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem ,int)),this,SLOT(maybeShowldr(QTreeWidgetItem*)));


// slot methods

void X::maybeShowpvc(QTreeWidgetItem *plc)
{
if(item){
plc->show();
......
// code
}

void X::maybeShowplc(QTreeWidgetItem *pvc)
{
//connection code
pvc->show();
.............

}

void X::maybeShowpvc(QTreeWidgetItem *ioc)
{
//connection code
.....
}

void X::maybeShowldr(QTreeWidgetItem *ldr)
{
//connection code
.....

}

when iam using above code all the connections and slots are working at a time...........is there any possibility to get the connections to particular item (plc,pvc,ioc,ldr) ...

aamer4yu
25th January 2010, 06:25
You dont need separate slots, you need only one slot.

connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem ,int)),this,SLOT(showItem(QTreeWidgetItem*)));
MyWindow::showItem(QTreeWidgetItem* item)
{
if(item == plcItem)
plc->show();
else if(item == pvcItem)
pvc->show();
...
}

where plcItem and pvcItem are the QTreeWidgetItems you have added in your tree.

faldzip
25th January 2010, 09:34
I think that mkkguru misunderstood or misused word "connection". Can you explain in other words what do you mean by "getting the connection to particular item"?

Just to make things clear:
connect() method only sets the signal and slot in such way, that if signal is emitted then the connected slot is called (with emitted arguments). So if you connect your QTreeWidget itemClicked(QTreeWidgetItem *, int) signal to 4 slots, then clicking on item will emit one signal ONCE but this will call every slot connected to your signal, so all your 4 slots will be called. But you did not notice that there is QTreeWidgetItem* argumet in that signal, which is there to let you know what item was clicked. So in your case there should be one slot connected to this itemClicked() signal, where depending on information retrieved from given QTreeWidgetItem*, you will know that this clicked item is pvc, plc, ioc, etc...

mkkguru
25th January 2010, 10:28
Image shows the treewidget ,when we click on item the particular item window has to opend.

//connection

connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem* ,int)),this,SLOT(showItem(QTreeWidgetItem*)));

//slots method

void xxx::showItem(QTreeWidgetItem* item)
{
if(item == lnx)
{
ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
ECLogic_Lnx->show();
}
else
if(item == plc)
tab_3=new QTabBar(main_tab_widget);
PLCconfig=new plcconfiguration(tab_3);
PLCconfig->setWindowTitle(QString("PLC"));
main_tab_widget->insertTab(0,tab_3,QString("PLC"));
tab_3->setCurrentIndex(main_tab_widget->indexOf(tab_3));
PLCconfig->show();
}


When iam using above code the connecting to other windows are not getting ,where ECLogic_Lnx , PLCconfig,.. are window names.
where lnx,plc,...are qtreewidgetitem obj names,so is there any method to check the condition

[

faldzip
25th January 2010, 11:13
what is the exact type of plc and lnx variables?

mkkguru
27th January 2010, 05:39
Exact type of plc and lnx are as follows.


QTreeWidgetItem *lnx=new QTreeWidgetItem(treeWidget);
QTreeWidgetItem *plc=new QTreeWidgetItem(plc);

aamer4yu
27th January 2010, 06:41
so is there any method to check the condition
What condition ??

mkkguru
27th January 2010, 08:11
plz go through the below code and before posted code........


void xxx::showItem(QTreeWidgetItem* item)

{

if(item == lnx)

{

ECLogic_Lnx=new eclogic_lnx(main_tab_widget);

ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));

ECLogic_Lnx->show();

}

else

if(item == plc)

tab_3=new QTabBar(main_tab_widget);

PLCconfig=new plcconfiguration(tab_3);

PLCconfig->setWindowTitle(QString("PLC"));

main_tab_widget->insertTab(0,tab_3,QString("PLC"));

tab_3->setCurrentIndex(main_tab_widget->indexOf(tab_3));

PLCconfig->show();

}

while iam using if(........) condition the other windows(plc,pvc,....)has to open when i click the treewidgetitems like PLC,ECLOGIC-LNX........but the condition is not working correctly

aamer4yu
27th January 2010, 08:30
I guess you are messing with the tab bar and tab widget.
PLCconfig->show();
I guess that should be tab_3->show();
or main_tab_widget->show();

I guess you are not showing the parent widget and somehow messing with tab widget.

mkkguru
27th January 2010, 09:59
I guess you are messing with the tab bar and tab widget.
PLCconfig->show();
I guess that should be tab_3->show();
or main_tab_widget->show();

I guess you are not showing the parent widget and somehow messing with tab widget.

PLCconfig,ECLogic_Lnx are different ui names iam calling that ui's in tab bar and placing in main_tab_widget.,so me facing problem in writing the connections to treewidget,where items like plc,...etc has to click,when we click on plc (QTreewidgetItem) the PLCconfig(ui) has to open,then when we click on ECLOGIC-lnx(QTrreewidgetItem)the ECLogic-LNX(ui)has to open,.............

please kindly go through the code and image(treewidget) which i posted and plz give me the solution.

faldzip
27th January 2010, 10:39
It is hard to understand what you are saying but your code where you add new tab is wrong. For what do you need new QTabBar?
It should be like this:


main_tab_widget->insertTab(0, new plcconfiguration(main_tab_widget), "PLC");
main_tab_widget->setCurrentIndex(0);

mkkguru
27th January 2010, 13:13
It is hard to understand what you are saying but your code where you add new tab is wrong. For what do you need new QTabBar?
It should be like this:


main_tab_widget->insertTab(0, new plcconfiguration(main_tab_widget), "PLC");
main_tab_widget->setCurrentIndex(0);


ok thankq,

i tried ,i got the required solution but the connections to the treewidget not getting ,as iam attaching the treewidget image,when iam clicking the items in that the particular window(Gui )has to open

faldzip
27th January 2010, 14:03
Can you finally explain what is a "connection to treewidget"?
You said that now your code is working but it is not working... So can you decide if it is or it is not?
I don't see how your picture can help us understand your problem?
Maybe you can find someone to help you explain your problem in english or use some google translator to help you.
And use CODE tags when pasting code.

mkkguru
28th January 2010, 05:33
Can you finally explain what is a "connection to treewidget"?
You said that now your code is working but it is not working... So can you decide if it is or it is not?
I don't see how your picture can help us understand your problem?
Maybe you can find someone to help you explain your problem in english or use some google translator to help you.
And use CODE tags when pasting code.

do you know how we can use signals and slots in treewidget?

do you know below code?

connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showPLC(QTreeWidgetItem *)));

faldzip
28th January 2010, 09:06
do you know how we can use signals and slots in treewidget?
Yes, I know, like in every other class.


do you know below code?
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showPLC(QTreeWidgetItem *)));
Yes, I know, you've pasted it before :]

Ok, so please run this example and tell me if it is what you want, because I did what I understand you want but as I said, it is difficult to understand your needs.


#include <QtGui>

class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0) : QWidget(parent) {
treeWidget = new QTreeWidget(this);
tabWidget = new QTabWidget(this);
tabWidget->setTabsClosable(true);
QHBoxLayout *la = new QHBoxLayout;
la->addWidget(treeWidget);
la->addWidget(tabWidget);
setLayout(la);

for (int i = 0; i < 2; ++i) {
QTreeWidgetItem *p = new QTreeWidgetItem(treeWidget, QStringList() << QString("parent item %1").arg(i));
for (int j = 0; j < 5; ++j) {
QTreeWidgetItem *c = new QTreeWidgetItem(QStringList() << QString("child item %1%2").arg(i).arg(j));
p->addChild(c);
}
}
connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(slotItemClicked(QTreeWidgetItem*)));
connect(tabWidget, SIGNAL(tabCloseRequested(int)), SLOT(slotCloseTab(int)));
}
private slots:
void slotItemClicked(QTreeWidgetItem *item) {
QWidget *w = itemToWidget.value(item, 0);
if (w) {
tabWidget->setCurrentWidget(w);
return;
}
QLabel *label = new QLabel(item->text(0));
itemToWidget.insert(item, label);
widgetToItem.insert(label, item);
label->setAlignment(Qt::AlignCenter);
tabWidget->insertTab(0, label, item->text(0));
tabWidget->setCurrentIndex(0);
}
void slotCloseTab(int index) {
QWidget *w = tabWidget->widget(index);
itemToWidget.remove(widgetToItem.value(w));
widgetToItem.remove(w);
tabWidget->removeTab(index);
}
private:
QTabWidget *tabWidget;
QTreeWidget *treeWidget;
QMap<QTreeWidgetItem *, QWidget *> itemToWidget;
QMap<QWidget *, QTreeWidgetItem *> widgetToItem;
};

int main(int argc, char **argv)
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"

mkkguru
28th January 2010, 11:11
Yes, I know, like in every other class.

Yes, I know, you've pasted it before :]

Ok, so please run this example and tell me if it is what you want, because I did what I understand you want but as I said, it is difficult to understand your needs.


#include <QtGui>

class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0) : QWidget(parent) {
treeWidget = new QTreeWidget(this);
tabWidget = new QTabWidget(this);
tabWidget->setTabsClosable(true);
QHBoxLayout *la = new QHBoxLayout;
la->addWidget(treeWidget);
la->addWidget(tabWidget);
setLayout(la);

for (int i = 0; i < 2; ++i) {
QTreeWidgetItem *p = new QTreeWidgetItem(treeWidget, QStringList() << QString("parent item %1").arg(i));
for (int j = 0; j < 5; ++j) {
QTreeWidgetItem *c = new QTreeWidgetItem(QStringList() << QString("child item %1%2").arg(i).arg(j));
p->addChild(c);
}
}
connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(slotItemClicked(QTreeWidgetItem*)));
connect(tabWidget, SIGNAL(tabCloseRequested(int)), SLOT(slotCloseTab(int)));
}
private slots:
void slotItemClicked(QTreeWidgetItem *item) {
QWidget *w = itemToWidget.value(item, 0);
if (w) {
tabWidget->setCurrentWidget(w);
return;
}
QLabel *label = new QLabel(item->text(0));
itemToWidget.insert(item, label);
widgetToItem.insert(label, item);
label->setAlignment(Qt::AlignCenter);
tabWidget->insertTab(0, label, item->text(0));
tabWidget->setCurrentIndex(0);
}
void slotCloseTab(int index) {
QWidget *w = tabWidget->widget(index);
itemToWidget.remove(widgetToItem.value(w));
widgetToItem.remove(w);
tabWidget->removeTab(index);
}
private:
QTabWidget *tabWidget;
QTreeWidget *treeWidget;
QMap<QTreeWidgetItem *, QWidget *> itemToWidget;
QMap<QWidget *, QTreeWidgetItem *> widgetToItem;
};

int main(int argc, char **argv)
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"




Hi ,

I really thanks ,its working for me ,thanks for the code...

faldzip
28th January 2010, 11:33
I really hope you make some effort to understand this piece of code and not only copy-paste it to your application.
And next time please try to describe your problem more clearly and attach image which could be helpful, because yours did not even contain any tab widget and we already know how tree widget looks like...

mkkguru
28th January 2010, 12:42
Hi,

iam attaching my project,u will now understand about my project, plz go through the below ..

1)click on New menu ->new Project window has to open and ECLogic-LNX wil place in Treewidget.then
2)click on ECLogic-LNX->ECLogic-LNX window has to open.
3) click on PLC->PLC configrtn window has to open.
4)click on CPU configuration ->cpu confi window '" " """""""""""""""""""""
5)click on Program variable configuration->Program varbl config "" """"" """"
6)click on IO configuration -> IO Config """"""""""""""""""""'
7)click on BIN ->Bin """"""""""""""""""""""'

But my problem iam getting in my project is when iam clicking on treewidget item all the windows are coming....
plz help to get the solutions of above mention points..

faldzip
28th January 2010, 13:08
Man, read all answers to your posts once again, ok? It was already mentioned that this is wrong way:


connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showPLC(QTreeWidgetItem *)));
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showpvc(QTreeWidgetItem *)));
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showLDR()));
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showIOC(QTreeWidgetItem*)));
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showCHNL(QTreeWidgetItem *)));
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showCPU(QTreeWidgetItem *)));
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showSLT(QTreeWidgetItem *)));
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showBIN(QTreeWidgetItem *)));

connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem* ,int)),this,SLOT(showItem(QTreeWidgetItem*)));

and read about Signals and slots in Qt Assistant and you will know why it is working wrong. There is even a picture showing how it is working.
And the solution has already been given to you in previous posts.

mkkguru
28th January 2010, 13:32
Hi faldzip,

I tried the previous post code ,which i used the single slot,there i written like below

void ECLogic::showItem(QTreeWidgetItem* item )
{
if(item=="PLC")
{
PLCconfig=new plcconfiguration();
main_tab_widget->insertTab(0,PLCconfig, "PLC");
main_tab_widget->setCurrentIndex(0);
PLCconfig->show();
}
else if(item==lnx)
{
ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
ECLogic_Lnx->show();
}
}
but above condition is not working for me,can you please solve how to write that code......

faldzip
28th January 2010, 14:04
What is the type of item and what is the type of "PLC"?
And in your code lnx is a local variable in different method so how do you want to reference it in this method?
I think you need to revise your C++ knowledge...
Why can't you use QTreeWidgetItem methods like for example text() to get know what item did you clicked?
You can event set your data with QTreeWidgetItem::setData() with Qt::UserRole while creating item to let you know what item it is.


QTreeWidgetItem lnx = new QTreeWidgetItem(ui->treeWidget);
lnx->setData(0, Qt::UserRole, "lnx");

// and check it:
if (item->data().toString() == "lnx")


P.S. And use CODE tags for pasting code! Your code is already a mess in terms of formatting and without CODE tags it is even worse.

mkkguru
28th January 2010, 15:11
Hi,
I didnt mention any type to Item and PLC ,i just created as QTreewidgetItem *item and QTreeWidgetItem *plc ,please go through my attached project in previous post ,let me know what i done the mistakes in code,as iam new to Qt,this is the first project iam handling ,please feel free to answer to my post .
thankq ..

faldzip
28th January 2010, 15:57
Hi,
I didnt mention any type to Item and PLC ,i just created as QTreewidgetItem *item and QTreeWidgetItem *plc
I mean C++ type... To make you things easier I can say that in this code:


void ECLogic::showItem(QTreeWidgetItem* item )
{
if(item=="PLC")
{
PLCconfig=new plcconfiguration();
main_tab_widget->insertTab(0,PLCconfig, "PLC");
main_tab_widget->setCurrentIndex(0);
PLCconfig->show();
}
else if(item==lnx)
{
ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
ECLogic_Lnx->show();
}
}

item's type is QTreeWidgetItem* and "PLC" is const char *, so both are pointers. Do you want to compare two memory addresses of two completely different things? I don't think so...
And again:


i just created as QTreewidgetItem *item and QTreeWidgetItem *plc

You have created them in another method as local variables so they not exist where you want to reference them. They are dead :]

Your problem now is pure C++ issue. Please revise your C++ basics, like local variables, scopes, referencing pointers and so on.

And your project your slot showItem looks:


void ECLogic::showItem(QTreeWidgetItem* item )
{

ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
ECLogic_Lnx->show();

QStringList item;
item << "PLC" << "CPU CONFIGURATION" << "PROGRAM VARIABLE CONFIGURATION" << "IO CONFIGURATION"
<< "BIN" << "SLOT" <<"LADDER PROGRAM";

if(item=="PLC")
{
PLCconfig=new plcconfiguration();
main_tab_widget->insertTab(0,PLCconfig, "PLC");
main_tab_widget->setCurrentIndex(0);
PLCconfig->show();
}
else if(item==lnx)
{
ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
ECLogic_Lnx->show();
}
}

Which does not make sense at all.

EDIT:
Oops now I found that there is another lnx variable which is class member - that would be better, but when you are creating new QTreeWidgetItem you are assigning it to the local variable with the same name (lnx) so your member variable lnx is always unassigned and referencing it will cause segmentation fault.

wysota
28th January 2010, 21:13
P.S. And use CODE tags for pasting code! Your code is already a mess in terms of formatting and without CODE tags it is even worse.

You can "reward" the author with negative reputation if you feel he is not behaving well by clicking the star icon under his post.

mkkguru
29th January 2010, 12:53
I mean C++ type... To make you things easier I can say that in this code:


void ECLogic::showItem(QTreeWidgetItem* item )
{
if(item=="PLC")
{
PLCconfig=new plcconfiguration();
main_tab_widget->insertTab(0,PLCconfig, "PLC");
main_tab_widget->setCurrentIndex(0);
PLCconfig->show();
}
else if(item==lnx)
{
ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
ECLogic_Lnx->show();
}
}

item's type is QTreeWidgetItem* and "PLC" is const char *, so both are pointers. Do you want to compare two memory addresses of two completely different things? I don't think so...
And again:

You have created them in another method as local variables so they not exist where you want to reference them. They are dead :]

Your problem now is pure C++ issue. Please revise your C++ basics, like local variables, scopes, referencing pointers and so on.

And your project your slot showItem looks:


void ECLogic::showItem(QTreeWidgetItem* item )
{

ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
ECLogic_Lnx->show();

QStringList item;
item << "PLC" << "CPU CONFIGURATION" << "PROGRAM VARIABLE CONFIGURATION" << "IO CONFIGURATION"
<< "BIN" << "SLOT" <<"LADDER PROGRAM";

if(item=="PLC")
{
PLCconfig=new plcconfiguration();
main_tab_widget->insertTab(0,PLCconfig, "PLC");
main_tab_widget->setCurrentIndex(0);
PLCconfig->show();
}
else if(item==lnx)
{
ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
ECLogic_Lnx->show();
}
}

Which does not make sense at all.

EDIT:
Oops now I found that there is another lnx variable which is class member - that would be better, but when you are creating new QTreeWidgetItem you are assigning it to the local variable with the same name (lnx) so your member variable lnx is always unassigned and referencing it will cause segmentation fault.


Hi,
Thankq.

mkkguru
30th January 2010, 12:42
Hi All,
i got my requirement from below code ,thanks all.


void xxx::xxx()
{
]if (item->data(0,Qt::UserRole) == "PLC")
{
PLCconfig=new plcconfiguration();
scrollArea1=new QScrollArea;
scrollArea1->setWidget(PLCconfig);
main_tab_widget->insertTab(0,scrollArea1, "PLC");
itemToWidget.insert(item,scrollArea1);
widgetToItem.insert(scrollArea1,item);
main_tab_widget->setCurrentIndex(0);
PLCconfig->show();
}

As i need to have many modifications in my project ,plz welcome my threads in near future.....
thanks .