Results 1 to 7 of 7

Thread: Autoresize widget, containing QTreeView, when the second is expanded

  1. #1
    Join Date
    Oct 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Autoresize widget, containing QTreeView, when the second is expanded

    Hi there,
    What I would like to know is how can I change widget's width when QTreeView, that inside of that widget expands?
    In other words, I have a QTreeView and I would like to resize the width of columns with the resizeColumnToContents() method if anything expands therefore QTreeView may not fit in the main widget.
    How can I do that? I've tried connecting QTreeView::expand(QModelIndex&) with layout's activate() method, but had no luck as I don't know how to use the QModelIndex parameter. Is there any other options? Inside of QTreeView I have a subclassed QAbstractItemModel.
    Thanks in advance!

  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: Autoresize widget, containing QTreeView, when the second is expanded

    Could you explain what you mean by "QTreeView may not fit in the main widget"?

    How can I do that? I've tried connecting QTreeView::expand(QModelIndex&) with layout's activate() method, but had no luck as I don't know how to use the QModelIndex parameter.
    I'd suggest reading what a particular method does instead of using it blindly.
    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. #3
    Join Date
    Oct 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Autoresize widget, containing QTreeView, when the second is expanded

    Quote Originally Posted by wysota View Post
    Could you explain what you mean by "QTreeView may not fit in the main widget"?
    Yeah, by that I've meant that if, for example:
    • QTreeView's width is 400 px and the MainWindow's 400 px + epsilon,
    • but, after expanding some QTreeView item, it's width become larger, for example, 500 px.
    • The main widget in that situation will still have the same width and will display sliders.

    How to autoresize it?
    Can it be done by connecting some QTreeView's, MainWindow's, or even QAbstractItemModel's methods?
    Or should I re-implement some displaying methods?

  4. #4
    Join Date
    Oct 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Autoresize widget, containing QTreeView, when the second is expanded

    To make things more clear, I provide you with couple screenshots:



    If I expand the last item, the contents wouldn't fit the window. I would like to resize it that way:



    What is the best way to do it?
    Thanks in advance!

  5. #5
    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: Autoresize widget, containing QTreeView, when the second is expanded

    Quote Originally Posted by Cucumber View Post
    How to autoresize it?
    Reimplement the widget's sizeHint() method.
    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.


  6. #6
    Join Date
    Oct 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Autoresize widget, containing QTreeView, when the second is expanded

    Quote Originally Posted by wysota View Post
    Reimplement the widget's sizeHint() method.
    Thanks! I re-implement it with following:
    Qt Code:
    1. QSize TreeView::sizeHint() const
    2. {
    3. int doubleFrame = 2 * frameWidth();
    4.  
    5. int w = header()->length() + doubleFrame;
    6. int h = height() + doubleFrame;
    7.  
    8. return QSize(w, h);
    9. }
    To copy to clipboard, switch view to plain text mode 

    As well, I connect(treeView, SIGNAL(expanded(const QModelIndex&)), treeView, SLOT(needResize(const QModelIndex&)));

    Qt Code:
    1. void TreeView::needResize(const QModelIndex& index)
    2. {
    3. resize(sizeHint());
    4. updateGeometry();
    5. }
    To copy to clipboard, switch view to plain text mode 
    And it works. Have I done it right?
    Last edited by Cucumber; 3rd November 2010 at 14:56.

  7. #7
    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: Autoresize widget, containing QTreeView, when the second is expanded

    No. If you need the needResize() slot then it means your widget is not in a layout. You only need to call updateGeometry() but not resize(sizeHint()).
    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.


Similar Threads

  1. QLabels autoresize to a pixmap?
    By hersheyzombie in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2009, 00:34
  2. QTreeView autoresize and autoexpand
    By scrasun in forum Newbie
    Replies: 1
    Last Post: 26th September 2009, 16:18
  3. About QTreeVew's expanded() signal
    By yangyunzhao in forum Qt Programming
    Replies: 0
    Last Post: 18th August 2009, 03:32
  4. QTreeView restore Expanded node after reload model
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2008, 17:42
  5. Qt 4 expanded mask
    By bunjee in forum Qt Programming
    Replies: 4
    Last Post: 7th May 2008, 14:21

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.