qt - 使自定义 QWidget 可选

标签 qt qwidget

我正在开发一个棋盘游戏,我正在尝试使 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/

相关文章:

python - 在 python 方法中使用 np.genfromtxt 时出现问题

QtCreator Build 不会更新设计表单中的 UI 更改

java - 我如何在 C++ 的堆栈上正确添加 QTWidgets?

python - PyQt5 停止从不同类添加的小部件内的计时器

linux - Qt 链接错误

c++ - Qt:编辑条目时 QtAbstractItemView (QTableView) 的背景

qt - .ico图标未在Windows上显示

qt - QWidget如何检测鼠标停止移动

c++ - Qt - 如何从 QObject 转换为 UI 元素?

qt - QStyledItemDelegate-updateEditorGeometry如何工作?