如何以包装的方式初始化布局,从而减少布局之间的空间
How to initialize layouts in a packed way, reducing space between layouts?
我有几个qhboxlayout都包含在qvboxlayout中。
QHBoxLayout *hb1 = new QHBoxLayout;
// ...
QHBoxLayout *hb2 = new QHBoxLayout;
// ...
QWidget *hb1_layout = new QWidget;
hb1_layout->setLayout(hb1);
QWidget *hb2_layout = new QWidget;
hb2_layout->setLayout(hb2);
// ...
QVBoxLayout *ps = new QVBoxLayout;
ps->addWidget(hb1_layout);
ps->addWidget(hb2_layout);
ps->addWidget(hb3_layout);
ps->addWidget(hb4_layout);
我要做的是将小部件在每个qhboxlayout中打包在接近qhboxlayout下方的小部件的每个qhboxlayout中。即,水平布局之间每行小部件之间都有很多空间。
调整窗口的大小时,需要有一个最小尺寸的小部件不会超越。但是调整大小时,它们会均匀分布。
如果我调整了窗口大小,我可以将其看起来像我想要的。但是我不知道如何首先将窗口的编程初始化为正确的大小。当我启动应用程序时,我必须通过调整窗口大小以使小部件看起来很好地包装。
想法?
首先将窗口的编程初始化为正确的大小。
使用adjustSize();
设置窗口尺寸适合您的小部件。
我要做的是在每个qhboxlayout中打包小部件 接近QHBoxLayout下一个下向下的小部件。
QVBoxLayout
不会直接做到这一点。,而是使用 QFormLayout
,行不会单独散开,行只扩展到其内容。您可以将行之间的间距调整为合适的int
值,QFormLayout::setVerticalSpacing(40);
..和列之间QFormLayout::setHorizontalSpacing(40);
,恕我直言,没有任何点操纵小部件的尺寸或每次窗口的尺寸都会调整大小。.如果您的窗口有少数小部件。.p>样本:
QFormLayout * const formlayout = new QFormLayout(this->ui->centralWidget);
QHBoxLayout *hb1 = new QHBoxLayout;
QHBoxLayout *hb2 = new QHBoxLayout;
QHBoxLayout *hb3 = new QHBoxLayout;
QLabel *hb1_layout = new QLabel("Short Text1");
hb1_layout->setStyleSheet("background-color: rgb(212, 192, 255)");
hb1_layout->setFixedSize(100,100);
hb1->addWidget(hb1_layout);
formlayout->addRow(hb1);
//
QLabel *hb2_layout = new QLabel("Long Long Long Text");
hb2_layout->setFixedSize(200,200);
hb2_layout->setStyleSheet("background-color: rgb(25, 255, 192)");
hb2->addWidget(hb2_layout);
formlayout->addRow(hb2);
//
QLabel *hb3_layout = new QLabel("Long Long Long Text too n Long Long Long Text too n Long Long Long Text too");
hb3_layout->setStyleSheet("background-color: rgb(100, 25, 25)");
hb3_layout->setFixedSize(300,300);
hb3->addWidget(hb3_layout);
formlayout->addRow(hb3);
//
formlayout->setHorizontalSpacing(40); // between columns
formlayout->setVerticalSpacing(10); // between rows
adjustSize(); // Window to contents
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 如何使用C/C++在MacOSX中获得键盘布局
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- libstdc++的make_shared布局在gcc 4.x和gcc 6.x之间是否发生了变化?
- 如何以包装的方式初始化布局,从而减少布局之间的空间
- 我可以合法地在布局兼容的标准布局类型之间重新解释(_C)吗
- LLVM 内存布局中的矢量和数组之间的任何差异
- 编译器之间的浮点二进制布局
- 具有相同布局的不同类型之间的c++转换
- C和c++之间的内存布局兼容性
- QLayoutItem和QWidget之间的Qt布局关系