PDA

View Full Version : Bug in QStringList custom sort



giblit
29th March 2013, 01:30
QStringList listNames;
QStringList list;
QStringList tempList;
listNames << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
list << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec";
int startSize = list.size();
int num;
for(int i = 0; i<startSize; i++){
for(int j = 0; j<listNames.size(); j++){
if(list.contains(listNames[j],Qt::CaseInsensitive)){
num = list.indexOf(listNames[j]);
//cout << num << endl;
}
}
tempList << list[num];
list.removeAt(num);
}
for(int i = 0; i<startSize; i++){
list << tempList[tempList.size()-1];
tempList.removeLast();
}


for(int i = 0; i<list.size(); i++){
cout << list[i].toStdString() << endl;
}
in this example the user input would be the QStringList list and the order they are being sorted is QStringList listNames
it works perctly if the user inputs the same exact as the sorting order.

but what I would like to do is if the user inputs "jan" it will know it is the same as "Jan" I can check to see if it is the same by using contains which will yeah find it...but
when I do the index of it I will always get -1 because I can not think of a way to do
indexOf(QString, Qt::CaseInsensitive) is this even possible?

EDIT: well I can think of one way but I would prefer not to do it.
it would be to use QString::toUpper or QString::toLower for both of the QStringLists and it wouldnt matter what they input.
but the problem with that is.....I want the end result to be first letter Uppercase rest of the letters Lowercase

wysota
29th March 2013, 01:51
So is your question about sorting or searching?

giblit
29th March 2013, 02:09
kinda both I made it so I can sort and it works fine if A = A or a = a and not if A = a or a = A which is what I am trying to do
I figured how to make all the them A = A no matter what they input by doing
list[i] = list[i].toLower(); list[i][0] = list[i][0].toTitleCase();
but then the app crashes and never runs and I narrowed it down to this line.
list.removeAt(num) I didnt change anything besides in the list I made a few things upper and a few lower case but then when I couted i got the same exact for the list and listNames but doesnt work and If i change the list back to normal and comment out the list = list things it works fine but that shouldnt even effect it because I am setting the old one to a new one that is the same excact as the old. any suggestions?

this WORKS but only if Jan = Jan and not jan = Jan or jAn = Jan

QStringList listNames;
QStringList list;
QStringList tempList;
//listNames << "January" << "February" << "March" << "April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
listNames << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
list << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec";
int startSize = list.size();
int num;

for(int i = 0; i<startSize; i++){
//list[i] = list[i].toLower();
//list[i][0] = list[i][0].toTitleCase();
//cout << list[i].toStdString() << endl;
for(int j = 0; j<listNames.size(); j++){
if(list.contains(listNames[j],Qt::CaseInsensitive)){
num = list.indexOf(listNames[j]);
//cout << num << endl;
//cout << "LIST AT NUM: " << list.at(num).toStdString() << endl;

}
}
//cout << list[i].toStdString() << flush;
//cout << tempList[i].toStdString() << flush;
tempList << list[num];
list.removeAt(num);
}
for(int i = 0; i<startSize; i++){
list << tempList[tempList.size()-1];
tempList.removeLast();
}


for(int i = 0; i<list.size(); i++){
cout << list[i].toStdString() << endl;
}

and this does not work and I have no idea why(well it is something to do with the list[i][0] = list[i][0] and the removeAt(num) lines):

QStringList listNames;
QStringList list;
QStringList tempList;
//listNames << "January" << "February" << "March" << "April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
listNames << "JaN" << "FeB" << "mAr" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
list << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec";
int startSize = list.size();
int num;

for(int i = 0; i<startSize; i++){
list[i] = list[i].toLower();
list[i][0] = list[i][0].toTitleCase();
//cout << list[i].toStdString() << endl;
for(int j = 0; j<listNames.size(); j++){
if(list.contains(listNames[j],Qt::CaseInsensitive)){
num = list.indexOf(listNames[j]);
//cout << num << endl;
//cout << "LIST AT NUM: " << list.at(num).toStdString() << endl;

}
}
//cout << list[i].toStdString() << flush;
//cout << tempList[i].toStdString() << flush;
tempList << list[num];
list.removeAt(num);
}
for(int i = 0; i<startSize; i++){
list << tempList[tempList.size()-1];
tempList.removeLast();
}


for(int i = 0; i<list.size(); i++){
cout << list[i].toStdString() << endl;
}

Added after 6 minutes:

okay when I closed qt and reopened it, it worked I have no idea why but it works for me now
thanks anyways though wysota

wysota
29th March 2013, 07:44
Your code is extremely inefficient. It's complexity is O(n^4) (n*n*n*n where each 'n' comes from a for loop, a contains call or a indexOf call) while it can be much faster and simpler. Optimally sorting a list is O(n * lgn) and searching in such a sorted list is O(lgn) which can be both achieved using qSort (or std::sort) and qLowerBound() and passing a custom comparator that will ignore the case of compared strings. Alternatively you can just use a map where the key is lower case string and the value is a list of proper case (or title case or whatever you want) strings. Then it's just a matter of iterating through the map (which is sorted by key) or asking it to retrieve values for the key you want.

giblit
30th March 2013, 02:41
how would I sort by using a QMap I couldnt figure out how to use Qmaps even after reading the class references
I can figure out this
QMap<QString, int> map;
map["January"] = 1;
map["December"] = 12;

but after that I am a bit confused on how I would sort that and that would only be for the inital values not for the inputed values which is what I am trying to sort.

wysota
30th March 2013, 10:35
You don't have to sort it, it's already sorted by key name. Just use whatever you want to sort by for keys and real data for values.

giblit
30th March 2013, 23:02
okay cool thanks i understand how to use the QMap now, the first value is the key name and the second item is the key value so I just just use this:

QStringList listnames;
QStringList list;
listNames << "January" << "Jan" << "1" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
//user inputs something list << "INPUT";
for(int i = 0; i<list.size(); i++){
list[i] = list[i].toLower();
list[i][0] = list[i][0].toTitleCase();
}
QMap<QString,int> map;
for(int i = 0; i<listNames.size(); i++){
map[listNames[i]] = i;
}

thanks for the help wysota =]

wysota
30th March 2013, 23:24
That's still superflous, you don't need this first for loop. And I have no idea why you're putting integers as values for the map. If you do that, you get exactly the same output as if you were using a list (because the index of the list is equal to the value you put into the map). I suggest you first try to determine what you want to achieve and how you want to achieve it, then understand why it works the way it works (or not) and only then start implementing your solution. Programming is not rocket science but you can't blindly manipulate code hoping something that works for you comes out of it.

giblit
30th March 2013, 23:44
I have two lists, the first is the correct order which is why I am setting them to that position in the QMap, and the second list is the user input list which could be in any order which I am setting to the same order as the first list which is also why I have the first loop that is making sure they are all in the same format (first letter capital second letter small)
and it works for me I even couted it =p
maybe you are not understanding what I did.
here is the full code that seems to work

QStringList listNames;
QStringList list;
listNames << "January" << "Jan" << "01" << "1" << "February" << "Feb" << "02" << "2" << "March" << "Mar" << "03" << "3" << "April" << "Apr" << "04" << "4" << "May" << "05" << "5" << "June" << "Jun" << "06" << "6" << "July" << "Jul" << "07" << "7" << "August " << "Aug" << "08" << "8" << "September" << "Sep" << "09" << "9" << "October" << "Oct" << "10" << "Novemeber" << "Nov" << "11" << "Decemeber" << "Dec" << "12";
list << "JaN" << "MaR" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "oct" << "DEC" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "Jan" << "Mar" << "May" << "Jul" << "Sep" << "Nov" << "Feb" << "Apr" << "Jun" << "Aug" << "Oct" << "Dec" << "January" << "1";
for(int i = 0; i<list.size(); i++){
list[i] = list[i].toLower();
list[i][0] = list[i][0].toTitleCase();
}

cout << "BEFORE SORT: " << flush;
for(int i = 0; i<list.size();i++){
cout << list[i].toStdString() << flush;
}

QMap<QString,int> map;
for(int i = 0; i<listNames.size(); i++){
map[listNames[i]] = i;
}
QList<int> list2;
for(int i = 0; i<list.size(); i++){
//cout << list[i].toStdString() << ": " << flush;
//list[i] = map[list[i]];
//cout << map[list[i]] << endl;
list2 << map[list[i]];
qSort(list2.begin(),list2.end());
}
list.clear();
for(int j = 0; j<list2.size();j++){
//cout << list2[j] << endl;
list << map.key(list2[j]);
//cout << map.key(list2[j]).toStdString() << endl;
//cout << map[list2[j]].toStdString() << endl;
}
cout << endl << "AFTER SORT: " << flush;
for(int i = 0; i<list.size();i++){
cout << list[i].toStdString() << flush;
}

giblit
31st March 2013, 08:50
okay so I can sort by months, or days, or years...by using QMap with this code:

QStringList sortedMonth, inMonth;
sortedMonth << "January" << "February" << "March" << "April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
inMonth << "May" << "Dec" << "Jan" << "Sep";
for(int i = 0; i<inMonth.size(); i++){
if(inMonth.contains("Jan",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Jan"),"January");
} else if(inMonth.contains("Feb",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Feb"),"February");
} else if(inMonth.contains("Mar", Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Mar"),"March");
} else if(inMonth.contains("Apr",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Apr"),"April");
} else if(inMonth.contains("Jun",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Jun"),"June");
} else if(inMonth.contains("Jul",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Jul"),"July");
} else if(inMonth.contains("Aug",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Aug"),"August");
} else if(inMonth.contains("Sep",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Sep"),"September");
} else if(inMonth.contains("Oct",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Oct"),"October");
} else if(inMonth.contains("Nov",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Nov"),"November");
} else if(inMonth.contains("Dec",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Dec"),"December");
}
}
QMap<QString,int> map, map2, map3;
for(int i = 0; i<sortedMonth.size();i++){
map[sortedMonth[i]] = i;
}
QList<int> listorder, listorder2, listorder3;
for(int i = 0; i<inMonth.size(); i++){
listorder << map[inMonth[i]];
}
qSort(listorder.begin(),listorder.end());
inMonth.clear();
for(int i = 0; i<listorder.size();i++){
inMonth << map.key(listorder[i]);
}
for(int i = 0; i<inMonth.size();i++){
cout << inMonth[i].toStdString() << endl;
}
treeWidget = new QTreeWidget;
for(int i = 0; i<inMonth.size(); i++){
item[i] = new QTreeWidgetItem(treeWidget);
item[i]->setText(0, inMonth[i]);
treeWidget->insertTopLevelItem(i, item[i]);
}
treeWidget->setColumnCount(1);
setCentralWidget(treeWidget);


my problem though is say I have the date January 1 and January 2 I want january 1 to come before january 2
and the only way I can do it is if I have the same size sortedDays as the SortedMonths so that the indexes match when I map, but....if I do it dynamically which I want to..I wont always know the days and if I put in days 1-31 for the sorted then the index will not match and it will just put the numbers in order instead of for the coresponding date.

is there a way I could sort a QStringList that contains these QStrings January 3, December 21, March 28, January 25, October 15, December 7, March 7, October 28. so that it would sort months first, e.g.(Jan, Mar, Oct, Dec) then after doing that it would be Jan 3, Jan 25, March 28, March 7, Oct 15, Oct 28, Dec 21, Dec 7 . then After the first sort of months it would sort again based on days and have an end product of: Jan 3, Jan 25, March 7, March 28, Oct 15, Oct 28, Dec 7, Dec 21

sorry for taking so much time to do this and hogging up the forum =/

Added after 14 minutes:


okay so I can sort by months, or days, or years...by using QMap with this code:

QStringList sortedMonth, inMonth;
sortedMonth << "January" << "February" << "March" << "April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
inMonth << "May" << "Dec" << "Jan" << "Sep";
for(int i = 0; i<inMonth.size(); i++){
if(inMonth.contains("Jan",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Jan"),"January");
} else if(inMonth.contains("Feb",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Feb"),"February");
} else if(inMonth.contains("Mar", Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Mar"),"March");
} else if(inMonth.contains("Apr",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Apr"),"April");
} else if(inMonth.contains("Jun",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Jun"),"June");
} else if(inMonth.contains("Jul",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Jul"),"July");
} else if(inMonth.contains("Aug",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Aug"),"August");
} else if(inMonth.contains("Sep",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Sep"),"September");
} else if(inMonth.contains("Oct",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Oct"),"October");
} else if(inMonth.contains("Nov",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Nov"),"November");
} else if(inMonth.contains("Dec",Qt::CaseInsensitive)){
inMonth.replace(inMonth.indexOf("Dec"),"December");
}
}
QMap<QString,int> map, map2, map3;
for(int i = 0; i<sortedMonth.size();i++){
map[sortedMonth[i]] = i;
}
QList<int> listorder, listorder2, listorder3;
for(int i = 0; i<inMonth.size(); i++){
listorder << map[inMonth[i]];
}
qSort(listorder.begin(),listorder.end());
inMonth.clear();
for(int i = 0; i<listorder.size();i++){
inMonth << map.key(listorder[i]);
}
for(int i = 0; i<inMonth.size();i++){
cout << inMonth[i].toStdString() << endl;
}
treeWidget = new QTreeWidget;
for(int i = 0; i<inMonth.size(); i++){
item[i] = new QTreeWidgetItem(treeWidget);
item[i]->setText(0, inMonth[i]);
treeWidget->insertTopLevelItem(i, item[i]);
}
treeWidget->setColumnCount(1);
setCentralWidget(treeWidget);


my problem though is say I have the date January 1 and January 2 I want january 1 to come before january 2
and the only way I can do it is if I have the same size sortedDays as the SortedMonths so that the indexes match when I map, but....if I do it dynamically which I want to..I wont always know the days and if I put in days 1-31 for the sorted then the index will not match and it will just put the numbers in order instead of for the coresponding date.

is there a way I could sort a QStringList that contains these QStrings January 3, December 21, March 28, January 25, October 15, December 7, March 7, October 28. so that it would sort months first, e.g.(Jan, Mar, Oct, Dec) then after doing that it would be Jan 3, Jan 25, March 28, March 7, Oct 15, Oct 28, Dec 21, Dec 7 . then After the first sort of months it would sort again based on days and have an end product of: Jan 3, Jan 25, March 7, March 28, Oct 15, Oct 28, Dec 7, Dec 21

sorry for taking so much time to do this and hogging up the forum =/

argh nevermind I don't know what the hell I was thinking....all I had to do was convert the dates into numbers eg Jan 18 2010 and december 31 2009 would be: 2010118 and 20091231 then all I have to do is sort the numbers from least to greatest (eg the farthest back to most current) sorry to waste everyones time.

EDIT: got it working nicely =] thanks agian for the help earlier appreciate it.

QStringList months, dates;
QDate date[9];
months << "January" << "February" << "March" <<"April" << "May" << "June" << "July" << "August" << "September" << "October" << "November" << "December";
//format is MMddyyyy
dates << "January 01, 2010"<< "December 31, 2010"<< "November 03 2009" << "April 25, 2010"<< "January 03, 2011"<< "January 02, 2011" << "March/25/2013" << "09-21-2008" << "October/03-2012";
for(int i = 0; i<dates.size();i++){
dates[i].remove(" ");
dates[i].remove(",");
dates[i].remove("/");
dates[i].remove("-");
for(int j = 0;j<12;j++){
if(j<8){
dates[i].replace(months[j],QString("0%1").arg(j+1));
} else {
dates[i].replace(months[j],QString("%1").arg(j+1));
}
}
//cout << dates[i].toStdString() << endl;
dates[i].prepend(dates[i].right(4));
dates[i].remove(8,4);;
//cout << dates[i].toStdString() << endl;
}
QList<int> dateNum;
for(int i =0; i<dates.size();i++){
dateNum << dates[i].toInt();
}
dates.clear();
qSort(dateNum.begin(),dateNum.end());
for(int i = 0; i<dateNum.size();i++){
dates << QString("%1").arg(dateNum[i]);
dates[i].append(dates[i].left(4));
dates[i].remove(0,4);
date[i] = date[i].fromString(dates[i],"MMddyyyy");
}
for(int i = 0; i<dates.size();i++){
// cout << dateNum[i] << endl;
//cout << dates[i].toStdString() << endl;
cout << date[i].toString("MMMM dd, yyyy").toStdString() << endl;
}


the output from that is:

September 21, 2008
November 03, 2009
January 01, 2010
April 25, 2010
December 31, 2010
January 02, 2011
January 03, 2011
October 03, 2012
March 25, 2013

wysota
31st March 2013, 14:26
Maybe you should just sort dates and not strings?

giblit
31st March 2013, 23:57
I guess that would be a lot easier to use a DateEdit or Date/TimeEdit instead of using a QText edit never used it before but thats what is nice about Qt you can google the references for each item =] thats how I have been teaching myself qt the last few weeks. thanks again Wysota

here is the final code much shorter and easier to use =P


//header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDateEdit>
#include <QPushButton>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

private slots:
void buttonPressed();

private:
QDateEdit *dateEdit;
QPushButton *button;
QWidget *widget;
};

#endif // MAINWINDOW_H

//main c++
int dateCount = 0;
QList<QDate> date;
MainWindow::MainWindow(){
dateEdit = new QDateEdit;
button = new QPushButton;
widget = new QWidget;
button->setText("Ok");
QGridLayout *layout = new QGridLayout;
layout->addWidget(dateEdit,0,0);
layout->addWidget(button,0,1);
widget->setLayout(layout);
setCentralWidget(widget);
connect(button,SIGNAL(clicked()),
this,SLOT(buttonPressed()));
}

void MainWindow::buttonPressed(){
date << dateEdit->date();
qSort(date.begin(),date.end());
dateCount++;
for(int i = 0; i<dateCount;i++){
cout << date[i].toString("MMMM dd, yyyy").toStdString() << " " << flush;
}
}

thanks agian appreciate the ideas

giblit
2nd April 2013, 03:57
I have made it so when it sorts QTreeWidgetItem (column 0 text) it will also sort the corresponding columns text for that item here is the final code that seems to be working, didn't find any bugs in it (in case anyone else is having the same problem I was having.)


//header:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTreeWidget>
#include <QDateEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

private slots:
void buttonPressed();
void button2Pressed();
void sortTree();

private:
QTreeWidget *treeWidget;
QList<QTreeWidgetItem*> itemList;
QTreeWidgetItem *item;

QDateEdit *dateEdit;
QTimeEdit *timeEdit;
QPushButton *button, *button2, *sort;
QWidget *widget;
QGridLayout *layout;

};

#endif // MAINWINDOW_H


//main c++ file
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


//mainwindow c++ file
#include "mainwindow.h"
#include <iostream>
#include <QtGui>

using namespace std;
QList<QDate> sortedDate;
QList<QDate> regDate;
QList<QTime> time;
QMap<int, int> sortedOrder;
int timeCount = 0;
int dateCount = 0;
MainWindow::MainWindow()
{
layout = new QGridLayout;
dateEdit = new QDateEdit;
timeEdit = new QTimeEdit;
button = new QPushButton;
button2 = new QPushButton;
sort = new QPushButton;
widget = new QWidget;
button->setText("AddDate");
button2->setText("AddTime");
sort->setText("sort");
dateEdit->setDate(QDate::currentDate());
timeEdit->setTime(QTime::currentTime());
connect(button,SIGNAL(clicked()),
this,SLOT(buttonPressed()));
connect(button2,SIGNAL(clicked()),
this,SLOT(button2Pressed()));
connect(sort,SIGNAL(clicked()),
this, SLOT(sortTree()));

treeWidget = new QTreeWidget;
treeWidget->setColumnCount(2);
layout->addWidget(treeWidget,0,0);
layout->addWidget(dateEdit,1,0);
layout->addWidget(timeEdit,2,0);
layout->addWidget(button,1,1);
layout->addWidget(button2,2,1);
layout->addWidget(sort, 0, 1,Qt::AlignBottom);
widget->setLayout(layout);
setCentralWidget(widget);

}
void MainWindow::buttonPressed(){
regDate << dateEdit->date();
sortedDate << regDate[dateCount];
item = new QTreeWidgetItem(treeWidget);
itemList << item;
itemList[dateCount]->setText(0, regDate[dateCount].toString("MMMM dd, yyyy"));
dateCount++;
}
void MainWindow::button2Pressed(){
if(timeCount < dateCount){
time.removeAt(dateCount-1);
time.insert(dateCount-1,timeEdit->time());
itemList[dateCount-1]->setText(1, time[dateCount-1].toString("h:mm AP"));
timeCount++;
} else if(timeCount == dateCount){
time.removeAt(dateCount-1);
time.insert(dateCount-1,timeEdit->time());
itemList[dateCount-1]->setText(1, time[dateCount-1].toString("h:mm AP"));
}
}

void MainWindow::sortTree(){
if(dateCount == timeCount){
qSort(sortedDate.begin(),sortedDate.end());
for(int i = 0; i<regDate.size(); i++){
sortedOrder[i] = sortedDate.indexOf(regDate[i]);
}
treeWidget->clear();
for(int i = 0; i<dateCount; i++){
itemList[i] = new QTreeWidgetItem(treeWidget);
itemList[i]->setText(0, sortedDate[i].toString("MMMM dd, yyyy"));
itemList[i]->setText(1, time[sortedOrder.key(i)].toString("h:mm AP"));
}
} else {
QMessageBox error(QMessageBox::Warning, "ERROR", "Missing Date or Time.");
error.exec();
}
}}