PDA

View Full Version : resize qdockwidget through embedded widget



codeman
28th August 2012, 20:52
Hello friends,

I have a question. I add a QWidget into a QDockwidget. How can I resize(the width) from dockwidget, when I resize the QWidget in it??

Yours,

ChrisW67
29th August 2012, 03:23
Your question is unclear: do you want to resize the dock widget and have the contents resize, or do you want to resize the dock widget to fit the content you put in it.

codeman
29th August 2012, 14:10
When I resize the embedded widget in its EnterEvent I resize it but the QDockwidget, which operate here as container for my widget dont resize it self.Thats my point.

Yours,

ChrisW67
30th August 2012, 02:45
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:
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"

codeman
31st August 2012, 12:41
Great thanks for your help,

in my situation I am also subclass QWidget.I paint some rects on the widget, and in enter event I change the size of the rects, so I make a repaint, resize the widget and the Dockwidget make nothing. I dont have any layout in there.

Maybe I have to give a pointer of the Dockwidget to the childwidget and make a setFixedWidth or so!!

Yours,