gpt4 book ai didi

c++ - 重新定义 QTreeWidgetItem::operator<

转载 作者:行者123 更新时间:2023-11-28 07:27:45 24 4
gpt4 key购买 nike

我的主要目标是重新定义 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);

最佳答案

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

代替

QTreeWidgetItem *item = new QTreeWidgetItem();

这应该是:

TreeWidgetItem *item = new TreeWidgetItem();

除此之外,您的代码 operator <看起来不错。我使用的是相同的,它对我有用。

关于c++ - 重新定义 QTreeWidgetItem::operator<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18408006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com