Results 1 to 4 of 4

Thread: problems with getting height and width of widget

  1. #1
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default problems with getting height and width of widget

    I need to get the height and width for a QFrame inside a tab of a QTabWidget. The QFrame is inside the first tab of the tabwidget and I can only get the right width and height if this is the tab selected when I do showMaximized. From what I understand I can get the width and height if the widget have been shown as this is when it is calculated. So from what I understand the best way to get the size must be when changing between the tabs and the new tab is visible. How can I do that?

    Its a little bit hard to explain the problem but the main thing is that I need to get the height and width of a QFrame and as the QFrame is depending of the screen size it changes when it becomes visible. So how can I get the size of the QFrame?

    This is the code (If I set tabWidget->setCurrentIndex(0) I get the right size in whatIsTheSize(), as 0 is the tabindex with the QFrame inside.)
    main.cpp:
    Qt Code:
    1. TestSize *w = new TestSize();
    2. w->showMaximized();
    3. w->whatIsTheSize();
    To copy to clipboard, switch view to plain text mode 

    TestSize.cpp:
    Qt Code:
    1. void TestSize::whatIsTheSize() {
    2. int WIDGETWIDTH = ui.frame->width();
    3. int WIDGETHEIGHT = ui.frame->height();
    4. }
    To copy to clipboard, switch view to plain text mode 

    ui_testsize.h:
    Qt Code:
    1. void setupUi(QWidget *TestSizeClass)
    2. {
    3. if (TestSizeClass->objectName().isEmpty())
    4. TestSizeClass->setObjectName(QString::fromUtf8("TestSizeClass"));
    5. TestSizeClass->resize(207, 300);
    6. verticalLayout = new QVBoxLayout(TestSizeClass);
    7. verticalLayout->setSpacing(6);
    8. verticalLayout->setContentsMargins(11, 11, 11, 11);
    9. verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    10. tabWidget = new QTabWidget(TestSizeClass);
    11. tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
    12. tab_3 = new QWidget();
    13. tab_3->setObjectName(QString::fromUtf8("tab_3"));
    14. verticalLayout_2 = new QVBoxLayout(tab_3);
    15. verticalLayout_2->setSpacing(6);
    16. verticalLayout_2->setContentsMargins(11, 11, 11, 11);
    17. verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
    18. frame = new QFrame(tab_3);
    19. frame->setObjectName(QString::fromUtf8("frame"));
    20. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    21. sizePolicy.setHorizontalStretch(0);
    22. sizePolicy.setVerticalStretch(0);
    23. sizePolicy.setHeightForWidth(frame->sizePolicy().hasHeightForWidth());
    24. frame->setSizePolicy(sizePolicy);
    25. frame->setFrameShape(QFrame::StyledPanel);
    26. frame->setFrameShadow(QFrame::Raised);
    27.  
    28. verticalLayout_2->addWidget(frame);
    29.  
    30. tabWidget->addTab(tab_3, QString());
    31. tab_4 = new QWidget();
    32. tab_4->setObjectName(QString::fromUtf8("tab_4"));
    33. verticalLayout_3 = new QVBoxLayout(tab_4);
    34. verticalLayout_3->setSpacing(6);
    35. verticalLayout_3->setContentsMargins(11, 11, 11, 11);
    36. verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));
    37. frame_2 = new QFrame(tab_4);
    38. frame_2->setObjectName(QString::fromUtf8("frame_2"));
    39. frame_2->setFrameShape(QFrame::StyledPanel);
    40. frame_2->setFrameShadow(QFrame::Raised);
    41.  
    42. verticalLayout_3->addWidget(frame_2);
    43.  
    44. tabWidget->addTab(tab_4, QString());
    45.  
    46. verticalLayout->addWidget(tabWidget);
    47.  
    48. retranslateUi(TestSizeClass);
    49.  
    50. tabWidget->setCurrentIndex(1);
    51.  
    52. QMetaObject::connectSlotsByName(TestSizeClass);
    53. }
    To copy to clipboard, switch view to plain text mode 

    Thanks really much!

  2. #2
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: problems with getting height and width of widget

    Anyone got an idea on this?

  3. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: problems with getting height and width of widget

    Not sure if I understand you, but did you try currentChanged ?
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  4. The following user says thank you to john_god for this useful post:

    martinn (10th March 2010)

  5. #4
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: problems with getting height and width of widget

    Try to use contentsRect function.

Similar Threads

  1. how to increase menuitem's height and width?
    By aoo7wangyan in forum Qt Programming
    Replies: 1
    Last Post: 13th July 2009, 10:00
  2. Replies: 6
    Last Post: 14th April 2009, 17:40
  3. QLayout: different SizeConstraints for width and height?
    By PhilippB in forum Qt Programming
    Replies: 0
    Last Post: 23rd February 2009, 16:33
  4. Pixmap height / width without loading it
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 07:10
  5. width and height of QTabWidget
    By chikkireddi in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 13:53

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.