PDA

View Full Version : Problems with custom sorting of QTreeWidget (problem solved)



Radek
5th August 2013, 07:38
I need to sort a tree. The tree contains TreeItems:



class TreeItem : public QTreeWidgetItem
{
public :

enum type_t
{
tFile,
tDir
};

QString *data;
type_t dtype;
bool parsed;

QStringRef title;
QStringRef note;
QStringRef auth;
QStringRef src;
QStringRef date;
QStringRef url1;
QStringRef url2;
QStringRef txt;
int idate;

TreeItem();
virtual ~TreeItem();

virtual bool operator < ( const QTreeWidgetItem& other ) const;

bool SetData( const QString *fd );
bool SetType( const type_t it );
};


The tree is populated from a (application modal) dialog:



void MyFrame::ShowDirDlg()
{
OpenDlg->ShowDlg(true); // show the dialog

if( OpenDlg->current != nullptr ) // if selected item acceptable
{
MyTraverser dt; // MyTraverser will load data and populate the tree

ui.wndTree->setSortingEnabled(false); // start updating the tree
ui.wndTree->clear();
ui.wndText->clear();

dt.tree = ui.wndTree;
dt.traverse(OpenDlg->current->fname); // populate the tree

ui.wndTree->setSortingEnabled(true); // sort
ui.wndTree->sortItems(0,Qt::AscendingOrder);
}
}


Everything works perfectly except for sorting. The operator < is never called. Without sortItems() the items get sorted but in descending order. With sortItems() the items get sorted in asscending order but still using default (sort by item labels). What am I doing wrong? How to force the tree using my operator < ?

-----------------------------------

Edit: Never mind, problem solved. I needed to rebuild instead of build after adding the operator to the header. I've seen this bug (it's definitely a "make" bug) many times even if I am a complete newbie in Qt. The bug consists in:

- overlooking patched headers and using the old ones somehow. (You can see this while debugging the app.)
- overlooking patched .ui files (even if you patch from the Creator project and not from the Designer) and using the old ones. (In fact, using the old "ui_form.h" and "ui_form.cpp" files.)

When this happens, the project "compiles ok" and then crashes mysteriously. Step 1 (be polite): rebuild. Step 2 (be rude): quit Creator, delete debug and release folders, restart, build. Step 1 is usually enough. In the "sort" cause, the project did not crash so that I forgot to rebuild.

I wonder whether the bug is specific for Linux Debian or whether the bug has been seen elsewhere, too.