【问题标题】:How to insert a dynamic multidimensional QComboBox into a LayOut如何将动态多维 QComboBox 插入到 LayOut
【发布时间】:2021-12-25 15:28:05
【问题描述】:

我正在尝试从动态多维 QComboBox 中插入多个 QComboBox,例如:

QComboBox **test = new QComboBox *[x];
test[x] = new QComboBox [y];

ui->QVBoxLayout->addWidget(test["one of x values"]["one of y values"]);

但这给了我一个错误:没有从 QComboBox 到 *QWidget 的可行转换。

使用:

  QComboBox *test = new QComboBox;
  ui->QVBoxLayout->addWidget(test);

工作得很好。

我的情况是(这是示例):

  for(int tmp = 1; fieldAmount >= tmp; tmp++){
            //fieldAmount is the number of fields presented in a table that was loaded in from a file
            combobox1[currentTable] = new QComboBox [tmp];
            ui->verticalLayout_2->addWidget(&combobox1[currentTable][tmp]); //Gives the seg fault

  }

我的案例所做的是基于我加载的文件,查找我将拥有的表的数量以及需要输入的值的数量。这就是为什么我需要一个动态的多维 QComboBox。

我不正确的语法(或执行顺序)是什么? 如果这是重复的,那么我很抱歉,但我找不到它已经在这里发布的问题。

【问题讨论】:

  • ui->QVBoxLayout->addWidget(test["one of x values"]["one of y values"]); 更改为ui->QVBoxLayout->addWidget(&test["one of x values"]["one of y values"]);
  • 感谢您指出这一点,但这并不能解决主要问题。
  • QComboBox **test = new QComboBox *[x]; test[x] = new QComboBox [y]; 这通过越界访问索引表现出未定义的行为。 test 的有效索引为 0 到 x-1
  • @Igor Tandetnik 你是对的,但我忘了在我的 .h 中将其初始化为 QComboBox **combobox1 = NULL; 所以我在加载 sqlite3 文件时为组合框分配空间
  • 如果您认为您的解决方案对其他人有用,请将其作为“答案”发布在下方(并按旁边的复选标记接受它,将问题标记为已解决)。

标签: c++ qt qcombobox


【解决方案1】:
combobox1[currentTable] = new QComboBox [fieldAmount];
for(int tmp = 0; fieldAmount > tmp; tmp++){

ui->verticalLayout_2->addWidget(&combobox1[currentTable][tmp]);

}

fieldAmount 在哪里

int fieldAmount = (SQLDataBaseContet->record()).count()-1; // -1 as offset because of id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2019-12-03
    • 1970-01-01
    • 2021-05-28
    相关资源
    最近更新 更多