我正在开发一个棋盘游戏,我正在尝试使 QWidgets(矩形)可选。
所以我有一个 BoardView(继承自 QWidget),其中包含 BuildingViews、PlantationViews(都继承自 QWidget)。这一切都显示在窗口上,但不可点击。我怎样才能使这个可点击?
最佳答案
您可以尝试在转发小部件 ID 的地方进行 QMouseEvent 实现,
像这样:
在您的小部件的实现中(例如 YourWidget.cpp):
YourWidget::MouseReleaseEvent(QMouseEvent *event)
{
emit clickedWithMouse(this); // this is a signal, declared in YourWidget.h
}
在“主”游戏文件(例如 Game.cpp)中:
Game::onButtonClicked(YourWidget* widget) // this is a public slot, you must connect all YourWidgets's clickedWithMouse signals to this slot (in Game object code, e.g. when initialising the board)
{
lastWidget = widget; //save the widget "ID" (lastWidget is a member of class Game)
someFunction(widget); //do something with the widget (if you wish)
}
关于qt - 使自定义 QWidget 可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16961684/