你能隐藏一个QGroupBox框架但保持它的内容可见吗?

问题描述 投票:3回答:5

我有一个QGroupBox。根据上下文,它的标题可能是redundent(显示在GUI的另一个地方),所以我需要做好像QGroupBox不在这里....但我必须保持它的内容可见(所以我不想打电话给QGroupBox::hide())!

我需要在运行时动态地执行此操作,并希望避免创建/销毁QGroupBox +重新显示它的内容....必须有一种更简单的方法来执行此操作。

到目前为止我尝试了什么:

QGroupBox可见:

enter image description here

  • QGroupBox::setTitle("")删除了文本。
  • QGroupBox::setFlat(true)使框架成为一条线。

我最终得到了这个:

enter image description here

还不错......但一条线仍然存在......有没有办法完全隐藏QGroupBox框架但保留其内容可见?

qt groupbox
5个回答
2
投票

你可以使用QFrame + QGridLayout(或一些更复杂的布局组合)+ QSS而不是QGroupBox

仅考虑QGroupBox,通过QSS的一个简单的解决方案可能是:

static const char kSavedTitle[] = "_savedTitle";
void hideBoxFrame(QGroupBox * box) {
  box->setProperty(kSavedTitle, box->title());
  box->setTitle(QString());
  box->setStyleSheet("border:none");
}
void showBoxFrame(QGroupBox * box) {
  box->setTitle(box->property(kSavedTitle).toString());
  box->setStyleSheet(QString());
}

2
投票

您可以从QGroupBox派生自己的Group Box并重新实现paintEvent()方法。应该很简单。原始的QGroupBox::paintEvent()看起来像这样:

void QGroupBox::paintEvent(QPaintEvent *)
{
    QStylePainter paint(this);
    QStyleOptionGroupBox option;
    initStyleOption(&option);
    paint.drawComplexControl(QStyle::CC_GroupBox, option);
}

您需要做的只是在绘制小部件之前修改样式选项:

void CMyGroupBox::paintEvent(QPaintEvent *)
{
    QStylePainter paint(this);
    QStyleOptionGroupBox option;
    initStyleOption(&option);

    // This should disable frame painting.
    option.features = QStyleOptionFrame::None;

    paint.drawComplexControl(QStyle::CC_GroupBox, option);
}

2
投票

这是一个通过交换小部件并重新创建子级来实现它的示例。它适用于任何有直接孩子的小部件,而不仅仅是QGroupBox。它需要特殊的案例处理小部件,如QScrollAreaQMainWindow,它们将子项包装在一个特殊的子小部件中。

有关以编程方式提升小部件的相关讨论,请参阅this question

screenshot of the example

// https://github.com/KubaO/stackoverflown/tree/master/questions/group-reparent-36603051
#include <QtWidgets>

/// Replaces the visible widget with a hidden widget, preserving the layout of the
/// children, and making the new widget visible.
void swapWidgets(QWidget * a, QWidget * b)
{
   auto src = a->isVisible() ? a : b;
   auto dst = a->isVisible() ? b : a;
   Q_ASSERT(dst->isHidden());
   /// Move the children to the destination
   dst->setLayout(src->layout());
   /// Replace source with destination in the parent
   auto layout = src->parentWidget()->layout();
   delete layout->replaceWidget(src, dst);
   /// Unparent the source, otherwise it won't be reinsertable into the parent.
   src->setParent(nullptr);
   /// Only the destination should be seen.
   src->hide();
   dst->show();
}

int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   QWidget w;
   QGridLayout wLayout{&w};
   QPushButton swapBtn{"Swap"};
   wLayout.addWidget(&swapBtn);

   QWidget noBox;
   QGroupBox box{"Group"};
   wLayout.addWidget(&box);
   QGridLayout boxLayout{&box};
   for (int i = 0; i < 16; ++i)
      boxLayout.addWidget(new QLabel(QString("Tr%1").arg(i)), i/8, i%8);

   swapBtn.connect(&swapBtn, &QPushButton::clicked, [&] { swapWidgets(&box, &noBox); });
   w.show();
   return app.exec();
}

2
投票

我的选择:

QGroupBox theBox;
theBox.setFlat(true);
theBox.setStyleSheet("border:0;");

0
投票

是的,您可以尝试其他选择。

你可以变形为一个QFrame,它将保持行为但是使容器无边界

您只需右键单击QDesigner中的Group Box,然后选择'Morph Into'选项以进行选择

© www.soinside.com 2019 - 2024. All rights reserved.