Results 1 to 6 of 6

Thread: QTreeWidget and QStackedWidget

  1. #1
    Join Date
    Jul 2009
    Location
    Poland, Łódź/Bełchatów
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Question 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!

  2. #2
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  3. #3
    Join Date
    Jul 2009
    Location
    Poland, Łódź/Bełchatów
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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?

  4. #4
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget and QStackedWidget

    this is done in like 3 mins...


    Qt Code:
    1. MyTreeWidget::MyTreeWidget(...)
    2. {
    3. // ...
    4. connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), SLOT(currentItemChanged(QTreeWidgetItem*)))
    5. // ...
    6. }
    7. void MyTreeWidget::populate(...)
    8. {
    9. // ...
    10. QTreeWidgetItem *item = new QTreeWidgetItem("tree_item") ;
    11. item->setUserData(0,Qt::UserRole, PAGE_ID) ; // unique ID HERE
    12. addTopLevelItem(item) ;
    13. // ...
    14. }
    15. void MyTreeWidget::currentItemChanged(QTreeWidgetItem * current)
    16. {
    17. int id = item->data(0,Qt::UserRole).toInt() ;
    18. emit currentIDChanged(id) ;
    19. }
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. MyWindow::MyWindow(...)
    2. {
    3. // MyTreeWidget *p_treeWidget
    4. // QStackedWidget *p_stackedWidget
    5.  
    6. // ...
    7.  
    8. connect(p_treeWidget, SIGNAL(currentIDChanged(int)), p_stackedWidget, SLOT(setCurrentIndex(int))) ;
    9.  
    10. // ...
    11. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to totem for this useful post:

    arturo182 (14th October 2011)

  6. #5
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default 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):
    Qt Code:
    1. ui->stackedWidget->setCurrentIndex(current->text(1).toInt());
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to arturo182 for this useful post:

    Kill3rReaper (23rd October 2011)

  8. #6
    Join Date
    Jul 2009
    Location
    Poland, Łódź/Bełchatów
    Posts
    16
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QTreeWidget and QStackedWidget

    IMHO it's the fastest and easiest way to do this - thank you!

Similar Threads

  1. QStackedLayout / QStackedWidget
    By morraine in forum Newbie
    Replies: 15
    Last Post: 12th July 2013, 10:16
  2. QStackedWidget
    By sattu in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2011, 14:44
  3. QTableWidget along with QStackedWidget
    By smiling in forum Qt Programming
    Replies: 5
    Last Post: 13th March 2010, 13:46
  4. Help with QStackedWidget
    By onírico in forum Newbie
    Replies: 6
    Last Post: 12th November 2009, 17:34
  5. QSplitter and QStackedWidget
    By nikita in forum Qt Programming
    Replies: 4
    Last Post: 15th November 2006, 05:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.