PDA

View Full Version : Qt signal/slot auto connect problem in TREE



kamlmish
10th December 2010, 03:46
Hi
I have created an app , where I click a node of a tree, it would open a website.
my problem is that , it should work only for a particular node, but currently it works for every node.
the code for creating the tree is



//creates a child of a root
QTreeWidgetItem* SettingsTree::createItem(const QString &text, QTreeWidgetItem *parent, int index)
{
QTreeWidgetItem *after = 0;
if (index != 0)
after = childAt(parent, index - 1);

if (parent)
item = new QTreeWidgetItem(parent, after);
else
item = new QTreeWidgetItem(this, after);

item->setText(0, text);
return item;
}


//finds the child at particular index
QTreeWidgetItem *SettingsTree::childAt(QTreeWidgetItem *parent, int index)
{
if (parent)
return parent->child(index);
else
return topLevelItem(index);
}
//tree 1 created
QTreeWidgetItem* SettingsTree::eBeamTREE()
{

root->setText(0,"EBEAM GALLERY");
for(int i=0;i<1;i++)
{

createItem("Art & Design",root,0);
createItem("Classroom Tools",root,1);
createItem("Dynamic Content",root,2);
createItem("Geography",root,3);
createItem("Health",root,4);
createItem("History & Social Studies",root,5);
createItem("Literature & Language Arts",root,6);
createItem("Mathematics",root,7);
createItem("Music & Theatre",root,8);
createItem("Recreation & Games",root,9);
createItem("Science",root,10);
createItem("Backgrounds",root,11);
createItem("Tips & Tricks",root,12);

}
return root;
}

//tree 2
QTreeWidgetItem* SettingsTree::eBeamMYGALLERY()
{

root1->setText(0,"MY GALLERY");
for(int i =0;i<1;i++)
{
createItem("My Gallery",root1,0);
}
return root1;
}


//tree3
QTreeWidgetItem* SettingsTree::eBeamRESOURCETREE()
{
QTreeWidgetItem* child;
root2->setText(0,"RESOURCES");
for(int i=0;i<1;i++)
{
child = createItem("eBeam Web Resources",root2,0);

}
root2->setFlags(Qt::ItemIsSelectable);
return child;
}


Codeto connect to a website


void MainWindow::openwebsite()
{
QTreeWidgetItemIterator it(settTree,QTreeWidgetItemIterator::Selectable);
while(*it)
{
if((*it)->text(0)=="eBeam Web Resources")
{
QUrl url(QString("http://www.e-beam.com/education/web-resources.html"));
bool flag = QDesktopServices::openUrl(url);
if (flag == false)
{
qDebug()<<"Error Opening the website";
}
}

++it;
}
}

FelixB
10th December 2010, 08:37
maybe the Problem is at the string comparison.are you sure, that text(0) is another string for the other nodes? try the following code and debug it.


QString test = (*it)->text(0);
if(test==QString("eBeam Web Resources"))

franz
10th December 2010, 12:47
I would put the url in a custom role (Qt::UserRole or higher) and fetch it from the item. If an url is set, you open it. If not, you open nothing.