QTreeWidgetItem operator<

0

Potrzebuje posortować QTreeWidget po wartościach int a nie po QString. Dla przykładu mam: 0x1, 0x021, 0x2, 0x035, 0x3. Wynikiem sortowania po int powinno być 0x1, 0x2 , 0x021, 0x3, 0x035. Napisałem taką klasę:

class TreeWidgetItem : public QTreeWidgetItem
{
public:
    TreeWidgetItem(QTreeWidget* parent);
    TreeWidgetItem();

    // QTreeWidgetItem interface
    
    bool operator <(const QTreeWidgetItem &other) const override;
    TreeWidgetItem &operator=(const TreeWidgetItem &other);
};
TreeWidgetItem::TreeWidgetItem(QTreeWidget *parent): QTreeWidgetItem(parent)
{

}

TreeWidgetItem::TreeWidgetItem()
{

}

bool TreeWidgetItem::operator <(const QTreeWidgetItem &other) const
{
    bool ok_num, ok_num2;
    int sortCol = treeWidget()->sortColumn();
    int num = text(sortCol).toUInt(&ok_num,16);
    int num2 = other.text(sortCol).toUInt(&ok_num2,16);
    if(ok_num && ok_num2)
    {
        return num<num2;
    }
    else
    {
        return text(sortCol) < other.text(sortCol);
    }
}

W klasie main wszędzie gdzie używałem QTreeWidgetItem , zamieniłem na typ TreeWidgetItem. Ale pojawiły się błedy:

itemList = ui->treeWidget->selectedItems();

error: no match for 'operator=' (operand types are 'QList<TreeWidgetItem>' and 'QList<QTreeWidgetItem>')**

findList = ui->treeWidget->findItems(ui->searchText->toPlainText(), Qt::MatchRecursive,selectedColumn);

error: no match for 'operator=' (operand types are 'QList<TreeWidgetItem>' and 'QList<QTreeWidgetItem>')**

Próbowałem przeładować operator=

TreeWidgetItem &TreeWidgetItem::operator=(const TreeWidgetItem &other)
{
    list=other.list;
    return *this;
}

gdzie list jest typu QList<TreeWidgetItem*> ale nie pomogło.

1

Z tego co piszesz, findItems zwraca QList<qtreewidgetitem*>

Swoją drogą, sortowanie które przedstawiłeś nie wygląda jak sortowanie po wartości int.

0

Spróbowałem z QList<QTreeWidgetItem*> list. Ale dalej występują te same błędy.

0

Ogólnie problem zdaje się wynikać z faktu, że tworzysz klasę pochodną, żeby zdefiniować porządek klasy bazowej. Jestem prawie pewien, że do posortowania wystarczy podać jakiś komparator - wszystko zależy od funkcji, której używasz do sortowania.

Wynikiem sortowania po int powinno być 0x1, 0x2 , 0x021, 0x3, 0x035
Dlaczego 0x3 jest większe od 0x21?

0

Wiem gdzie był błąd. Jak definiowałem findList i itemList to zmieniłem ich typ na QList<TreeWidgetItem*> zamiast zostawić QList<QTreeWidgetItem*> Błędy zniknęły. Ale sortowanie działa źle...

//EDIT

Udało się :)

bool TreeWidgetItem::operator <(const QTreeWidgetItem &other) const
{
    bool ok_num, ok_num2;
    int sortCol = treeWidget()->sortColumn();
    int num = text(sortCol).toUInt(&ok_num,16);
    int num2 = other.text(sortCol).toUInt(&ok_num2,16);
    if(ok_num && ok_num2)
    {
        return num<num2;
    }
    else
    {
        return text(sortCol) < other.text(sortCol);
    }
}

Wartości QString były bardzo duże. Nie mięściły się w int. Wystarczyło zmienić na

uint

1 użytkowników online, w tym zalogowanych: 0, gości: 1