doLayout:如何在另一个小部件上设置Geometry
doLayout: how to setGeometry on top of another widget?
我想在这里创建类似的自定义布局:http://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-flowlayout-cpp.html
我想要一些方法把复选框放在自定义按钮的顶部。目前有
setGeometry(QRect(QPoint(...
按钮和复选框的方法,但无论我是为按钮还是checkobox先做,复选框都会出现在按钮下面,我看不到/点击它。
我怎样才能把复选框放在按钮的顶部?
只需将复选框设为按钮的子项,并相对于按钮调用setGeometry。孩子们总是被吸引到父母的前面。
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
QCheckBox checkBox(&button);
checkBox.setGeometry(button.rect());
checkBox.show();
无需将复选框放入布局中。
我刚刚制作了这个片段来检查按钮顶部的复选框,它对我有效。
#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>
#include <QCheckBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
QCheckBox checkBox(&w);
checkBox.setGeometry(30,30,50,50);
checkBox.show();
w.show();
return a.exec();
}
如果你想改变"养育子女"的顺序,并希望复选框仍然在顶部:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QCheckBox checkBox(&w);
checkBox.setGeometry(30,30,50,50);
checkBox.show();
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
checkBox.setParent(NULL);
checkBox.setParent(&w);
w.show();
return a.exec();
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 嵌套在类中时无法设置成员数据
- 需要帮助设置在C++中使用的Potrace
- 如何在自删除后将对象设置为nullptr
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何在Ubuntu中使用cmake设置qt4
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 如何在boost beast http请求中设置http头
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 如何在24位SDL_Surface上设置像素的颜色
- std::设置自定义比较器
- 如何设置一个范围来提取我想要获得的信息
- 如何在C/C++中用FD_set Unix设置套接字文件描述符
- 通过选项卡的文本设置QTabWidget顺序
- 如何在 osg::Geometry 上设置光泽度,而无需在 OpenSceneGraph 中设置材质
- doLayout:如何在另一个小部件上设置Geometry