Qt5中如何正确使用QGridLayout将Widgets并排添加
How to properly use QGridLayout to addWidgets next to each other in Qt5
我在尝试将两个布局并排放置时遇到了一个小问题。
到目前为止我得到的实际结果如下:
但它应该如下所示:
我错过了什么?
在我为此使用的一小段代码下方:
MyViz::MyViz(QWidget *parent) : QWidget(parent)
{
// Qt Components set up......
QGridLayout* control_layout = new QGridLayout();
control_layout->addWidget(thickness_label, 0, 0);
control_layout->addWidget(thickness_slider, 0, 1);
control_layout->addWidget(cell_size_label, 1, 0);
control_layout->addWidget(cell_size_slider, 1, 1);
control_layout->addWidget(thickness_label_second_renderer, 0, 2);
control_layout->addWidget(thickness_slider_second_renderer, 0, 3);
control_layout->addWidget(cell_size_second_renderer_label, 1, 2);
control_layout->addWidget(cell_size_second_renderer_slider, 1, 3);
// construct lay-out and render panels next to each other
render_panel = new rviz::RenderPanel();
render_panel2 = new rviz::RenderPanel();
QVBoxLayout* main_layout = new QVBoxLayout;
main_layout->addLayout(control_layout);
main_layout->addWidget(render_panel);
main_layout->addWidget(render_panel2);
// set the top-leve layout for this MyViz widget
setLayout(main_layout);
// other operation....
}
问题是您正在创建一个 QVBoxLayout
('V' 垂直)并在其中放置您的 QGridLayout
和 RenderPanel
实例。只需将所有内容放入 QGridLayout
...
MyViz::MyViz(QWidget *parent) : QWidget(parent)
{
// Qt Components set up......
QGridLayout* control_layout = new QGridLayout();
control_layout->addWidget(thickness_label, 0, 0);
control_layout->addWidget(thickness_slider, 0, 1);
control_layout->addWidget(cell_size_label, 1, 0);
control_layout->addWidget(cell_size_slider, 1, 1);
control_layout->addWidget(thickness_label_second_renderer, 0, 2);
control_layout->addWidget(thickness_slider_second_renderer, 0, 3);
control_layout->addWidget(cell_size_second_renderer_label, 1, 2);
control_layout->addWidget(cell_size_second_renderer_slider, 1, 3);
// construct lay-out and render panels next to each other
render_panel = new rviz::RenderPanel();
render_panel2 = new rviz::RenderPanel();
/*
* At this point the QGridLayout has 4 occupied columns. Make
* each rviz::RenderPanel instance span 2 columns.
*/
control_layout->addWidget(render_panel, 2, 0, 1, 2);
control_layout->addWidget(render_panel2, 2, 2, 1, 2);
// set the top-level layout for this MyViz widget
setLayout(control_layout);
// other operation....
}
注意到传递给QGridLayout::addWidget
的第四个和第五个参数分别是行和列的跨度。
我在尝试将两个布局并排放置时遇到了一个小问题。
到目前为止我得到的实际结果如下:
但它应该如下所示:
我错过了什么?
在我为此使用的一小段代码下方:
MyViz::MyViz(QWidget *parent) : QWidget(parent)
{
// Qt Components set up......
QGridLayout* control_layout = new QGridLayout();
control_layout->addWidget(thickness_label, 0, 0);
control_layout->addWidget(thickness_slider, 0, 1);
control_layout->addWidget(cell_size_label, 1, 0);
control_layout->addWidget(cell_size_slider, 1, 1);
control_layout->addWidget(thickness_label_second_renderer, 0, 2);
control_layout->addWidget(thickness_slider_second_renderer, 0, 3);
control_layout->addWidget(cell_size_second_renderer_label, 1, 2);
control_layout->addWidget(cell_size_second_renderer_slider, 1, 3);
// construct lay-out and render panels next to each other
render_panel = new rviz::RenderPanel();
render_panel2 = new rviz::RenderPanel();
QVBoxLayout* main_layout = new QVBoxLayout;
main_layout->addLayout(control_layout);
main_layout->addWidget(render_panel);
main_layout->addWidget(render_panel2);
// set the top-leve layout for this MyViz widget
setLayout(main_layout);
// other operation....
}
问题是您正在创建一个 QVBoxLayout
('V' 垂直)并在其中放置您的 QGridLayout
和 RenderPanel
实例。只需将所有内容放入 QGridLayout
...
MyViz::MyViz(QWidget *parent) : QWidget(parent)
{
// Qt Components set up......
QGridLayout* control_layout = new QGridLayout();
control_layout->addWidget(thickness_label, 0, 0);
control_layout->addWidget(thickness_slider, 0, 1);
control_layout->addWidget(cell_size_label, 1, 0);
control_layout->addWidget(cell_size_slider, 1, 1);
control_layout->addWidget(thickness_label_second_renderer, 0, 2);
control_layout->addWidget(thickness_slider_second_renderer, 0, 3);
control_layout->addWidget(cell_size_second_renderer_label, 1, 2);
control_layout->addWidget(cell_size_second_renderer_slider, 1, 3);
// construct lay-out and render panels next to each other
render_panel = new rviz::RenderPanel();
render_panel2 = new rviz::RenderPanel();
/*
* At this point the QGridLayout has 4 occupied columns. Make
* each rviz::RenderPanel instance span 2 columns.
*/
control_layout->addWidget(render_panel, 2, 0, 1, 2);
control_layout->addWidget(render_panel2, 2, 2, 1, 2);
// set the top-level layout for this MyViz widget
setLayout(control_layout);
// other operation....
}
注意到传递给QGridLayout::addWidget
的第四个和第五个参数分别是行和列的跨度。