如何制作小部件的 QVector?
如何制作动态数量的小部件的 QVector
(或其他一些容器类),例如 Qt 4 中的 QPushButton
或 QComboBox
?
我在窗口类的构造函数中使用了以下内容:
QVector<QComboBox*> foo; // Vector of pointers to QComboBox's
现在我想用一些可以动态更改的控件来填充它:
for(int count = 0; count < getNumControls(); ++count) {
foo[count] = new QComboBox();
}
我已经搜索了几个小时试图找到这个问题的答案。 Qt 论坛提到制作 QPtrList
,但该类在 Qt4 中不再存在。 我稍后会尝试使用数组样式索引或 .at()
函数从每个中获取文本值。
我真的很欣赏一个声明、初始化和填充任何 QWidgets 的任何数据结构的示例(QComboBox
、QPushButton
等)
How do I make a QVector
(or some other container class) of a dynamic number of widgets, such as QPushButton
or QComboBox
in Qt 4?
I've used the following in my window class's constructor:
QVector<QComboBox*> foo; // Vector of pointers to QComboBox's
And now I want to fill it with some number of controls which can change dynamically:
for(int count = 0; count < getNumControls(); ++count) {
foo[count] = new QComboBox();
}
I've searched for hours trying to find the answer to this. The Qt forums mention making a QPtrList
, but that class no longer exists in Qt4.
I'd later try to get the text value from each using array-style indexing or the .at()
function.
I would really appreciate an example of declaring, initializing, and populating any data structure of any QWidgets
(QComboBox
, QPushButton
, etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给你:)
如果您只是想获得一个简单的示例,那么您的小部件有一个父级(用于上下文/清理)目的很重要。
希望这有帮助。
here you go :)
If you are trying just to get a simple example to work, its important that your widgets have a parent (for context / clean up) purposes.
Hope this helps.
这不会影响 foo 的大小。如果索引计数中尚不存在项目,则此操作将会失败。
请参阅 push_back 或 运算符<<,将项目添加到列表末尾。
稍后,要检索值:
This won't affect the size of foo. If there isn't already an item at index count, this will fail.
See push_back, or operator<<, which add an item to the end of the list.
Later, to retrieve the values: