The size of the dock widget container resizes to accommodate its content when that content imposes a constraint that would require it to: in all other cases the container sets the size of its content. The constraint will come from the contained widget's layout, minimum and maximum size, size hint, and size policy. Your best bet is to give your contained widget's layout a QLayout::SetFixedSize size constraint.
#include <QtGui>
#include <QDebug>
class MorphingWidget
: public QWidget { Q_OBJECT
public:
layout
->setSizeConstraint
(QLayout::QLayout::SetFixedSize);
// try with/without this
layout->addWidget(top);
bottom->hide();
layout->addWidget(bottom);
}
protected:
void enterEvent
( QEvent * event
) { bottom
->show
();
QWidget::enterEvent(event
);
} void leaveEvent
( QEvent * event
) { bottom
->hide
();
QWidget::leaveEvent(event
);
} private:
};
Q_OBJECT
public:
setCentralWidget(central);
dock->setWidget(new MorphingWidget(this));
addDockWidget(Qt::RightDockWidgetArea, dock);
resize(640, 480);
}
};
int main(int argc, char *argv[])
{
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QDebug>
class MorphingWidget: public QWidget {
Q_OBJECT
public:
MorphingWidget(QWidget *p = 0): QWidget(p) {
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setSizeConstraint(QLayout::QLayout::SetFixedSize); // try with/without this
top = new QPushButton("Top Button", this);
layout->addWidget(top);
bottom = new QPushButton("Bottom Button Long", this);
bottom->hide();
layout->addWidget(bottom);
}
protected:
void enterEvent ( QEvent * event ) { bottom->show(); QWidget::enterEvent(event); }
void leaveEvent ( QEvent * event ) { bottom->hide(); QWidget::leaveEvent(event); }
private:
QPushButton *top;
QPushButton *bottom;
};
class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
QWidget *central = new QTextEdit(this);
setCentralWidget(central);
QDockWidget *dock = new QDockWidget(this);
dock->setWidget(new MorphingWidget(this));
addDockWidget(Qt::RightDockWidgetArea, dock);
resize(640, 480);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks