QTreeWidget and QStackedWidget
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!
Re: QTreeWidget and QStackedWidget
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.
Re: QTreeWidget and QStackedWidget
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?
Re: QTreeWidget and QStackedWidget
this is done in like 3 mins...
Code:
MyTreeWidget::MyTreeWidget(...)
{
// ...
// ...
}
void MyTreeWidget::populate(...)
{
// ...
item->setUserData(0,Qt::UserRole, PAGE_ID) ; // unique ID HERE
addTopLevelItem(item) ;
// ...
}
{
int id = item->data(0,Qt::UserRole).toInt() ;
emit currentIDChanged(id) ;
}
Code:
MyWindow::MyWindow(...)
{
// MyTreeWidget *p_treeWidget
// QStackedWidget *p_stackedWidget
// ...
connect(p_treeWidget, SIGNAL(currentIDChanged(int)), p_stackedWidget, SLOT(setCurrentIndex(int))) ;
// ...
}
Re: QTreeWidget and QStackedWidget
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):
Code:
ui->stackedWidget->setCurrentIndex(current->text(1).toInt());
Re: QTreeWidget and QStackedWidget
IMHO it's the fastest and easiest way to do this - thank you! :)