是否可以将 QCombobox 复制到另一个 QCombobox
我使用 Qt Designer 来构建我的 UI 布局。在布局上,我有一个名为 cb_fac_cd
的组合框。在我的代码中,我有一个函数可以根据我尝试构建的列表自动构建组合框。我在数据库中定义了很多这样的列表,这个函数会输出一个 QComboBox。
不幸的是,到目前为止我只使用这个函数将cellWidgets添加到QTableWidgets。它在那里完美地工作。现在我想填充这个预先存在的组合框。
看似简单的一个 self.ui.cb_fac_cd = makeComboBox('FACILITIES')
不起作用。我可以看到该函数照常返回 QComboBox,但 cb_fac_cd 组合框仍未填充。
如何将返回的组合框复制或分配给 Qt Designer 中的一个构建?
我正在使用 PyQt,但这应该没有任何区别。
I use Qt Designer to build my UI layout. On the layout, I have a combobox named cb_fac_cd
. In my code I have a function that will automatically build a combobox based on the list I'm attempting to build. I have a lot of these lists defined in the database and this function spits out a QComboBox.
Unfortunately, until now I've only used this function to add cellWidgets to QTableWidgets. It works perfectly there. Now I want to populate this preexisting combobox.
It seems that a simpleself.ui.cb_fac_cd = makeComboBox('FACILITIES')
doesn't work. I can see that the function returns the QComboBox as usual, but the cb_fac_cd combobox remains unpopulated.
How can I copy or assign the returned combobox to the one build in Qt Designer?
I am using PyQt, but that shouldn't make any difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,您无法替换 Qt 的 UIC 机制生成的 .h 文件中的对象(尽管我现在无法 100% 确认)。
在这种情况下,我通常做的是在 ui 文件中保留一个空布局,然后执行以下操作:
(请注意,我使用的是 Qt/C++ 语法,因为我不知道 pyqt 语法,但我认为您会明白的)
此外,如果您的程序可能,请考虑在 makeComboBox 函数中使用枚举而不是字符串。这通常更快更安全。
As far as I know, you cannot replace objects which are part of the .h files generated by Qt's UIC mechanism (though I cannot confirm that 100% right now).
What I usually do in such cases is to have an empty layout in the ui file and then do the following:
(Note that I'm using Qt/C++ syntax as I don't know the pyqt syntax but I think you will get the idea)
Additionally, if possible for your program, consider using an enumeration rather than a string for your makeComboBox function. This is usually faster and safer.
听起来您应该
It sounds like you should either
您可以将模型从您的 Ui 更改为从其他组合框获得的模型:
但是对于该部分:
如果数据来自数据库,您可能需要查看 QSqlRelationalTableModel 以避免手动执行此操作。
You can change the model from you Ui to the one you get from the other combobox:
But for that part:
If the data comes from a database, you might want to look at QSqlRelationalTableModel to avoid doing that manually.