【发布时间】:2017-12-07 08:56:14
【问题描述】:
我在Qt中做一个小游戏,尝试用WASD来移动我的主角(QGraphicsPixmapItem)。地图很大,所以我使用 QComboBox 来改变场景的比例。
游戏是这样的:
简单的游戏
我是如何塑造主角的:
protagonist = new MyProtagonist();
protagonist->setFlag(QGraphicsItem::ItemIsFocusable);
protagonist->setFocus();
scene->addItem(protagonist);
我是如何构建组合框的:
sceneScaleCombo = new QComboBox;
QStringList scales;
scales << tr("1%")<<tr("10%") << tr("20%") <<tr("50%") << tr("100%") <<tr("200%");
sceneScaleCombo->addItems(scales);
sceneScaleCombo->setCurrentIndex(4);
connect(sceneScaleCombo,SIGNAL(currentIndexChanged(QString)),this,SLOT(sceneScaleChanged(QString)));
void MainWindow::sceneScaleChanged(const QString &scale)
{
double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100;
QMatrix oldMatrix = view->matrix();
view->resetMatrix();
view->translate(oldMatrix.dx(),oldMatrix.dy());
view->scale(newScale,newScale);
protagonist->setFocus();
}
一开始一切都很顺利。但是,在我点击组合框后,我的主角就不能再被键盘控制了。我需要点击我的主角让它再次聚焦。
有什么方法可以自动设置焦点?
非常感谢。
【问题讨论】:
-
如果您希望我们帮助您,我们需要您提供minimal reproducible example
-
而不是
protagonist->setFocus();试试view->setFocus();。 -
@scopchanov 那行得通!非常感谢!
-
不客气!
-
@scopchanov 抱歉,已接受