c++ - Redefining QTreeWidgetItem::operator< -
my main goal here redefine operator<
can sort columns numerically instead of alphabetically. code have far:
mainwindow.h
#ifndef mainwindow_h #define mainwindow_h //bunch of includes... #include <qtreewidgetitem> #include <qtreewidget> extern qsqldatabase db; extern qstring prefix; extern qstring site; extern qstring dbname; extern qstring user; namespace ui { class mainwindow; } class clientinfo; class mainwindow : public qmainwindow { q_object public: explicit mainwindow(qwidget *parent = 0); void rpopulate(); void rbookingdisplay(); qpushbutton *button; ~mainwindow(); public slots: //slots... private: ui::mainwindow *ui; void closeevent(qcloseevent *close); }; class treewidgetitem : public qtreewidgetitem { public: treewidgetitem(qtreewidget *tree) : qtreewidgetitem(tree) {} treewidgetitem(qtreewidget * parent, const qstringlist & strings) : qtreewidgetitem (parent,strings) {} bool operator< (const qtreewidgetitem &other) const { int sortcol = treewidget()->sortcolumn(); int mynumber = text(sortcol).toint(); int othernumber = other.text(sortcol).toint(); return mynumber < othernumber; } }; #endif // mainwindow_h
and in mainwindow.cpp have ui->_display->setsortingenabled(true);
somehow doesn't use redefined operator<
(the redefined function never called, uses predefined one). please help.
ui->_abooking->clear(); ui->_abooking->setsortingenabled(false); qsqlquery query; query.prepare(" select shownumdays, shownumpastdays setting_"+ user + " id = 1; "); query.exec(); query.first(); int days = query.record().value("shownumdays").toint(); int pastdays = query.record().value("shownumpastdays").toint(); query.prepare(" select `"+columns.join("`, `")+ "`,`currency`, `# of room` " " abooking " " (date_add(`arrival date`,interval `nights` - 1 day) >= (current_date - "+ qstring::number(pastdays) +") , " " (to_days(`arrival date`) + `nights` - 1 - to_days(now())) <= "+ qstring::number(days) +") "); query.exec(); qstring confirm_num = ""; bool flip = true; while (query.next()){ if (!(confirm_num == query.record().value("confirmation #").tostring())) { flip = (!flip); } qtreewidgetitem *item = new qtreewidgetitem(); (int = 0; < columns.length(); i++) { if (columns.at(i) == "arrival date") { item->settext(i, query.record().value(columns.at(i)).todate().tostring("dd-mm-yyyy")); } else if ((columns.at(i) == "amount due" || columns.at(i) == "paid") && (confirm_num == query.record().value("confirmation #").tostring())) { item->settext(i, ""); } else if ((columns.at(i) == "arrival time")){ item->settext(i, " " + query.record().value(columns.at(i)).totime().tostring("hh:mm")); } else { item->settext(i, query.record().value(columns.at(i)).tostring()); } if (columns.at(i) == "amount due" && (!(item->text(i) == ""))) { item->settextcolor(i, qt::darkred); item->settext(i, item->text(i) + " " + query.record().value("currency").tostring()); } else if (columns.at(i) == "room") { item->settext(i, query.record().value("# of room").tostring() + " " + item->text(i)); } if (flip) { item->setbackground(i , qt::gray); } } if ((!(confirm_num == query.record().value("confirmation #").tostring()))) { confirm_num = query.record().value("confirmation #").tostring(); ui->_abooking->addtoplevelitem(item); } else { ui->_abooking->toplevelitem(ui->_abooking->toplevelitemcount() - 1)->addchild(item); ui->_abooking->toplevelitem(ui->_abooking->toplevelitemcount() - 1)->setexpanded(true); } } ui->_abooking->setsortingenabled(true);
you're reimplementing qtreewidgetitem
custom class, don't use when populating qtreewidget
.
instead of
qtreewidgetitem *item = new qtreewidgetitem();
this should be:
treewidgetitem *item = new treewidgetitem();
other that, code operator <
looks fine. i'm using same , works me.
Comments
Post a Comment