PDA

View Full Version : Issues with QTreeWidget



m3d
11th March 2010, 20:34
Hi everyone

I am working withr Qt 4.6 (which seems quite buggy), and I would like to know if these errors comes from me, or qt

I would like a qtreewidget, made of children, each children having children. So the design is:

TREE
Children 1
Sub Children 1 Sub Children 2 Children 2



Here I have my first issue. I successively tried:



QTreeWidget* tree = new QTreeWidget(parentwidget); //tree creation
tree->setColumnCount(2); //not the point here but I need it

QTreeWidgetItem* main = new QTreeWidgetItem(tree); //1st level item creation
QTreeWidgetItem* child= new QTreeWidgetItem(main); //2d level item creation, with 'main' as parent
child->setHidden(false); //just to be sure


and



QTreeWidget* tree = new QTreeWidget(parentwidget);
tree->setColumnCount(2);

QTreeWidgetItem* main = new QTreeWidgetItem(tree);
QTreeWidgetItem* child= new QTreeWidgetItem(); //2d level creation, without parent
main -> addChild(enfant); //link child->parent
child->setHidden(false);


In both cases, I can't see the items but the 1st leveled one.
However with:



QTreeWidget* tree = new QTreeWidget(parentwidget);
tree->setColumnCount(2);

QTreeWidgetItem* main = new QTreeWidgetItem(tree);
QTreeWidgetItem* child= new QTreeWidgetItem(tree); //child is created as 1st level item
child->setHidden(false);


Of course all items are 1st-level items now (which is not what I want), but at least they all are here!

So FIRST QUESTION:
What is wrong with my ways of including children to the 1st level item?
__________________________________________________ __________________________________________________ ________

Then, I am enjoying my all-1st-leveled items. They all are with the design:
[] Name_of_item,
[] being a checkbox.

Then I do:


QTreeWidgetItem* item = new QTreeWidgetItem(tree);
item ->setCheckState(0,Qt::Checked); //to create the column 0 checkbox
item->setText(1,tr("Name")); //to label the ligne

//blablabla do the same for other items

connect( tree , SIGNAL( itemClicked( QTreeWidgetItem*,int ) ), this, SLOT ( updateitem ( QTreeWidgetItem*,int ) ) );


So there, each item looks exactly as I want it to (which is greeeaaaaat). Let's click the first checkbox! Nothing. The second? Nothing. I am a little bit naive ; "Hum, my slot is buggy... Let's add some breakpoints..." The first one I had is right at the first line of the slot function, so if the slot function is read, the breakpoint is hit. Let's run it again. clic. clic. CLIC. CLIC. "SO why are you not hitting my breakpoint?!!!"

So there is my second question:
Why this tree is not clever enough to understand that when I clic on it, I want the itemClicked signal to be emitted?


Is there a way to solve these issues without having to use an older version of qt? I used qtreewidgets just like that with 4.3 and it worked, so what?!

Thanks,
M.

Lykurg
11th March 2010, 22:21
To 1)
ui->treeWidget->setColumnCount(2);
QTreeWidgetItem* main = new QTreeWidgetItem(); //1st level item creation
main->setCheckState(0, Qt::Checked);
main->setText(1, "main");
QTreeWidgetItem* child= new QTreeWidgetItem(); //2d level item creation, with 'main' as parent
child->setText(1,"child");
main->addChild(child);
ui->treeWidget->addTopLevelItem(main);

Works, and a connection with your provided statement also works well on my side. Is the connection really established?

m3d
11th March 2010, 22:50
I'm with 4.6.2, so maybe it is the issue...

I am in a QMainWindow, and try to build this tree in the dockwindow.
If I try to build it in and set it as the central widget, then there is no difference but I can highlight the main line while clicking it

If I build this widget in the dockwindow of a QMainWindow, I have the same issue.



Whoops spoke too fast. It is true there is this tiny little arrow on the left side, I can't click on.
BUT if I set:
main -> setExpanded(true);

NOW I CAN SEE EVERYTHING :)

Just a tricky case however :s

So Actually my first issue was also my second: the connectivity is not right :(

m3d
12th March 2010, 16:03
Actually setExpanded shows ALL children, included the hidden ones... which is not what I want! :(
Someone please help :P

m3d
12th March 2010, 16:17
With all these visible hidden children I can see a funny stuff:
Parent item and its 8 first children are not accessible, but the others are.

m3d
12th March 2010, 17:17
For the accessible ones, the signal is emitted but the qtreewidgetItem* sent to the slot is NULL.
Any Idea?

m3d
12th March 2010, 21:33
Well, I guess with all these updates it was not clear anymore...



my object is a QMainWindow, and in its constructor I call this function:




//creation of the dockwidget and its components
QDockWidget* dockWidget = new QDockWidget(tr("Name"),this);
QTreeWidget* tree = createTree(dockWidget);
QWidget* otherwidget = createOtherWidget(dockWidget);

//creation of the design
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(tree);
layout->addWidget(otherwidget);
dockWidget->setLayout(layout);

//finishing
addDockWidget(Qt::RightDockWidgetArea,dockWidget);




the createtree(DockWidget*) function is:




QTreeWidget* MyMainWindow::createTree(QWidget* parent){

//creation of the tree and the main item
QTreeWidget* tree = new QTreeWidget(parent);
tree->setColumnCount(2);
QTreeWidgetItem* main = new QTreeWidgetItem(tree);
main-> setCheckState(0,Qt::Checked);
main-> setText(1,tr("Gulf"));
main -> setExpanded(true);

//loading the children
for( QList<MyObject>::iterator itr(_myList.begin()); itr!=_myList.end(); ++itr){

QTreeWidgetItem* child = new QTreeWidgetItem();
child->setCheckState(0,Qt::Checked);
child->setText(1,itr->name());
parent->addChild(child);
child->setHidden(itr->ishidden());

}//end for

//do the connect

connect( tree, SIGNAL( itemClicked( QTreeWidgetItem*, int ) ),
this, SLOT( updateChild( QTreeWidgetItem*, int ) )
);



The issues I have are:

I can't click on the parent item nor its 8 first children
When I click on a 9-n child, the QTreeWidgetItem* which is loaded at the fist line of my slot is not correct. It as an address, but when I go to 'values' -> [2] -> error(0),error(0). Then my slot is not efficient since I want for instance unchecked the (checked) item but its state is unchecked because of this 'error' field...



Here is what this tree looks like:
http://img697.imageshack.us/img697/6682/treeh.png

I can highlight (and click on) items Child 18, 19 and 20 (and other if there were), but not the previous ones.
If I click on it, an invalid item is sent to the slot:

http://img690.imageshack.us/img690/636/itemv.png

m3d
15th March 2010, 14:14
up? it has been buried deep during the week-end....

m3d
15th March 2010, 21:41
I declared dockWidget and treeWidget in the .h, I added dockWidgetContents (and the setContentsMargins but I doubt this changes anything). I also used static_cast<Qt::DockWidgetArea>(2) instead of Qt::RightDockWidgetArea.

The result is nicely surprising, and I now can access all the items of my tree! Another problem solved.

However, the second issue is still open:
When I click on one of the items, I enter the slot and have an invalid QTreeWidgetItem* that is returned (see last image posted)...