我有一个包含2个组合框消息的Qt表单.第二个组合框消息取决于第一个组合框消息.我的意思是第二个组合框消息的日期取决于我在第一个组合框中选择的元素.在这一刻,我在第一个组合框中有不同的日期.但第二个组合框不起作用.我需要创建一个连接方法或什么?谢谢!欣赏!有人可以给我一个简短的例子吗?
这很简单.组合框会发出currentIndexChanged信号,该信号也会告诉您新的索引.编写一个接受整数的方法,并根据整数(组合框1中的选择索引)更改第二个组合框.
以下是一个工作示例中的一些代码片段.
窗口中的方法声明/任何类头:
public slots:
void setI1(int index);
Run Code Online (Sandbox Code Playgroud)
填充组合框1,连接信号,例如在构造函数中:
i1Box->addItem("Neutral", 0);
i1Box->addItem("2,856 K (Illuminant A, light bulb)", 2856);
// ...
connect(i1Box, SIGNAL(currentIndexChanged(int)),
this, SLOT(setI1(int)));
Run Code Online (Sandbox Code Playgroud)
方法的实施:
void ViewerWindow::setI1(int index) {
// either use index directly, or, as in this case we have items holding an int:
int i1 = i1Box->itemData(index).value<int>();
// use the value to change second combobox here
}
Run Code Online (Sandbox Code Playgroud)
如果它不能按预期工作,那么在应该调用的方法中打印一些调试输出总是有帮助的,以查看它在链中出错的位置.
参考:http://doc.qt.nokia.com/latest/signalsandslots.html
| 归档时间: |
|
| 查看次数: |
4554 次 |
| 最近记录: |