For integer $n \ge 1$, let $[n]$ be a short hand for the interval of integers $\{ 1, 2,\ldots, n \}$.
Let $\{ s_1, s_2, \ldots, s_p \}$ be the set of sides of a bunch of squares that cover a rectangle of dimension $w \times h$.
Since $\mathbb{R}$ is a vector space over $\mathbb{Q}$, there is a hamel basis $E$ of $\mathbb{R}$ over $\mathbb{Q}$. Every real number can be uniquely expressed as a finite linear combination of elements from $E$ with rational coefficients.
There will be finitely many of $e \in E$ that appear in the expansion of $s_1, \ldots, s_p$.
Let $e_1, \ldots, e_q \in E$ be those appear in expansion of some $s_i$.
There will be $p \times q$ coefficients $\alpha_{ij} \in \mathbb{Q}, (i,j) \in [p] \times [q]$ such that
$$s_i = \sum_{j=1}^q \alpha_{ij} e_j\quad\text{ for } i \in [p]$$
Furthermore, for each $j \in [q]$, there is some $i \in [p]$ with $\alpha_{ij}\ne 0$.
Rescale $e_i$ if necessary, we can assume all $\alpha_{ij} \in \mathbb{Z}$.
Under this setting, it is easy to see we can find integers $w_j, h_j \in \mathbb{Z}, j \in [q]$ such that
$$w = \sum_{j=1}^q w_j e_j\quad\text{ and }\quad h = \sum_{j=1}^q h_j e_j$$
For any $j \in [q]$, define function $f_j : [0,w] \times [ 0, h ] \to \mathbb{R}$ by $f_j(x,y) = \frac{\alpha_{ij}}{s_i}$ whenever $(x,y)$ is covered by a square of side $s_i$.
Aside from a set of measure zero, $f_j$ is well defined. It is a piecewise constant function and integrable over $[0,w]\times[0,h]$. We can evaluate their integral over $[0,w]\times [0,h]$ in two different orders.
Aside from a finite choice of $y_0$, the line $y = y_0$ cut through
finitely many squares "normally". Let $s_{i_1}, s_{i_2}, \ldots, s_{i_r}$ be the sides of the squares it cut through. We have
$$\int_0^w f_j(x,y_0) dx
= \sum_{k=1}^r \int_{\sum_{\ell=1}^{k-1} s_{i_\ell}}^{\sum_{\ell=1}^{k} s_{i_\ell}}\frac{\alpha_{i_\ell j}}{s_{i_\ell}} dx
= \sum_{k=1}^r \alpha_{i_\ell j}
\in \mathbb{Z}
$$
Notice
$$\sum_{j=1}^q e_j \int_0^w f_j(x,y_0) dx =
\int_0^w \sum_{j=1}^q e_j f_j(x,y_0) dx = \int_0^w dx = w$$
We obtain
$$\sum_{j=1}^q \left(\sum_{k=1}^r \alpha_{i_\ell j}\right)e_j
= w = \sum_{j=1}^q w_j e_j$$
Since $e_j$ are linear independent over $\mathbb{Q}$, we obtain
$$\int_0^w f_j(x,y_0) dx = \sum_{k=1}^r \alpha_{i_\ell j} = w_j$$
From this, we can deduce
$$\int_0^h\int_0^w f_j(x,y) dx dy = w_j h$$
By a similar argument, we have
$$\int_0^w\int_0^h f_j(x,y) dy dx = h_j w$$
Since these functions are integrable, we have
$$w_j h = \int_0^h\int_0^w f_j(x,y) dx dy = \int_0^w\int_0^h f_j(x,y) dy dx = h_j w$$
Since $w \ne 0$, some $w_j \ne 0$. Let's say $w_1 \ne 0$, we have
$w_1 h = h_1 w \implies h_1 \ne 0$. As a result,
$$\frac{w}{h} = \frac{w_1}{h_1} \in \mathbb{Q}$$