Results 1 to 7 of 7

Thread: QTreeView -- only expand active (clicked) row

  1. #1
    Join Date
    Apr 2012
    Location
    Bali - Indonesia
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTreeView -- only expand active (clicked) row

    Dear,

    Is it possible to make QTreeView only expand the active row? which mean, when I click another row than all the row except one (that I expanded) will be collapse.

    I just try to make a sample application with a signal from QTreeView, where it will call a slot when it is expand. The slot function will collapse all row except the row that we expanded.
    But my code come with error when it is running. For detail, I atttach my code.

    treeViewExpandCollapse.zip

    please, can anyone help me?
    all suggestion will be appreciate

    Regards,

    Mardi

  2. #2

    Default Re: QTreeView -- only expand active (clicked) row

    Hi mardi,

    Well, your code is compiling and running just find on my machine here... Could you be a little more specific about what errors occur when you're running it (e.g. output)?

  3. #3
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView -- only expand active (clicked) row

    Hey mardi,
    I studied your code and got one fault i.e. in your rowGroup[3] (i.e. 3rd model) it is showing 3 rows in the debug output. So, uncomment this
    Qt Code:
    1. // rowGroups[3]->appendRow(rowItems[15]);
    To copy to clipboard, switch view to plain text mode 
    It is only crashing when you first click on the 0TH MODEL AND THEN AFTER CLICK ON the 3rd model.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  4. #4
    Join Date
    Apr 2012
    Location
    Bali - Indonesia
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView -- only expand active (clicked) row

    Quote Originally Posted by huilui View Post
    Hi mardi,

    Well, your code is compiling and running just find on my machine here... Could you be a little more specific about what errors occur when you're running it (e.g. output)?
    Hi huilui. The error like sonulohani said.
    Quote Originally Posted by sonulohani
    It is only crashing when you first click on the 0TH MODEL AND THEN AFTER CLICK ON the 3rd model.
    Hi Sonulohani. by uncomment this code
    Qt Code:
    1. // rowGroups[3]->appendRow(rowItems[15]);
    To copy to clipboard, switch view to plain text mode 

    I could not understand the logic. by remove this code it's mean there is a limited to using QTreeView on especially for adding child.

    did anyone had different explanation?
    Last edited by mardi; 27th August 2012 at 10:10.

  5. #5
    Join Date
    Apr 2012
    Location
    Bali - Indonesia
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView -- only expand active (clicked) row

    Dear,

    Because I still not get a proper solution, might be most of user forum is easy analyze through code directlry. So I'll try to display the code in here.

    source code could grab from here

    here the detail of code..


    Qt Code:
    1. //treeViewExpandCollapse.pro
    2. HEADERS += \
    3. form.h
    4.  
    5. SOURCES += \
    6. form.cpp \
    7. main.cpp
    8.  
    9. FORMS += \
    10. form.ui
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //form.h
    2.  
    3. #ifndef FORM_H
    4. #define FORM_H
    5.  
    6. #include <QWidget>
    7. #include <QModelIndex>
    8.  
    9. namespace Ui {
    10. class Form;
    11. }
    12.  
    13. class Form : public QWidget
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit Form(QWidget *parent = 0);
    19. ~Form();
    20.  
    21. private:
    22. Ui::Form *ui;
    23.  
    24. private slots:
    25. void slotTreeClick(QModelIndex);
    26. };
    27.  
    28. #endif // FORM_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //form.ui
    2.  
    3. <?xml version="1.0" encoding="UTF-8"?>
    4. <ui version="4.0">
    5. <class>Form</class>
    6. <widget class="QWidget" name="Form">
    7. <property name="geometry">
    8. <rect>
    9. <x>0</x>
    10. <y>0</y>
    11. <width>400</width>
    12. <height>240</height>
    13. </rect>
    14. </property>
    15. <property name="windowTitle">
    16. <string>Form</string>
    17. </property>
    18. <widget class="QTreeView" name="treeView">
    19. <property name="geometry">
    20. <rect>
    21. <x>0</x>
    22. <y>10</y>
    23. <width>256</width>
    24. <height>192</height>
    25. </rect>
    26. </property>
    27. </widget>
    28. <widget class="QPushButton" name="pushButton">
    29. <property name="geometry">
    30. <rect>
    31. <x>280</x>
    32. <y>130</y>
    33. <width>75</width>
    34. <height>23</height>
    35. </rect>
    36. </property>
    37. <property name="text">
    38. <string>satu</string>
    39. </property>
    40. </widget>
    41. </widget>
    42. <resources/>
    43. <connections/>
    44. </ui>
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //main.cpp
    2.  
    3. #include <QApplication>
    4. #include "form.h"
    5.  
    6. int main(int argc , char* argv[])
    7. {
    8. QApplication app(argc, argv);
    9. Form test;
    10. test.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //form.cpp
    2.  
    3. #include "form.h"
    4. #include "ui_form.h"
    5. #include <QStandardItemModel>
    6. #include <QDebug>
    7.  
    8. Form::Form(QWidget *parent) :
    9. QWidget(parent),
    10. ui(new Ui::Form)
    11. {
    12. ui->setupUi(this);
    13.  
    14. QStandardItemModel *standardModel = new QStandardItemModel ;
    15.  
    16. QStandardItem *rootNode = standardModel->invisibleRootItem();
    17. QList<QStandardItem *> rowItems, rowGroups;
    18.  
    19. for (int h = 0; h < 4; h++)
    20. {
    21. rowGroups << new QStandardItem(QString::number(h));
    22. rootNode->appendRow(rowGroups[h]);
    23. }
    24.  
    25. rowItems << new QStandardItem("a1");
    26. rowItems << new QStandardItem("a2");
    27. rowItems << new QStandardItem("a3");
    28. rowItems << new QStandardItem("a4");
    29.  
    30. rowItems << new QStandardItem("b1");
    31. rowItems << new QStandardItem("b2");
    32. rowItems << new QStandardItem("b2");
    33. rowItems << new QStandardItem("b2");
    34.  
    35. rowItems << new QStandardItem("c1");
    36. rowItems << new QStandardItem("c2");
    37. rowItems << new QStandardItem("c4");
    38. rowItems << new QStandardItem("c4");
    39.  
    40. rowItems << new QStandardItem("d1");
    41. rowItems << new QStandardItem("d2");
    42. rowItems << new QStandardItem("d3");
    43. rowItems << new QStandardItem("d4");
    44.  
    45. rowGroups[0]->appendRow(rowItems[0]);
    46. rowGroups[0]->appendRow(rowItems[1]);
    47. rowGroups[0]->appendRow(rowItems[2]);
    48. rowGroups[0]->appendRow(rowItems[3]);
    49.  
    50. rowGroups[1]->appendRow(rowItems[4]);
    51. rowGroups[1]->appendRow(rowItems[5]);
    52. //rowGroups[1]->appendRow(rowItems[6]);
    53. //rowGroups[1]->appendRow(rowItems[7]);
    54.  
    55. rowGroups[2]->appendRow(rowItems[8]);
    56. //rowGroups[2]->appendRow(rowItems[9]);
    57. //rowGroups[2]->appendRow(rowItems[10]);
    58. //rowGroups[2]->appendRow(rowItems[11]);
    59.  
    60. rowGroups[3]->appendRow(rowItems[12]);
    61. rowGroups[3]->appendRow(rowItems[13]);
    62. rowGroups[3]->appendRow(rowItems[14]);
    63. //rowGroups[3]->appendRow(rowItems[15]);
    64.  
    65. //register the model
    66. ui->treeView->setModel(standardModel);
    67. //ui->treeWidget->setModel(standardModel);
    68.  
    69. connect(ui->treeView, SIGNAL(expanded(QModelIndex)), this, SLOT(slotTreeClick(QModelIndex)));
    70. }
    71.  
    72. Form::~Form()
    73. {
    74. delete ui;
    75. }
    76.  
    77. void Form::slotTreeClick(QModelIndex obj)
    78. {
    79. qDebug()<<"clicked " << obj.row()<<" -- " << obj.column();
    80. //qDebug()<<obj.parent();
    81. //qDebug()<<obj.child();
    82.  
    83. for (int i = 0; i < 4; i++)
    84. {
    85. qDebug() << "proces " << i;
    86. if (i != obj.row())
    87. {
    88. qDebug() << "proces " << i <<" to collapse";
    89. if (obj.sibling(i, 0).isValid())
    90. ui->treeView->collapse(obj.sibling(i, 0));
    91. //ui->treeView->collapse(obj.parent().child(i, 0));
    92. }
    93. }
    94. qDebug()<<"finish slotTreeClick";
    95. }
    To copy to clipboard, switch view to plain text mode 

    issue to duplicate : first click 0th model (1st row tree) and then click 3rd model (4th row tree)

    Regards,
    Mardi

  6. #6
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QTreeView -- only expand active (clicked) row

    Try this, and see what's going on.

    Qt Code:
    1. ...
    2. ui->setupUi(this);
    3. ui->treeView->setUniformRowHeights(true); // try adding this line
    4. ...
    To copy to clipboard, switch view to plain text mode 
    ~ We are nothing in this universe ~

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

    mardi (5th September 2012)

  8. #7
    Join Date
    Apr 2012
    Location
    Bali - Indonesia
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [Solved] QTreeView -- only expand active (clicked) row

    Quote Originally Posted by viulskiez View Post
    Try this, and see what's going on.

    Qt Code:
    1. ...
    2. ui->setupUi(this);
    3. ui->treeView->setUniformRowHeights(true); // try adding this line
    4. ...
    To copy to clipboard, switch view to plain text mode 
    It is working..
    thank you very much..

    Regards,
    Mardi
    Last edited by mardi; 5th September 2012 at 09:58.

Similar Threads

  1. Simple way to expand all nodes on a QTreeView?
    By cboles in forum Qt Programming
    Replies: 10
    Last Post: 12th April 2014, 16:54
  2. Replies: 0
    Last Post: 26th June 2012, 04:07
  3. QTreeView expand all columns in current row
    By Kicer in forum Qt Programming
    Replies: 0
    Last Post: 9th May 2010, 06:25
  4. preserve collapse/expand of a QTreeView
    By yazwas in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2009, 09:49
  5. QTreeView expand signals
    By Vidar Bøe in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2008, 09:47

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.