Results 1 to 4 of 4

Thread: Window with changeable height but width set by the contained widget

  1. #1
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Window with changeable height but width set by the contained widget

    BACKGROUND

    I have a widget, MyWidget, deriving from QWidget. MyWidget contains a class, MyTableView, which derives from QTableView which is in a QVBoxLayout.

    MyWidget has setWindowFlags(Qt::Tool) so it is displayed in a window even though it's a child of QMainWindow.

    MyTableView has sizeHint overidden to pass back the size of the actual table view based on its headers:

    Qt Code:
    1. return QSize(horizontalHeader()->sizeHint().width(), verticalHeader()->sizeHint().width());
    To copy to clipboard, switch view to plain text mode 

    This means that MyTableView is only as wide as its horizontal header and will change its width along with the horizontal header. The user is able to change the width of MyTableView by changing the width of the columns.

    DESIRED BEHAVIOUR

    I want the width of the window to expand and contract along with the table view. But I also want the user to be able to change the height of the window. So, the window's width is dictated by the width of the widget, but the window should control its own height or allow its height to be changed by the user.

    PROBLEM

    If I call MyWidget::layout()->setSizeConstraint(QLayout::SetFixedSize) the layout responds to sizeHint of the widget. This works perfectly for width - as MyTableWidget's width changes the window changes to fit. But this means that the height of the window is determined by the widget's sizeHint too. MyTableView can contain hundreds of rows, so this doesn't work. Is there a way to have SetFixedSize apply only to width for a layout?

    I have tried every combination that I can think of (sizeConstraints, sizePolicy, etc.), but I just can't seem to get this working the way I would like it to work.

    Does anyone have any ideas? If there was a way to resize the height of MyTableView that would solve the problem, but that would involve resizing the viewport and I don't know how to do that or if it's even possible.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Window with changeable height but width set by the contained widget

    Unfortunately size constraints work in both directions. You need to reimplement resizeEvent for the table widget and make it adjust its window() size. A bit dirty solution but it will work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    stefanadelbert (10th February 2011)

  4. #3
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Window with changeable height but width set by the contained widget

    Wysota, thanks very much for your reply.

    This is what I found on QWidget::window():

    QWidget * QWidget::window () const
    Returns the window for this widget, i.e. the next ancestor widget that has (or could have) a window-system frame.

    If the widget is a window, the widget itself is returned.

    Typical usage is changing the window title:

    Qt Code:
    1. aWidget->window()->setWindowTitle("New Window Title")
    To copy to clipboard, switch view to plain text mode 
    ;
    So if I understand your suggestions correctly, I could handle a resizeEvent on MyWidget and directly resize its parent window's size. I could thereby limit the width of the parent window to whatever I want it to be (width of sizeHint of MyTableView), but allow the height to change freely. If that works, then it's a pretty nifty solution, albeit dirty.

    I'll post back with the results.

    :wq

  5. #4
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Window with changeable height but width set by the contained widget

    Seems I've found a fairly simple solution to this problem. If the width of a widget is based is based on the width of one of its children, then the child should notify when its width has changed and this way the parent can adjust its width accordingly.

    To use my example above, I created a signal on MyTableView called geometriesChanged which is emitted when MyTableView changes width, e.g. column widths are changed. MyWidget handles SIGNAL(geometriesChanged()) by calling setFixedWidth(MyTableView->horizontalHeader()->width()).

    The key is that MyWidget set its width based on the width of a child widget using setFixedWidth, but it never calls setFixedHeight, which means that the user can freely adjust the height of MyWidget by dragging the MyWidget's top and bottom window borders. But if the user wants to change the width of MyWidget he needs to change the width of MyTableView which can only be done by changing the column widths of MyTableView.

    If anyone actually wants working code to demonstrate this, let me know. I've implemented this solution in the project I'm working on and that code is convoluted at best, so it would take a little work to put a working example of the principle together which is uncluttered by all the other stuff going on.

Similar Threads

  1. Replies: 3
    Last Post: 30th December 2010, 16:34
  2. Auto-adjusting height of a constant width window
    By akiross in forum Qt Programming
    Replies: 0
    Last Post: 7th July 2010, 02:46
  3. Replies: 2
    Last Post: 29th June 2010, 01:40
  4. problems with getting height and width of widget
    By martinn in forum Qt Programming
    Replies: 3
    Last Post: 10th March 2010, 04:04
  5. Replies: 6
    Last Post: 14th April 2009, 18:40

Tags for this Thread

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.