torres
29th March 2013, 16:21
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.
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
vBoxLayout->setContentsMargins(1, 1, 0, 0);
mLineEdit = new QLineEdit;
vBoxLayout->addWidget(mLineEdit);
mTreeWidget = new MyTreeWidget;
mTreeWidget->setWindowFlags(Qt::Popup);
mTreeWidget->setFocusPolicy(Qt::NoFocus);
mTreeWidget->setMouseTracking(true);
mTreeWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mTreeWidget->setColumnCount(1);
mTreeWidget->setUniformRowHeights(true);
mTreeWidget->setRootIsDecorated(false);
mTreeWidget->setEditTriggers(QTreeWidget::NoEditTriggers);
mTreeWidget->setSelectionBehavior(QTreeWidget::SelectRows);
mTreeWidget->setFrameStyle(QFrame::Box | QFrame::Plain);
mTreeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
mTreeWidget->header()->hide();
mTreeWidget->resizeColumnToContents(0);
mLineEdit->installEventFilter(this);
vBoxLayout->addWidget(mTreeWidget);
connect(mLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateList(const QString&)));
connect(mTreeWidget, SIGNAL(itemClicked( QTreeWidgetItem*, int)), SLOT(updateLineEdit(QTreeWidgetItem*, int)));
move(QCursor::pos());
show();
mLineEdit->setFocus();
setLayout(vBoxLayout);
layout()->setSizeConstraint(QLayout::SetFixedSize);
}
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.
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
vBoxLayout->setContentsMargins(1, 1, 0, 0);
mLineEdit = new QLineEdit;
vBoxLayout->addWidget(mLineEdit);
mTreeWidget = new MyTreeWidget;
mTreeWidget->setWindowFlags(Qt::Popup);
mTreeWidget->setFocusPolicy(Qt::NoFocus);
mTreeWidget->setMouseTracking(true);
mTreeWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mTreeWidget->setColumnCount(1);
mTreeWidget->setUniformRowHeights(true);
mTreeWidget->setRootIsDecorated(false);
mTreeWidget->setEditTriggers(QTreeWidget::NoEditTriggers);
mTreeWidget->setSelectionBehavior(QTreeWidget::SelectRows);
mTreeWidget->setFrameStyle(QFrame::Box | QFrame::Plain);
mTreeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
mTreeWidget->header()->hide();
mTreeWidget->resizeColumnToContents(0);
mLineEdit->installEventFilter(this);
vBoxLayout->addWidget(mTreeWidget);
connect(mLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateList(const QString&)));
connect(mTreeWidget, SIGNAL(itemClicked( QTreeWidgetItem*, int)), SLOT(updateLineEdit(QTreeWidgetItem*, int)));
move(QCursor::pos());
show();
mLineEdit->setFocus();
setLayout(vBoxLayout);
layout()->setSizeConstraint(QLayout::SetFixedSize);
}