PDA

View Full Version : QTreeView -- only expand active (clicked) row



mardi
27th August 2012, 07:56
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.

8151

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

Regards,

Mardi

huilui
27th August 2012, 09:11
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)?

sonulohani
27th August 2012, 09:27
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


// rowGroups[3]->appendRow(rowItems[15]);

It is only crashing when you first click on the 0TH MODEL AND THEN AFTER CLICK ON the 3rd model.

mardi
27th August 2012, 09:53
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.


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

// rowGroups[3]->appendRow(rowItems[15]);

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?

mardi
29th August 2012, 06:56
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 (http://www.qtcentre.org/attachment.php?attachmentid=8151&d=1346046967)

here the detail of code..




//treeViewExpandCollapse.pro
HEADERS += \
form.h

SOURCES += \
form.cpp \
main.cpp

FORMS += \
form.ui




//form.h

#ifndef FORM_H
#define FORM_H

#include <QWidget>
#include <QModelIndex>

namespace Ui {
class Form;
}

class Form : public QWidget
{
Q_OBJECT

public:
explicit Form(QWidget *parent = 0);
~Form();

private:
Ui::Form *ui;

private slots:
void slotTreeClick(QModelIndex);
};

#endif // FORM_H




//form.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>240</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QTreeView" name="treeView">
<property name="geometry">
<rect>
<x>0</x>
<y>10</y>
<width>256</width>
<height>192</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>280</x>
<y>130</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>satu</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>




//main.cpp

#include <QApplication>
#include "form.h"

int main(int argc , char* argv[])
{
QApplication app(argc, argv);
Form test;
test.show();
return app.exec();
}




//form.cpp

#include "form.h"
#include "ui_form.h"
#include <QStandardItemModel>
#include <QDebug>

Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);

QStandardItemModel *standardModel = new QStandardItemModel ;

QStandardItem *rootNode = standardModel->invisibleRootItem();
QList<QStandardItem *> rowItems, rowGroups;

for (int h = 0; h < 4; h++)
{
rowGroups << new QStandardItem(QString::number(h));
rootNode->appendRow(rowGroups[h]);
}

rowItems << new QStandardItem("a1");
rowItems << new QStandardItem("a2");
rowItems << new QStandardItem("a3");
rowItems << new QStandardItem("a4");

rowItems << new QStandardItem("b1");
rowItems << new QStandardItem("b2");
rowItems << new QStandardItem("b2");
rowItems << new QStandardItem("b2");

rowItems << new QStandardItem("c1");
rowItems << new QStandardItem("c2");
rowItems << new QStandardItem("c4");
rowItems << new QStandardItem("c4");

rowItems << new QStandardItem("d1");
rowItems << new QStandardItem("d2");
rowItems << new QStandardItem("d3");
rowItems << new QStandardItem("d4");

rowGroups[0]->appendRow(rowItems[0]);
rowGroups[0]->appendRow(rowItems[1]);
rowGroups[0]->appendRow(rowItems[2]);
rowGroups[0]->appendRow(rowItems[3]);

rowGroups[1]->appendRow(rowItems[4]);
rowGroups[1]->appendRow(rowItems[5]);
//rowGroups[1]->appendRow(rowItems[6]);
//rowGroups[1]->appendRow(rowItems[7]);

rowGroups[2]->appendRow(rowItems[8]);
//rowGroups[2]->appendRow(rowItems[9]);
//rowGroups[2]->appendRow(rowItems[10]);
//rowGroups[2]->appendRow(rowItems[11]);

rowGroups[3]->appendRow(rowItems[12]);
rowGroups[3]->appendRow(rowItems[13]);
rowGroups[3]->appendRow(rowItems[14]);
//rowGroups[3]->appendRow(rowItems[15]);

//register the model
ui->treeView->setModel(standardModel);
//ui->treeWidget->setModel(standardModel);

connect(ui->treeView, SIGNAL(expanded(QModelIndex)), this, SLOT(slotTreeClick(QModelIndex)));
}

Form::~Form()
{
delete ui;
}

void Form::slotTreeClick(QModelIndex obj)
{
qDebug()<<"clicked " << obj.row()<<" -- " << obj.column();
//qDebug()<<obj.parent();
//qDebug()<<obj.child();

for (int i = 0; i < 4; i++)
{
qDebug() << "proces " << i;
if (i != obj.row())
{
qDebug() << "proces " << i <<" to collapse";
if (obj.sibling(i, 0).isValid())
ui->treeView->collapse(obj.sibling(i, 0));
//ui->treeView->collapse(obj.parent().child(i, 0));
}
}
qDebug()<<"finish slotTreeClick";
}


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

Regards,
Mardi

viulskiez
5th September 2012, 01:31
Try this, and see what's going on.



...
ui->setupUi(this);
ui->treeView->setUniformRowHeights(true); // try adding this line
...

mardi
5th September 2012, 10:41
Try this, and see what's going on.



...
ui->setupUi(this);
ui->treeView->setUniformRowHeights(true); // try adding this line
...


It is working..
thank you very much..

Regards,
Mardi