我有个-也许有点不寻常的-问题:
我有一个QTabWidget。应用程序从数据源读取记录。记录包含一个“pos”和“pos”字段(除其他信息外),应用程序应为每条记录在网格位置(x,y)的选项卡页上放置一个按钮。这些按钮有一个“固定”大小,由用户选择,这是通过一个Config对象从ini文件中读取的。如果这个“按钮网格”变得太大,对于选项卡页的空间,滚动条将出现。
首先,我认为这是非常简单和直截了当的。就像“把一个QScrollarea放在标签页上,把一个QGridLayout放到这个QScrollArea中。就是这样”。但后来我意识到这绝不是简单的事情。
据我所知,Qt的“布局和养育”正好相反。(如果我错了,请纠正我!)“通常”,周围的小部件大小它包含的布局管理器(例如QGridLayout),然后布局管理器大小它包含的孩子。
但我认为,我需要“相反的方向”。我有固定大小的按钮,需要由m列组成n行的网格,这将导致整个网格布局的大小。如果整个布局不合适,scrollarea将激活它的滚动条。
所以,我现在有点迷上了这个谜,想知道如何“正确”地做到这一点。也许有人能把我踢向正确的方向?
非常感谢!
发布于 2017-03-10 14:34:02
这你想要什么?如果是,尝试在滚动区域设置小部件,而不是布局。在小部件上按下按钮。
然后,如果您的意思是pos和pos是绝对的,则不需要在小部件上设置任何布局。只需创建一个子按钮并根据需要设置它们的几何图形:
// Suppose we read this info from a config file
int pbWidth = 150, pbHeight = 30;
// and this one from a datasource
QVector<QPoint> positions;
positions.push_back(QPoint(10, 10));
positions.push_back(QPoint(50, 50));
positions.push_back(QPoint(210, 30));
QTabWidget *tabWidget = new QTabWidget();
QScrollArea *scrollArea = new QScrollArea();
QWidget *scrollAreaWidget = new QWidget();
// We put a container widget inside the scroll area
scrollArea->setWidget(scrollAreaWidget);
// then we add the scroll area as the tab
tabWidget->addTab(scrollArea, tr("Tab 1"));
// Now let's put some push buttons on the widget
for (QVector<QPoint>::ConstIterator i = positions.cbegin();
i != positions.cend(); i++)
{
QString text = tr("Button at %1x%2").arg(i->x()).arg(i->y());
// We have to pass the parent to the constructor
// to place pb on the widget
QPushButton *pushButton = new QPushButton(text, scrollAreaWidget);
// Here we set position and size
pushButton->setGeometry(i->x(), i->y(), pbWidth, pbHeight);
}
// We have no layout on the widget, so don't forget to adjust size
scrollAreaWidget->adjustSize();
https://stackoverflow.com/questions/42727573
复制相似问题