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

前端 未结 10 2341
醉梦人生
醉梦人生 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:38

    You can use a QStackedWidget. Put your button on the first page, a blank QWidget on the second, and change the page index to make your button vanish while retaining its original space.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 10:44

    I had a similar problem and I ended up putting a spacer next to my control with a size of 0 in the dimension I cared about and an Expanding sizeType. Then I marked the control itself with an Expanding sizeType and set its stretch to 1. That way, when it's visible it takes priority over the spacer, but when it's invisible the spacer expands to fill the space normally occupied by the control.

    0 讨论(0)
  • 2020-12-01 10:45

    May be QWidget::setWindowOpacity(0.0) is what you want? But this method doesn't work everywhere.

    0 讨论(0)
  • 2020-12-01 10:47

    This problem was solved in Qt 5.2. The cute solution is:

    QSizePolicy sp_retain = widget->sizePolicy();
    sp_retain.setRetainSizeWhenHidden(true);
    widget->setSizePolicy(sp_retain);
    

    http://doc.qt.io/qt-5/qsizepolicy.html#setRetainSizeWhenHidden

    0 讨论(0)
  • 2020-12-01 10:47

    Try void QWidget::erase (). It works on Qt 3.

    0 讨论(0)
提交回复
热议问题