【发布时间】:2017-05-24 02:29:38
【问题描述】:
我可以使用 Qt 来做以下事情吗:
我需要两个QFrames,一个(孩子)在另一个(父母)的中间,当我改变父母QFrame时,孩子也会调整大小。
我怎样才能设置布局或其他可以这样做的东西。
如下图,黄色为child,白色为parent。
【问题讨论】:
-
您创建一个
QFrame对象,为其设置布局,然后在该布局中添加另一个QFrame对象。究竟是什么问题?
我可以使用 Qt 来做以下事情吗:
我需要两个QFrames,一个(孩子)在另一个(父母)的中间,当我改变父母QFrame时,孩子也会调整大小。
我怎样才能设置布局或其他可以这样做的东西。
如下图,黄色为child,白色为parent。
【问题讨论】:
QFrame 对象,为其设置布局,然后在该布局中添加另一个QFrame 对象。究竟是什么问题?
好吧,让我们成为两个QFrame...但是第一个答案更好。
#include <QApplication>
#include <QFrame>
#include <QHBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFrame f1;
f1.setStyleSheet( "background: white" );
QFrame f2;
f2.setStyleSheet( "background: yellow" );
QHBoxLayout l( &f1 );
l.setContentsMargins( 10, 10, 10, 10);
l.addWidget( &f2 );
f1.resize( 200, 200 );
f1.show();
return a.exec();
}
【讨论】:
我试图找到如下的一种解决方案。
QFrame *parent_frame = new QFrame(this);//this means parent_frame's parent widget
QFrame *child_frame = new QFrame(parent_frame);
child_frame ->setFixedSize(parent_frame->geometry().width() - 4, parent_frame->geometry().height() - 4);
child_frame ->move(2,2);
并重写parent_frame的父widget的resizeEvent事件,做同样的事情
child_frame ->setFixedSize(parent_frame->geometry().width() - 4, parent_frame->geometry().height() - 4);
child_frame ->move(2,2);
在执行 this.show() 之后唯一需要注意的是执行第一个 setfixedsize,以便它可以获得正确的宽度和高度。
【讨论】:
这样的事情怎么样?
#include <QApplication>
#include <QFrame>
namespace {
static const auto STYLE_SHEET = "QFrame#MyFrame {border: 10px solid white; background: yellow}";
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFrame f;
f.setObjectName( QStringLiteral( "MyFrame" ) );
f.setStyleSheet( STYLE_SHEET );
f.show();
return a.exec();
}
【讨论】:
QFrame。 OP询问嵌套QFrames。