我的主要目标是重新定义operator<
,以便我可以按字母顺序而不是按字母顺序对列进行排序。我到目前为止的代码:
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
在mainwindow.cpp中我有ui->_display->setSortingEnabled(true);
以某种方式它不使用重新定义的operator<
(重新定义的函数永远不会被调用,它使用预定义的函数)。请帮忙。
ui->_abooking->clear();
ui->_abooking->setSortingEnabled(false);
QSqlQuery query;
query.prepare(" SELECT shownumdays, shownumpastdays FROM setting_"+ user +
" WHERE 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` "
" FROM abooking "
" WHERE (DATE_ADD(`Arrival Date`,INTERVAL `Nights` - 1 DAY) >= (CURRENT_DATE - "+ QString::number(pastdays) +") AND "
" (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();
for (int i = 0; i < 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);
答案 0 :(得分:1)
您正在使用自定义类重新实现QTreeWidgetItem
,但在填充QTreeWidget
时不会使用它。
而不是
QTreeWidgetItem *item = new QTreeWidgetItem();
这应该是:
TreeWidgetItem *item = new TreeWidgetItem();
除此之外,operator <
的代码看起来不错。我使用的是相同的,它对我有用。
答案 1 :(得分:0)
bool operator< (const QTreeWidgetItem &other) const
{
int sortCol = treeWidget()->sortColumn();
int myNumber = text(sortCol).toInt();
int otherNumber = other.text(sortCol).toInt();
return myNumber < otherNumber;
}
参考上面的代码:
我认为您的问题是窗口小部件上的排序列对于所有QTreeWidgetItem都保持不变。您必须对项目的值(而不是窗口小部件的列)进行排序:
因此代码应如下所示:
bool operator< (const QTreeWidgetItem &other) const
{
Q_ASSERT( other.treeWidget() == treeWidget() );
const int col = treeWidget()->sortColumn();
const int role = Qt::DisplayRole;
return( data( col, role ).toInt() < other.data( col, role ).toInt() );
}
答案 2 :(得分:0)
单元格根据您存储在其中的数据类型进行排序。您无需为此重新定义运算符,因为QVariant
将为您进行比较。
例如,您应该使用:
item−>setData(0, `Qt::EditRole`, 10);
而不是:
item->setText(0, "10");