How to make a Qt widget invisible without changing the position of the other Qt widgets?

前端 未结 10 2403
醉梦人生
醉梦人生 2020-12-01 10:09

I\'ve got a window full of QPushButtons and QLabels and various other fun QWidgets, all layed out dynamically using various QLayout objects... and what I\'d lik

10条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 10:41

    I've 3 solutions in my mind:

    1) Subclass your QWidget and use a special/own setVisible() replacement method witch turns on/off the painting of the widget (if the widget should be invisible simply ignore the painting with an overridden paintEvent() method). This is a dirty solution, don't use it if you can do it other ways.

    2) Use a QSpacerItem as a placeholder and set it's visibility to the opposite of the QWidget you want to hide but preserve it's position+size in the layout.

    3) You can use a special container widget (inherit from QWidget) which gets/synchronizes it's size based on it's child/children widgets' size.

提交回复
热议问题