使QHBoxLayout在QWidget的实例中可访问
Make a QHBoxLayout accesible in an Instance of a QWidget
我想子类化一个QWidget…
Widget::Widget(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ImgWidget)
{
// Is this the right place or does it need to be outside like: Widget::layout...
QHBoxLayout *hLayout = new QHBoxLayout;
...
hLayout->addWidget( someWidget );
this->setLayout(hLayout);
}
…然后在实例中,我想在布局中添加一个小部件,比如
Widget *widget = new Widget();
...
widget->hLayout->addWidget( someOtherWidget );
在Python中等效的可以通过'self'关键字来完成,比如self.layout = QHBoxLayout()
,但我认为它不能用this
关键字
但更普遍的是…在c++中最好的方法是什么?因为我经常用pyqt
任何QWidget
都可以通过layout
函数提供对布局的访问。
Widget* widget = new Widget();
QLayout* layout = widget->layout();
我不知道你为什么要从外部修改小部件的布局,但在这一点上,你需要了解实际的布局类型。有了布局类型的知识,你就可以强制转换和使用它。
QHBoxLayout* hLayout = qobject_cast<QHBoxLayout>(layout);
实现函数addSubWidget (QWidget* aubWidget),其余的代码是ok的,并使hLayout私有。或者,像@James建议的那样,使用带强制类型转换的layout函数。
在c++中,只是推荐,而不是强制使用this->hLayout,以确保您不会将其与同名的局部变量遮蔽
相关文章:
- 初始化指向类实例的智能指针并访问其方法
- 如何从实现接口的模板化类实例访问结构
- 访问同级类实例的成员
- 从模板参数包实例化的访问类实现
- 如何使用包含内部类的类实例有效地从内部类访问成员?
- 限制多模板参数朋友函数可访问的类实例的范围
- 为什么c++允许成员函数定义中实例的私有成员访问
- 返回实例变量的c++方法可以访问变量中的数据,但不能更改它,但在编译时不会生成错误
- 当对象为值时访问实例方法<map>
- 访问实例化类的变量/函数
- 访问实例外部的函数指针
- 访问实例化类的变量
- 从另一个模板类访问实例变量
- C++派生模板类:访问实例的受保护成员
- C++通过类作用域访问实例的成员
- 如何从另一个函数访问实例化的类
- 需要学习如何通过指针正确访问实例变量"this"就像在 Java 中一样
- 如何通过std::shared_ptr访问实例成员变量
- 通过Openmp任务访问实例变量(隐式firstprivate)时出现分段错误
- 从回调函数访问实例