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:
Code:
TestSize *w = new TestSize();
w->showMaximized();
w->whatIsTheSize();
TestSize.cpp:
Code:
void TestSize::whatIsTheSize() {
int WIDGETWIDTH = ui.frame->width();
int WIDGETHEIGHT = ui.frame->height();
}
ui_testsize.h:
Code:
void setupUi
(QWidget *TestSizeClass
) {
if (TestSizeClass->objectName().isEmpty())
TestSizeClass
->setObjectName
(QString::fromUtf8("TestSizeClass"));
TestSizeClass->resize(207, 300);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout
->setObjectName
(QString::fromUtf8("verticalLayout"));
tabWidget
->setObjectName
(QString::fromUtf8("tabWidget"));
tab_3
->setObjectName
(QString::fromUtf8("tab_3"));
verticalLayout_2->setSpacing(6);
verticalLayout_2->setContentsMargins(11, 11, 11, 11);
verticalLayout_2
->setObjectName
(QString::fromUtf8("verticalLayout_2"));
frame
->setObjectName
(QString::fromUtf8("frame"));
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(frame->sizePolicy().hasHeightForWidth());
frame->setSizePolicy(sizePolicy);
frame
->setFrameShape
(QFrame::StyledPanel);
frame
->setFrameShadow
(QFrame::Raised);
verticalLayout_2->addWidget(frame);
tabWidget
->addTab
(tab_3,
QString());
tab_4
->setObjectName
(QString::fromUtf8("tab_4"));
verticalLayout_3->setSpacing(6);
verticalLayout_3->setContentsMargins(11, 11, 11, 11);
verticalLayout_3
->setObjectName
(QString::fromUtf8("verticalLayout_3"));
frame_2
->setObjectName
(QString::fromUtf8("frame_2"));
frame_2
->setFrameShape
(QFrame::StyledPanel);
frame_2
->setFrameShadow
(QFrame::Raised);
verticalLayout_3->addWidget(frame_2);
tabWidget
->addTab
(tab_4,
QString());
verticalLayout->addWidget(tabWidget);
retranslateUi(TestSizeClass);
tabWidget->setCurrentIndex(1);
}
Thanks really much!
Re: problems with getting height and width of widget
Anyone got an idea on this?
Re: problems with getting height and width of widget
Not sure if I understand you, but did you try currentChanged ?
Re: problems with getting height and width of widget
Try to use contentsRect function.