如何在QGridLayout中获取QComboBox的当前文本?
How Can I get the Current Text of a QComboBox in QGridLayout?
目前我有一个创建槽,当按下按钮时会调用它。Slot 函数需要从其上方 QGridLayout 中的组合框中获取所有数据。在上面的项目中,并非所有项目都有QComboBox。其中一些是QLineEdit,另一些是QLabel。我的QgridLayout称为ui.testgridLayout。
for (int i = 0; i < ui.testgridLayout->rowCount(); i++)
{
for (int j = 0; j < ui.testgridLayout->columnCount(); j++)
{
QLayoutItem *item= ui.testgridLayout->itemAtPosition(i, j);
if (item) {
if (strcmp(item->widget()->metaObject()->className(), "QComboBox")==0) {
QString objName= item->widget()->objectName();
QComboBox* cb =ui.testgridLayout->findChild<QComboBox*>(objName);
string text = cb->currentText().toLocal8Bit().constData();
}
}
}
}
目前,这会在 ConfigFileCreation.exe 中返回 0x00007FFB107DCC8A (Qt5Widgets.dll( 处的未处理异常: 0xC0000005:访问冲突读取位置0x0000000000000008。任何帮助或建议将不胜感激。
问题是你认为放置在布局中的小部件是布局的子项,但不,这些小部件是建立布局的小部件的子项,所以在你的代码中,"cb"是一个导致问题的空指针。解决方案是强制转换并验证指针是否有效:
for (int i = 0; i < ui.testgridLayout->rowCount(); i++){
for (int j = 0; j < ui.testgridLayout->columnCount(); j++){
if (QLayoutItem *item= ui.testgridLayout->itemAtPosition(i, j)) {
if (QComboBox* cb = qobject_cast<QComboBox*>(item->widget())) {
string text = cb->currentText().toLocal8Bit().constData();
}
}
}
}
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 在QComboBox或QListWidget中设置所选元素的样式文本?
- 如何在QGridLayout中获取QComboBox的当前文本?
- Qcombobox 如何更改行编辑的文本
- 如何使QComboBox的文本加粗,而不是列表项?
- 将文本保存在QCOMBOBOX到SQL表
- Qcombobox设置标题文本,无论项目如何
- 可编辑的QComboBox:将编辑文本与项目文本同步
- 更改委托中的QComboBox项目文本
- Qt:如何在任意索引(不是当前选定的项目)处获取QComboBox项目文本
- 设置QComboBox的占位符文本
- 信号编辑文本已更改为可编辑的QComboBox设置光标位置在设置编辑文本之后的末尾
- Qt -设置不可编辑的QComboBox的显示文本
- 我如何在QComboBox中移动文本上方的图标
- 如何在QComboBox中裁剪文本或如何获得其实际宽度