PDA

View Full Version : Improper size issue in QWidget::sizeHint()



George Neil
22nd September 2009, 09:31
I am using QT 4.5.0.
I am using a QWidget with 10 checkboxes. 5 of these checkboxes need to be show/hide dynamically depending on certain conditions.
When i hide those 5 check boxes the QWidget is not getting self adjusted.

I checked the sizeHint(), which is not getting self adjusted.

In my case the widget is adjusting the visible 5 checkbox into the area which is used for showing the 10 checkboxs.

What all may be the possible reasons which prevent the proper resizing of the QWidget ?

Here is a sample code which i tried within a button click SLOT.

QSize prefSize = QDialog::sizeHint();
wholeWordsCheckBox->setVisible(false);
backwardCheckBox->setVisible(false);
searchSelectionCheckBox->setVisible(false);
prefSize = QDialog::sizeHint();

here the sizeHint() is working when the checkboxes are set as visible, but NOT working when the checkboxes are set as hidden.

user_mail07
22nd September 2009, 18:15
Did u try QWidget::adjustSize()?

George Neil
23rd September 2009, 06:37
yes actually my requirement is to make use of the QWidget::adjustSize(). But that is NOT working because sizeHint is NOT correct.

I have tried the following sample code example.

#include <QtGui>

#include "finddialog.h"

//! [0]
FindDialog::FindDialog(QWidget *parent)
: QDialog(parent)
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);

caseCheckBox = new QCheckBox(tr("Match &case"));
fromStartCheckBox = new QCheckBox(tr("Search from &start"));
fromStartCheckBox->setChecked(true);

//! [1]
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);

moreButton = new QPushButton(tr("&More"));
moreButton->setCheckable(true);
//! [0]
moreButton->setAutoDefault(false);

buttonBox = new QDialogButtonBox(Qt::Vertical);
buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);
//! [1]

//! [2]
extension = new QWidget;

wholeWordsCheckBox = new QCheckBox(tr("&Whole words"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
searchSelectionCheckBox = new QCheckBox(tr("Search se&lection"));
//! [2]

//! [3]
// connect(moreButton, SIGNAL(toggled(bool)), extension, SLOT(setVisible(bool)));
connect(moreButton, SIGNAL(clicked()), this, SLOT(onClickMore()));

QVBoxLayout *extensionLayout = new QVBoxLayout;
extensionLayout->setMargin(0);
extensionLayout->addWidget(wholeWordsCheckBox);
extensionLayout->addWidget(backwardCheckBox);
extensionLayout->addWidget(searchSelectionCheckBox);
extension->setLayout(extensionLayout);
//! [3]

//! [4]
QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);

QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(fromStartCheckBox);
leftLayout->addStretch(1);

QGridLayout *mainLayout = new QGridLayout;
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
mainLayout->addLayout(leftLayout, 0, 0);
mainLayout->addWidget(buttonBox, 0, 1);
mainLayout->addWidget(extension, 1, 0, 1, 2);
setLayout(mainLayout);

setWindowTitle(tr("Extension"));
//! [4] //! [5]
// extension->hide();
}

void FindDialog::onClickMore()
{
QSize prefSize = sizeHint();
wholeWordsCheckBox->setVisible(!wholeWordsCheckBox->isVisible());
backwardCheckBox->setVisible(!backwardCheckBox->isVisible());
searchSelectionCheckBox->setVisible(!searchSelectionCheckBox->isVisible());
prefSize = sizeHint();
}