This:
#include <QtGui>
#include <QDebug>
int main(int argc, char *argv[])c++ singelton
{
tree.setColumnCount(3);
while (!s.atEnd()) {
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
QList<QTreeWidgetItem*> items = tree.findItems(vr_data, Qt::MatchExactly, 0);
if (items.count() == 0) { // not found, create new entry
group->setText(0, vr_data);
}
else
group = items.at(0);
// create the child entry
child->setText(0, vr_semana);
child->setText(1, vr_hora);
child->setText(2, vr_smalldescription);
}
}
tree.show();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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[])
{
tree.setColumnCount(3);
while (!s.atEnd()) {
// Look for the date in column zero
QList<QTreeWidgetItem*> items = tree.findItems(fields.at(0), Qt::MatchExactly, 0);
if (items.count() == 0) { // not found, create new entry
}
else
group = items.at(0);
// create the child entry
}
}
tree.show();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks