使自定义QWidget可选

时间:2013-06-06 12:07:16

标签: qt qwidget

我正在进行桌面游戏,我正在努力使QWidgets(矩形)可选。 所以我有一个BoardView(继承自QWidget),它包含BuildingViews,PlantationViews(都继承自QWidget)。这一切都显示在窗口上,但它不可点击。我怎样才能点击这个?

1 个答案:

答案 0 :(得分:2)

您可以尝试在转发窗口小部件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)
}