How to get the dimensions of the widgets
-
Hi All,
I want to know how to get the dimensions of the widgets.
Through following codes, I get the default dimension of my widget (height=480,width=640)
QTableWidget *first_table=new QTableWidget();
tableLayout->addWidget(first_table,0,0);
int height =first_table->height();
int width = first_table->width();
qDebug()<<"height is"<<height;
qDebug()<<"width is"<<width;After I did some operations like adding spacing in horizontal and vertical directions, then used height() and width() to get the dimensions of the widgets, it still gives the default dimensions.
How to get the exact dimension of any widget?
Thanks.
-
@Swati777999 said in How to get the dimensions of the widgets:
it still gives the default dimensions.
Where do you read the dimensions? If you do it in constructor before showing the parent widget you will not get correct geometry. The reason is that widgets are sized properly when they are shown, not before.
-
@Swati777999 Are your widgets in a layout?
Are you doing these manipulations in constructor? -
@Swati777999
@jsulm already told you:Where do you read the dimensions? If you do it in constructor before showing the parent widget you will not get correct geometry. The reason is that widgets are sized properly when they are shown, not before.
This is the correct answer. Please take the time to consider people's answers and act on them.
Again: widgets do not report any reliable size until they shown, as Qt does not do the calculations till it needs to as & when it displays them.
-
@Swati777999 said in How to get the dimensions of the widgets:
No, I am not doing these manipulations in the constructor.
OK, then tell us where you are looking at these sizes, and how it relates to the shown state of the widgets!
-
I've performed the following manipulations to change the size
first_table->setMinimumSize(100,100); first_table->setRowCount(8); first_table->setColumnCount(2); int height =first_table->height(); int width = first_table->width(); qDebug()<<"height is"<<height; qDebug()<<"width is"<<width;
It gives the result as height=480 ,width=640
-
@Swati777999 said in How to get the dimensions of the widgets:
first_table->setMinimumSize(100,100); first_table->setRowCount(8); first_table->setColumnCount(2); // <-- ERROR HERE! You did not wait until the changes have been applied. // Therefore, the lines below will give you wrong numbers. int height =first_table->height(); int width = first_table->width();
-
@Swati777999 said in How to get the dimensions of the widgets:
How can I introduce the time for waiting after manipulating the dimension of widget?
Have you tried @JoeCFD's suggestion?
You cannot call width()/height() in the same function that calls manipulators like
first_table->setMinimumSize(100,100)
. The changes will not be applied until all of your functions have returned -- that's when your application's Event Loop can process the changes. -
You can try this
ui->YourWidget->geometry();
it's Return Your Widget Location And Size;
-
@Ketan__Patel__0011 said in How to get the dimensions of the widgets:
You can try this
ui->YourWidget->geometry();
it's Return Your Widget Location And Size;
This won't work before the widget is shown.