有没有办法在 Qt 中创建 UI 变量的别名?
这个变量
ui->combobox->currentIndex()
例如成为
index
所以每当我在我的代码中调用 index 时,我都会得到 ui->combobox->currentIndex() 的值,即使她在两次调用中发生了变化。
我试过了,但我总是得到相同的值,即初始化期间的值。
int *index = 0;
index = (int *)ui->combobox->currentIndex() // equals -1 this time;
(int)index; //always return -1 but if I do ui->combobox->currentIndex() it returns 0;
目标是减少超长语句的长度。
谢谢:)
最佳答案
可能最好的方法是添加一个返回当前索引的内联函数:
inline int MainWindow::myIndex() const
{
return ui->combobox->currentIndex();
}
然后您可以在需要该值时调用 myIndex()
。
或者简单地在您使用它的语句之前分配一个局部变量:
int const index = ui->combobox->currentIndex();
关于c++ - C++/Qt 中变量的别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18121235/