Important: Please read the Qt Code of Conduct -

How to place widgets by specifying positions in QFrame

  • Please tell me how to place widgets by specifying positions in QFrame by program.
    I couldn't use frame->addWidget() method.
    I don't want the widgets laid out automatically.

  • Moderators

    Give the child widget a parent and then you can use setGeometry() or move() and resize().
    For example:

    QFrame* parentWidget = new QFrame();
    QPushButton* childWidget = new QPushButton("Hello!", parentWidget); //the parent is what's important here
    childWidget->setGeometry(10, 10, 200, 100);

  • @Chris-Kawa
    Thank you for answering.
    I could place widgets like that.
    But how to properly arrange widgets whose size changes like labels.
    I would like to calculate the vertical size that varies depending on the label text with fixed width.

  • Moderators

    Sure, you could do that using boundingRect(), but it sounds like you want to manually redo what the layout does for you. Are you sure you don't want it? What's your usecase exactly?

  • @Chris-Kawa
    I'm trying to make a Mastodon client application.
    So I am planning to display toot in conjunction with two QFrames.
    Specifically, move the toot that came to the bottom of the left QFrame to the top of the right QFrame.
    Also, if one toot is separated by the upper or lower end of the QFrame, it is displayed partly in two QFrames.

    This is what I want to do, but when I use to use setGeometry when displaying toot as shown in the figure,
    the text will be interrupted halfway.
    I wanted to fix this and I asked you any additional questions earlier.

    alt text

  • Lifetime Qt Champion


    Shouldn't this rather be implement with a QListView and a custom QStyledItemDelegate ?

Log in to reply