C++Qt用Qgridlayout让两个button在两个Qlistwidget中间竖向紧挨着排列
时间: 2023-11-27 09:50:13 浏览: 34
可以使用一个 QVBoxLayout 包含两个 QListWidget 和 QHBoxLayout,将两个 QPushButton 添加到 QHBoxLayout 中,然后将 QHBoxLayout 添加到 QVBoxLayout 中。具体代码如下:
```cpp
QListWidget *listWidget1 = new QListWidget(this);
QListWidget *listWidget2 = new QListWidget(this);
QPushButton *button1 = new QPushButton("Button 1", this);
QPushButton *button2 = new QPushButton("Button 2", this);
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addWidget(button1);
hLayout->addWidget(button2);
hLayout->setSpacing(0);
hLayout->setMargin(0);
QVBoxLayout *vLayout = new QVBoxLayout(this);
vLayout->addWidget(listWidget1);
vLayout->addLayout(hLayout);
vLayout->addWidget(listWidget2);
```
这样布局的效果是,两个 QListWidget 在垂直方向上紧挨着排列,中间有一个 QHBoxLayout,里面包含两个 QPushButton,两个 QPushButton 在中间位置竖向排列。如果需要调整 QPushButton 的大小和位置,可以通过设置 QHBoxLayout 的属性来实现。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)