Widget follow the size of another Widget



  • @MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    {}

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {

    ui->setupUi(this);
    QWidget *MWidget = new MyWidget(ui->widget);
    

    }@

    How do I make a widget (MyWidget) follow the size of another Widget(ui->widget)?


  • Moderators

    Qt widgets use layouting sytem:

    • Create a parent widget (in your case it's ui->widget) and a child widget (in your case MWidget)
    • Create a layout (QHBoxLayout, QGridLayout, QFormLayout etc.)
    • add child to the layout with addWidget(...) method
    • set this layout on the parent widget with setLayout(....)


  • I had a parent widget that I wanted to stay the same size as a child widget. To do that I set the size policy of the parent widget to that of the child widget in the constructor like so: parent->setSizePolicy(child->sizePolicy()); Then I overwrote the size hint method in the parent so it would return the size hint of the child. I seems to work fine. You might be able to do the same type of thing.



  • [quote author="Krzysztof Kawa" date="1357145428"]Qt widgets use layouting sytem:

    • Create a parent widget (in your case it's ui->widget) and a child widget (in your case MWidget)
    • Create a layout (QHBoxLayout, QGridLayout, QFormLayout etc.)
    • add child to the layout with addWidget(...) method
    • set this layout on the parent widget with setLayout(....)[/quote]

    Does not work in some places, for example: I am using the ui->widget with a splitter.

    When I run the application the size stays the same of your parent, but when I resize the splitter he(MyWidget) does not follow the size of ui->widget.

    [quote author="hans977" date="1357147158"]I had a parent widget that I wanted to stay the same size as a child widget. To do that I set the size policy of the parent widget to that of the child widget in the constructor like so: parent->setSizePolicy(child->sizePolicy()); Then I overwrote the size hint method in the parent so it would return the size hint of the child. I seems to work fine. You might be able to do the same type of thing.[/quote]

    This left me a little confused.
    I understand how to put the parent with the same size of the child, but I did not understand size hint.



  • [quote author="l3e0wulf" date="1357168878"]

    When I run the application the size stays the same of your parent, but when I resize the splitter he(MyWidget) does not follow the size of ui->widget.

    . . . I did not understand size hint.[/quote]

    You will have to call MyWidget->adjustSize() from ui->widget to update the size of the MyWidget when ui->widget changes. You might want to check out resizeEvent for that. You will have to call MyWidget->updateGeometry() on the widget also if its parents layout needs to update.

    Size hint (along with things like max size, min size, etc) is used by the layout or parent widget to determine what size to draw the child widget.

    I am no expert, but this is how it seems to work for me.


  • Moderators

    hans977
    This is no way to do layouts. What you're suggesting is reinventing the wheel (QLayout to be precise) and manually programming all the logic that Qt is actually designed to do for you.

    l3e0wulf
    I don't have a clear picture what you want to achieve but classic example of a splitter layout would be something like this:

    @QWidget* parentWidget = new QWidget();

    QVBoxLayout* layout = new QVBoxLayout();
    QSplitter * splitter = new QSplitter();
    QWidget * w1 = new QWidget();
    QWidget * w2 = new QWidget();

    splitter->addWidget(w1);
    splitter->addWidget(w2);
    layout->addWidget(splitter);
    parentWidget->setLayout(layout);@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.