Results 1 to 20 of 42

Thread: QTreeWidget clicked signal

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #30
    Join Date
    Oct 2009
    Posts
    35
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11
    Thanks
    12

    Smile Re: QTreeWidget clicked signal

    Quote Originally Posted by faldżip View Post
    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.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. Widget(QWidget *parent = 0) : QWidget(parent) {
    8. treeWidget = new QTreeWidget(this);
    9. tabWidget = new QTabWidget(this);
    10. tabWidget->setTabsClosable(true);
    11. la->addWidget(treeWidget);
    12. la->addWidget(tabWidget);
    13. setLayout(la);
    14.  
    15. for (int i = 0; i < 2; ++i) {
    16. QTreeWidgetItem *p = new QTreeWidgetItem(treeWidget, QStringList() << QString("parent item %1").arg(i));
    17. for (int j = 0; j < 5; ++j) {
    18. QTreeWidgetItem *c = new QTreeWidgetItem(QStringList() << QString("child item %1%2").arg(i).arg(j));
    19. p->addChild(c);
    20. }
    21. }
    22. connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(slotItemClicked(QTreeWidgetItem*)));
    23. connect(tabWidget, SIGNAL(tabCloseRequested(int)), SLOT(slotCloseTab(int)));
    24. }
    25. private slots:
    26. void slotItemClicked(QTreeWidgetItem *item) {
    27. QWidget *w = itemToWidget.value(item, 0);
    28. if (w) {
    29. tabWidget->setCurrentWidget(w);
    30. return;
    31. }
    32. QLabel *label = new QLabel(item->text(0));
    33. itemToWidget.insert(item, label);
    34. widgetToItem.insert(label, item);
    35. label->setAlignment(Qt::AlignCenter);
    36. tabWidget->insertTab(0, label, item->text(0));
    37. tabWidget->setCurrentIndex(0);
    38. }
    39. void slotCloseTab(int index) {
    40. QWidget *w = tabWidget->widget(index);
    41. itemToWidget.remove(widgetToItem.value(w));
    42. widgetToItem.remove(w);
    43. tabWidget->removeTab(index);
    44. }
    45. private:
    46. QTabWidget *tabWidget;
    47. QTreeWidget *treeWidget;
    48. QMap<QTreeWidgetItem *, QWidget *> itemToWidget;
    49. QMap<QWidget *, QTreeWidgetItem *> widgetToItem;
    50. };
    51.  
    52. int main(int argc, char **argv)
    53. {
    54. QApplication a(argc, argv);
    55. Widget w;
    56. w.show();
    57. return a.exec();
    58. }
    59. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Hi ,

    I really thanks ,its working for me ,thanks for the code...
    Last edited by mkkguru; 28th January 2010 at 10:24.

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
  •  
Qt is a trademark of The Qt Company.