PDA

View Full Version : QTreeWidget and QStackedWidget



Kill3rReaper
12th October 2011, 02:09
Hello Everyone!
I have a QTreeWidget with several TopLevelItems and Childs of it. I'm trying to connect one item from QTreeWidget with a one page of QStackedWidget. I was searching it everywhere, but I didn't found this what I mean.
Please help me...
Greetings!

totem
12th October 2011, 14:00
You could associate a unique ID to every QTreeWidgetItem, and when the current item changes in the tree you signal anyone interested that the corresponding ID has been activated.
Then, your QStackedWidget shows the correct widget accordingly.

Kill3rReaper
13th October 2011, 01:19
But how can I do this quickly? I have to manually create new elements to QTreeWidget, then insert the ID number to it? There is no other way out?

totem
13th October 2011, 08:59
this is done in like 3 mins...



MyTreeWidget::MyTreeWidget(...)
{
// ...
connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWi dgetItem*)), SLOT(currentItemChanged(QTreeWidgetItem*)))
// ...
}
void MyTreeWidget::populate(...)
{
// ...
QTreeWidgetItem *item = new QTreeWidgetItem("tree_item") ;
item->setUserData(0,Qt::UserRole, PAGE_ID) ; // unique ID HERE
addTopLevelItem(item) ;
// ...
}
void MyTreeWidget::currentItemChanged(QTreeWidgetItem * current)
{
int id = item->data(0,Qt::UserRole).toInt() ;
emit currentIDChanged(id) ;
}





MyWindow::MyWindow(...)
{
// MyTreeWidget *p_treeWidget
// QStackedWidget *p_stackedWidget

// ...

connect(p_treeWidget, SIGNAL(currentIDChanged(int)), p_stackedWidget, SLOT(setCurrentIndex(int))) ;

// ...
}

arturo182
23rd October 2011, 22:52
It's not the best solution, but I think it's the fastest: create a hidden column in which you store index of QStackedWiget's page that is associated with the item. This way you can do this in 1 line (in tree's currentItemChanged slot):

ui->stackedWidget->setCurrentIndex(current->text(1).toInt());

Kill3rReaper
23rd October 2011, 23:03
IMHO it's the fastest and easiest way to do this - thank you! :)