0

我在这里的主要目标是重新定义operator<,以便我可以按数字而不是按字母顺序对列进行排序。我到目前为止的代码:

主窗口.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);
4

3 回答 3

1

您正在QTreeWidgetItem使用自定义类重新实现,但是在填充QTreeWidget.

代替

QTreeWidgetItem *item = new QTreeWidgetItem();

这应该是:

TreeWidgetItem *item = new TreeWidgetItem();

除此之外,您的代码operator <看起来不错。我正在使用相同的,它适用于我。

于 2013-08-24T00:59:20.283 回答
0

单元格根据您存储在其中的数据类型进行排序。您不需要为此重新定义运算符,因为QVariant它将为您处理比较。

例如,您应该使用:

item−&gt;setData(0, `Qt::EditRole`, 10);

代替:

item->setText(0, "10");
于 2013-08-23T21:37:15.847 回答
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;
}

参考上面的代码:

我认为您的问题是小部件上的排序列对于所有 QTreeWidgetItems 保持不变。您必须对项目的值(而不是小部件的列)进行排序:

因此代码应如下所示:

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() );
}
于 2013-08-23T17:47:03.883 回答