Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make Custom QWidget selectable

Tags:

qt

qwidget

I'm working on a boardgame and I'm trying to make QWidgets (Rectangles) selectable. So I have a BoardView (inherited from QWidget) which contains BuildingViews, PlantationViews (both inherited from QWidget). It all shows up on the window, but it is not clickable. How can I make this clickable?

like image 819
user2459549 Avatar asked Sep 02 '26 05:09

user2459549


1 Answers

You could try to make a QMouseEvent implementation where the widget ID is forwarded, something like this:

In the implementation of your widget (e.g. YourWidget.cpp):

YourWidget::MouseReleaseEvent(QMouseEvent *event)
{
     emit clickedWithMouse(this);    // this is a signal, declared in YourWidget.h
}

In the "main" game file (e.g. 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)
}
like image 124
user2448027 Avatar answered Sep 04 '26 04:09

user2448027



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!