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:
TestSize *w = new TestSize();
w->showMaximized();
w->whatIsTheSize();
TestSize *w = new TestSize();
w->showMaximized();
w->whatIsTheSize();
To copy to clipboard, switch view to plain text mode
TestSize.cpp:
void TestSize::whatIsTheSize() {
int WIDGETWIDTH = ui.frame->width();
int WIDGETHEIGHT = ui.frame->height();
}
void TestSize::whatIsTheSize() {
int WIDGETWIDTH = ui.frame->width();
int WIDGETHEIGHT = ui.frame->height();
}
To copy to clipboard, switch view to plain text mode
ui_testsize.h:
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);
}
void setupUi(QWidget *TestSizeClass)
{
if (TestSizeClass->objectName().isEmpty())
TestSizeClass->setObjectName(QString::fromUtf8("TestSizeClass"));
TestSizeClass->resize(207, 300);
verticalLayout = new QVBoxLayout(TestSizeClass);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
tabWidget = new QTabWidget(TestSizeClass);
tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
tab_3 = new QWidget();
tab_3->setObjectName(QString::fromUtf8("tab_3"));
verticalLayout_2 = new QVBoxLayout(tab_3);
verticalLayout_2->setSpacing(6);
verticalLayout_2->setContentsMargins(11, 11, 11, 11);
verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
frame = new QFrame(tab_3);
frame->setObjectName(QString::fromUtf8("frame"));
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
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 = new QWidget();
tab_4->setObjectName(QString::fromUtf8("tab_4"));
verticalLayout_3 = new QVBoxLayout(tab_4);
verticalLayout_3->setSpacing(6);
verticalLayout_3->setContentsMargins(11, 11, 11, 11);
verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));
frame_2 = new QFrame(tab_4);
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);
QMetaObject::connectSlotsByName(TestSizeClass);
}
To copy to clipboard, switch view to plain text mode
Thanks really much!
Bookmarks