Momergil
6th September 2011, 13:44
Hello!
I'm having a work with QTreeWidget and I found a problem here in locating an item on it by one of its texts. The idea is the following:
I created a QTreeWidget and I decided to read from a txt file some columns and create items and sub-items in the QTreeWidget with the data collected in the txt. The idea is that in each line of the txt there are some parameters base on date values (02/12/2011, e.g.). The algorithm is supposed to do a research in the QTreeWidget each time a new line is read in the txt and see if there is already an item related to that date. If there is not, a new item is created with that new date and the rest of the line data is used to create a child on that item. By the other hand, if an item with that date already exists, than instead of creating a new item with that date and a child with the rest, the algorithm should create only the child under the already existing item.
But I'm having difficulties in creating a search mechanism to see if there is already an item related to that date. Everything else I already have. I tested the function QList<QTreeWidgetItem *> QTreeWidget::findItems ( const QString & text, Qt::MatchFlags flags, int column = 0 ) const (p.s.: where is the button for Qt functions and classes?), but its not working. By the moment, here is what I have:
void AlarmManagment::readExistingAlarms()
{
v_rewr->openRead2("./Config/Alarms.txt");
name: leituraall = v_rewr->Read();
vr_data = leituraall.section('\t',0,0);
vr_semana = leituraall.section('\t',1,1);
vr_hora = leituraall.section('\t',2,2);
vr_smalldescription = leituraall.section('\t',3,3);
if (!vector_String1.contains(vr_data))
{
vector_String1.push_back(vr_data);
addRoot(vr_data, vr_semana, vr_hora, vr_smalldescription);
qDebug() << "Item normal sendo colocado: " << vr_data;
}
else
{
a = ui->Listofalarms->findItems(vr_data,Qt::MatchExactly,1);
qDebug() << "item ja exite: " << vr_data;
addChild(a.front(),vr_semana,vr_hora,vr_smalldescr iption);
}
if (!v_rewr->attheEnd()) goto name;
}
void AlarmManagment::addRoot(QString data, QString semana, QString hora, QString smadesc)
{
QTreeWidgetItem *itm = new QTreeWidgetItem(ui->Listofalarms);
itm->setText(0,data);
QTreeWidgetItem *itm2 = new QTreeWidgetItem();
itm2->setText(0,semana);
itm2->setText(1,hora);
itm2->setText(2,smadesc);
itm->addChild(itm2);
}
void AlarmManagment::addChild(QTreeWidgetItem *parent, QString sem, QString hour, QString smaldescription)
{
QTreeWidgetItem *itm2 = new QTreeWidgetItem();
itm2->setText(0,sem);
itm2->setText(1,hour);
itm2->setText(2,smaldescription);
parent->addChild(itm2);
}
while:
QList<QTreeWidgetItem *> a;
I'm having problems in dealing with this QList of QTreeWidgetItem in terms of finding the item that has the date in question.
I'm having a work with QTreeWidget and I found a problem here in locating an item on it by one of its texts. The idea is the following:
I created a QTreeWidget and I decided to read from a txt file some columns and create items and sub-items in the QTreeWidget with the data collected in the txt. The idea is that in each line of the txt there are some parameters base on date values (02/12/2011, e.g.). The algorithm is supposed to do a research in the QTreeWidget each time a new line is read in the txt and see if there is already an item related to that date. If there is not, a new item is created with that new date and the rest of the line data is used to create a child on that item. By the other hand, if an item with that date already exists, than instead of creating a new item with that date and a child with the rest, the algorithm should create only the child under the already existing item.
But I'm having difficulties in creating a search mechanism to see if there is already an item related to that date. Everything else I already have. I tested the function QList<QTreeWidgetItem *> QTreeWidget::findItems ( const QString & text, Qt::MatchFlags flags, int column = 0 ) const (p.s.: where is the button for Qt functions and classes?), but its not working. By the moment, here is what I have:
void AlarmManagment::readExistingAlarms()
{
v_rewr->openRead2("./Config/Alarms.txt");
name: leituraall = v_rewr->Read();
vr_data = leituraall.section('\t',0,0);
vr_semana = leituraall.section('\t',1,1);
vr_hora = leituraall.section('\t',2,2);
vr_smalldescription = leituraall.section('\t',3,3);
if (!vector_String1.contains(vr_data))
{
vector_String1.push_back(vr_data);
addRoot(vr_data, vr_semana, vr_hora, vr_smalldescription);
qDebug() << "Item normal sendo colocado: " << vr_data;
}
else
{
a = ui->Listofalarms->findItems(vr_data,Qt::MatchExactly,1);
qDebug() << "item ja exite: " << vr_data;
addChild(a.front(),vr_semana,vr_hora,vr_smalldescr iption);
}
if (!v_rewr->attheEnd()) goto name;
}
void AlarmManagment::addRoot(QString data, QString semana, QString hora, QString smadesc)
{
QTreeWidgetItem *itm = new QTreeWidgetItem(ui->Listofalarms);
itm->setText(0,data);
QTreeWidgetItem *itm2 = new QTreeWidgetItem();
itm2->setText(0,semana);
itm2->setText(1,hora);
itm2->setText(2,smadesc);
itm->addChild(itm2);
}
void AlarmManagment::addChild(QTreeWidgetItem *parent, QString sem, QString hour, QString smaldescription)
{
QTreeWidgetItem *itm2 = new QTreeWidgetItem();
itm2->setText(0,sem);
itm2->setText(1,hour);
itm2->setText(2,smaldescription);
parent->addChild(itm2);
}
while:
QList<QTreeWidgetItem *> a;
I'm having problems in dealing with this QList of QTreeWidgetItem in terms of finding the item that has the date in question.