PDA

View Full Version : treewidget and a message system



alisami
16th July 2008, 08:29
hi,

i want to implement a message system where the message formats are stored in a treewidget. i have created the treewidget and the items with the designer. the structure is as the following:

message group1
- message type 11
- message type 12
- message type 13
message group2
- message type 21
- message type 22

when i choose a message from the widget and click `new message` i want to open a dialog box related to the message type.

my problem is that, in the designer, i cannot set an object name for te treewidgetitem's and the only way i have seen is to compare the treewidgetitem's name when the message type is selected and open the dialog box.

is there any other way to identify the items selected in the treewidget ?

sami

mchara
16th July 2008, 08:56
Hi,
i would do it this way:
create items programatically by

QTreeWidgetItem *item = new QTreeWidgetItem();
treeWidget->addTopLevelItem (item);

and adde them to some additional map

QMap<QTreeWidgetItem *, msgType> map;
map[item] = type;

alisami
16th July 2008, 09:03
if i create the items without using the designer, it would be a problem for me, because there are lots of messages and there will be a lot of change in the message list. i don't want to edit the code manually everytime -if possible-.

mchara
16th July 2008, 09:15
I'm not sure if i understood your problem.
You want to associate treeWidgetItems with message types without coding?

alisami
16th July 2008, 09:25
Yes, or any suggestion to carry out the work with another easy way.

As you may know in windows forms, the tree nodes can be given an object name so that the object can be identified. Is there such a feature for the Qt designer.

If that would not be possible, sure, I will construct the tree by coding.

mchara
16th July 2008, 11:24
Only QObjects(objects of classes that inherits QObject) can be named by setObjectName.

QTreeWidgetItem is not a QObject, so

i think you'll have to code it.

If you don't want to maintain additional code for it, consider reading massages types from kind of text file or xml.

jpn
27th July 2008, 16:14
Notice that QTreeWidgetItems have a built-in support for "types (http://doc.trolltech.com/4.4/qtreewidgetitem.html#type)". Alternatively you can use QTreeWidgetItem::setData() with Qt::UserRole to store arbitrary identifier data to each item.

wysota
27th July 2008, 18:05
my problem is that, in the designer, i cannot set an object name for te treewidgetitem's and the only way i have seen is to compare the treewidgetitem's name when the message type is selected and open the dialog box.

is there any other way to identify the items selected in the treewidget ?

Each item has an index associated with it, namely a row number, column number and the index of a parent. For instance message type 13 would have an index of "row 2, parent with row 0" or in code:


QModelIndex index = tree->currentIndex();
int row = index.row();
int parentrow = index.parent().row();
if(index.parent().isValid()){
switch(parentrow){
case 0: if(row==0) handle_type_11(); else if(row==1) handle_type_12(); else ...
...
}
} else {
// group clicked
switch(row){
case 0: handle_group_1(); break;
case 1: handle_group_2(); break;
//...
}
}