I have a widget with a QLineEdit and a QTreeWidget in a QVBoxLayout. I would like to size MyWidget to adjust to the longest string in the treeWidget.
Do I need to override sizeHint() or can qt do it somehow? When I type in the QLineEdit, it automatically adjusts the items in QTreeWidget.

Qt Code:
  1. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
  2. {
  3. setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
  4.  
  5. QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
  6. vBoxLayout->setContentsMargins(1, 1, 0, 0);
  7. mLineEdit = new QLineEdit;
  8.  
  9. vBoxLayout->addWidget(mLineEdit);
  10.  
  11. mTreeWidget = new MyTreeWidget;
  12. mTreeWidget->setWindowFlags(Qt::Popup);
  13. mTreeWidget->setFocusPolicy(Qt::NoFocus);
  14. mTreeWidget->setMouseTracking(true);
  15. mTreeWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  16.  
  17. mTreeWidget->setColumnCount(1);
  18. mTreeWidget->setUniformRowHeights(true);
  19. mTreeWidget->setRootIsDecorated(false);
  20. mTreeWidget->setEditTriggers(QTreeWidget::NoEditTriggers);
  21. mTreeWidget->setSelectionBehavior(QTreeWidget::SelectRows);
  22. mTreeWidget->setFrameStyle(QFrame::Box | QFrame::Plain);
  23. mTreeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  24. mTreeWidget->header()->hide();
  25. mTreeWidget->resizeColumnToContents(0);
  26.  
  27. mLineEdit->installEventFilter(this);
  28. vBoxLayout->addWidget(mTreeWidget);
  29. connect(mLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateList(const QString&)));
  30. connect(mTreeWidget, SIGNAL(itemClicked( QTreeWidgetItem*, int)), SLOT(updateLineEdit(QTreeWidgetItem*, int)));
  31. move(QCursor::pos());
  32. show();
  33. mLineEdit->setFocus();
  34. setLayout(vBoxLayout);
  35. layout()->setSizeConstraint(QLayout::SetFixedSize);
  36. }
To copy to clipboard, switch view to plain text mode