Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Widget follow the size of another Widget
Forum Updated to NodeBB v4.3 + New Features

Widget follow the size of another Widget

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 14.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    beowulf
    wrote on last edited by
    #1

    @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)?

    -- 0x00

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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(....)
      1 Reply Last reply
      1
      • H Offline
        H Offline
        hans977
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          beowulf
          wrote on last edited by
          #4

          [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.

          -- 0x00

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hans977
            wrote on last edited by
            #5

            [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.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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);@

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved