PDA

View Full Version : Finding an specific item in QTreeWidget by its text



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.

ChrisW67
7th September 2011, 00:10
You put vr_data into column zero (line 31) but then go looking for it in column one (line 20)

The whole routine seems much more complicated than it needs to be.

BTW: Goto! Really!

Momergil
7th September 2011, 03:07
You put vr_data into column zero (line 31) but then go looking for it in column one (line 20)

The whole routine seems much more complicated than it needs to be.

BTW: Goto! Really!

Hello ChrisW67!

First, thanks for the answer; it worked.

Second, well, this is the unique routine that I could created. Do you have any idea of how to make the code better with the same functionality?

And third, about the Go to, that was a necessity from some time ago in previous tests because the while() command wasn't working for some unidentified reason. Now that you spoke it, I noticed that I can already change to while.

But in any case, what would be the problem with go to? :]


Thanks!


Momergil

ChrisW67
7th September 2011, 05:08
This:


#include <QtGui>
#include <QDebug>


int main(int argc, char *argv[])c++ singelton
{
QApplication app(argc, argv);

QTreeWidget tree;
tree.setColumnCount(3);

QFile data("test.txt");
if (data.open(QIODevice::ReadOnly)) {
QTextStream s(&data);
while (!s.atEnd()) {
QString leituraall = s.readLine();
QString vr_data = leituraall.section('\t',0,0);
QString vr_semana = leituraall.section('\t',1,1);
QString vr_hora = leituraall.section('\t',2,2);
QString vr_smalldescription = leituraall.section('\t',3,3);

// Look for the date in column zero
QTreeWidgetItem *group;
QList<QTreeWidgetItem*> items = tree.findItems(vr_data, Qt::MatchExactly, 0);
if (items.count() == 0) { // not found, create new entry
group = new QTreeWidgetItem(&tree);
group->setText(0, vr_data);
}
else
group = items.at(0);

// create the child entry
QTreeWidgetItem *child = new QTreeWidgetItem(group);
child->setText(0, vr_semana);
child->setText(1, vr_hora);
child->setText(2, vr_smalldescription);
}
}

tree.show();

return app.exec();
}

I have dispensed with the separate QStringList of dates already seen, the code that creates the child is not repeated, no goto. Or even this:


#include <QtGui>
#include <QDebug>


int main(int argc, char *argv[])
{
QApplication app(argc, argv);


QTreeWidget tree;
tree.setColumnCount(3);

QFile data("test.txt");
if (data.open(QIODevice::ReadOnly)) {
QTextStream s(&data);
while (!s.atEnd()) {
QStringList fields = s.readLine().split('\t');

// Look for the date in column zero
QTreeWidgetItem *group;
QList<QTreeWidgetItem*> items = tree.findItems(fields.at(0), Qt::MatchExactly, 0);
if (items.count() == 0) { // not found, create new entry
group = new QTreeWidgetItem(&tree, fields.mid(0,1));
}
else
group = items.at(0);

// create the child entry
QTreeWidgetItem *child = new QTreeWidgetItem(group, fields.mid(1));
}
}

tree.show();

return app.exec();
}

which dispenses with intermediate variables.

If the second column in the data file contains any values that are identical to values in the first column this will fail.

"goto" is not inherently evil. However, using "goto" in code has a long track record of producing unmaintainable spaghetti code because it allows the execution path to jump around arbitrarily (bowl of spaghetti). Using "goto" when there is a perfectly good, well defined alternative (while loop, break, continue, return) is not likely to win many friends.

Momergil
11th September 2011, 01:39
This:

[...]

which dispenses with intermediate variables.

If the second column in the data file contains any values that are identical to values in the first column this will fail.

"goto" is not inherently evil. However, using "goto" in code has a long track record of producing unmaintainable spaghetti code because it allows the execution path to jump around arbitrarily (bowl of spaghetti). Using "goto" when there is a perfectly good, well defined alternative (while loop, break, continue, return) is not likely to win many friends.

ChrisW67,

thanks a lot for taking your time to write those two codes. I will test them soon after I do some changes in the code.

Also thanks for the explanation about the use of goto. In case you have time, could you please help me with another "goto situation" in this post? --> http://www.qtcentre.org/threads/44529-Making-my-central-code-better?p=202750

Thanks!


Momergil