Results 1 to 5 of 5

Thread: QFrame questions

  1. #1
    Join Date
    Apr 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default QFrame questions

    I have a few questions about frames.
    Here is a test program I have been experimenting with

    Qt Code:
    1. class BuildGui : public QMainApplication
    2. {
    3. public:
    4. BuildGui(QWidget* parent = 0);
    5. private:
    6. QWidget* makeWidget(QString text);
    7.  
    8. QVBoxLayout* layout;
    9. };
    10.  
    11. BuildGui::BuildGui(QWidget* parent = 0) : QMainApplication(parent)
    12. {
    13. QWidget* w1 = makeWidget("Ex 1");
    14. QWidget* w2 = makeWidget("Ex 2");
    15.  
    16. layout = new QVBoxLayout;
    17. layout->addWidget(w1);
    18. layout->addWidget(w2);
    19.  
    20. QWidget* cw = new QWidget;
    21. cw->setLayout(layout);
    22.  
    23. sa->setWidget(cw);
    24.  
    25. setCentralWidget(sa);
    26. }
    27.  
    28. QWidget* BuildGui::makeWidget(QString text)
    29. {
    30. QLineEdit* le = new QLineEdit(text);
    31. return le;
    32. }
    To copy to clipboard, switch view to plain text mode 

    which works as expected - I see the 2 text boxes one above the other with "Ex 1" and "Ex 2" for the text.

    Now I add this method:
    Qt Code:
    1. QWidget* BuildGui::addFrame(QWidget w)
    2. {
    3. QFrame* f = new QFrame;
    4. f->setFrameStyle(QFrame::Box);
    5. w->setParent(f);
    6. return f;
    7. }
    To copy to clipboard, switch view to plain text mode 

    and modify the constructor:
    Qt Code:
    1. QWidget* f1 = addFrame(sc1);
    2. ...
    3. // layout->setWidget(w1);
    4. layout->setWidget(f1);
    To copy to clipboard, switch view to plain text mode 

    now the "Ex 1" box does not show up. If I add
    Qt Code:
    1. f1->setMinimumSize(50,50);
    To copy to clipboard, switch view to plain text mode 
    then it is visible.
    So my question is
    Can i make the frame automatically adjust to the size of the widget it is framing?

    Thanks,
    Steve

    PS I know some widgets have a setFrame() method but I want this to work for framinh any widget.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QFrame questions

    If you were correctly using a Qt layout inside the frame then the frame would drive the size of the contained widget within the constraints imposed on the layout and widget. Regardless of the original size of the QFrame once you add it to a larger layout its size will be controlled by that layout.

    Qt does not, as a rule, operate in the content-drives-container-size direction because size constraints naturally flow down from the available desktop space, to the uppermost window and down to smaller and smaler widgets.

  3. #3
    Join Date
    Apr 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QFrame questions

    I'm finding this very confusing.
    I set a main window size of 210x210 in the main program.

    I create 3 LineEdits, add them to the vbox and they show up taking the minimum amount of space.

    I add a frame around the first one and it disappears.

    I set a size of 200x200 on the widget that the layout is set on, cw in my code example, now the frame shows up but it is a frame around the space not around the LineEdit.
    The 2 LineEdits without a frame are at the bottom and take up the minimum space they can while the frame takes up the rest of the 200 vertical space.
    You explained this saying that the frame is controlled by the layout it is placed in.

    I really want the top 2 widgets to take up the minimum space and let the bottom one expand to fill the rest of the space so I added:
    layout->setStretchFactor(sc3,1)

    My understanding (if you can call it that) is that the stretch factor is turned off by default so turning it on for one widget will cause that widget to stretch to take up the maximum space but what this did is remove the 1st widget!?!

    You said that "Qt does not, as a rule, operate in the content-drives-container-size direction". Does that imply that it is possible to make it work that way?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QFrame questions

    A QLineEdit has a fixed vertical height, i.e. a single line of text, so trying to stretch it vertically does not make a lot of sense. If the widget were QTextEdits then the result would be different.

    Does that imply that it is possible to make it work that way?
    Yes, to some degree, but you have to use the layouts correctly. The content of the frame is controlled by a layout or not at all as in your code. In your code the frame owns the widget but there is no layout applied so the widget and frame are independently sized. This is likely why it "disappears".

    Qt Code:
    1. QWidget *wrapWithFrame(QWidget *widget) {
    2. if (!widget)
    3. return widget;
    4.  
    5. QFrame *frame = new QFrame(widget->parentWidget());
    6. frame->setFrameStyle(QFrame::Box);
    7.  
    8. QVBoxLayout *layout = new QVBoxLayout(frame);
    9. layout->setContentsMargins(0, 0, 0, 0);
    10. // layout->setSizeConstraint(QLayout::SetFixedSize); // << you might like to read and experiment
    11. layout->addWidget(widget);
    12. frame->setLayout(layout);
    13. return frame;
    14. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QFrame questions

    Thanks, that's what I needed.

    I was thinking that a layout controlled how multiple widgets were layed out in relation to each other and hadn't considered that a single widget would need a layout.

Similar Threads

  1. QFrame
    By Johnnyj2j in forum Newbie
    Replies: 7
    Last Post: 21st July 2012, 12:03
  2. QFrame within QScrollArea
    By CSwanepoel in forum Newbie
    Replies: 3
    Last Post: 9th May 2011, 00:19
  3. Using css for all QFrame's except one.
    By babu198649 in forum Newbie
    Replies: 2
    Last Post: 10th July 2008, 08:05
  4. a tight QFrame is all i need
    By baray98 in forum Qt Programming
    Replies: 5
    Last Post: 7th January 2008, 07:53
  5. Reg - QFrame
    By suresh in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2006, 04:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.