I put the declaration of QTreeView into the header file, and it works!!!
That's still the wrong way to do it, Qt-wise. You should create the tree view instance on the heap, not the stack:

Qt Code:
  1. QTreeView * w = new QTreeView( swatch1 );
  2. w->setModel(&model);
  3. swatch1->setWidget( w );
  4. w->setEnabled(true);
To copy to clipboard, switch view to plain text mode 

If you need access to the tree widget from outside the methond where you create it, then put the "QTreeView * w;" declaration as a member variable of the class in the header file.