0

How can I create a triangular pushbutton in Qt? What is the most simplest way of executing this? I use the designer to create buttons and not code.

Also, I read somewhere that shapes may be changed as long as the frame of the button is still rectangular but I want the frame to adjust according to the shape as well. How can I achieve this?

More detail: I want to place lots of small triangular buttons next to each other with every other triangle flipped. Each triangle button has it's own function, etc (no overlapping borders accepted). Can anyone give me a descriptive explanation for how I might go about this?

Pyraminx

Outsmash
  • 77
  • 8
  • Do you need exactly `QPushButton`? You can use [QGraphicsView](http://doc.qt.io/qt-5/qgraphicsview.html), [QGraphicsScene](http://doc.qt.io/qt-5/qgraphicsscene.html) and reimplement [QGraphicsItem](http://doc.qt.io/qt-5/qgraphicsitem.html) to make it tiangle. – Alexander Sorokin Sep 09 '15 at 21:06

1 Answers1

0

The geometry on a QWidget is always a rectangle.

It would be possible to create a QPushButton derivative, override its paintevent and do some nasty painting considering its neighborhood etc. but it would be really a pain...

it is much easier to use a QGraphicsView, QGraphicsScene and add appropriate QGraphicsItem (maybe the QGraphicsPolygonItem?), add them and use their signals/slots or create a derived class for your purposes.

It is not that hard to override the mouseevents to recognize clicks and you can even use the QStyleSheets to let the "button" look like it gets pressed.

Gombat
  • 1,994
  • 16
  • 21
  • 3
    The geometry of a widget is a rectangle, but that's really just a bounding rectangle. You can define any visible geometry you want, and any event-sensitive geometry as well. A rectangular widget can let events fall through the transparent parts. About the only "issue" is that of a layout, one'd need fixed positioning or a custom layout for such widgets. – Kuba hasn't forgotten Monica Sep 10 '15 at 01:55
  • But every small triangle needs a bounding rectangle if I use QGraphicsItem right? Obviously, the rectangle would cut through the neighboring triangles. I want individual functionality for each small triangle. Is there any way to bypass the boundingrect? – Outsmash Sep 11 '15 at 06:49
  • @Outsmash: the bounding rectangle is just a method which returns the extents of the QGraphicsPolygonItem, it does not mean, that the whole bounding rectangle is your button area. You have the mousepressevent, mousereleaseevent, mousemoveevent and for those a method called "contains(QPoint)", to see whether the mouse position is inside the triangle. – Gombat Sep 11 '15 at 07:33
  • Thanks! I managed to create it. I overrode the shape function to match the polygon created. I created a sub class from QGraphicsItem and created three triangles using this subclass. The class definition has a mousepressedevent which has "update();". I have a few pushbuttons (outside graphicsview) that change a QColor variable to respective colors and the paint function in the sub class has a brush variable that changes it's color according to the QColor variable. When i click on a triangle in my application, all of the triangles change color. How can I separate the functionality? – Outsmash Sep 11 '15 at 13:16
  • the paintevent runs always, as it does not matter, if you clicked on it or not. the paintevent paints everything. So if you change a variable used in the paintevent of all triangles, of course all triangles have the changed variable as a color. instead, you could save the color to a member of the specific triangle instance. – Gombat Sep 11 '15 at 13:20
  • That helped! Thanks a lot. But if I created many triangles using a for loop and the user changes the color of each triangle as per his requirement. How will I know which instance of my class he clicked? I have it coded such that every instance of a traingle has a mousepressedevent which changes the color as per the user but how do I record the color chosen? I want all the colors in a particular order, not the way the user clicked which might be random. Thanks again – Outsmash Sep 11 '15 at 20:44
  • When constructing the triangles or in an initial routine, you could give your triangles IDs. In the mousepressevent, you could call a method returning a specific color depending on the ID. In the mousepressevent, you know the ID of your current triangle, since the mousepressevent is also a member method of the class and voila. – Gombat Sep 12 '15 at 00:49