Solved QPolygon as QPushButton
-
Hi
thank you so much. I almost got it working.
update:
It draws the polygon in the mainwindow, but it doesn't have the functionalities that a button has. How can I add these functionalities such as it being pressable?This is what it currently looks like, but it's not clickable:
-
Hi
But it should still have those features?
Its still a QPushButton yes ?Button::Button(QWidget *parent) : QPushButton(parent) <<< base class is a button
so it should still give the same signals as normally unless you
also overrode the mousePressEvent and didnt show in this code.So i think its still clickable but just dont show any press effect since its custom drawn now. ( i guess)
-
Try to connect to your PolygonButton and print some debug output to console with QDebug inside your slot function. It should work, as @mrjj already said, because you are subclassing
QPushButton
, which is aQAbstractButton
subclass. So yourPolygonButton
should have all non-private functions, signals, events etc as a regularQPushButton
You could add some graphic effects with stylesheet (change color on click or mouseDown and so on)
@mrjj For my interest: You suggested to use
setMask
... It doen't change the boundings of the object, right? (Just the painted area?!) The button still keeps its bounding rectangle?! -
@Pl45m4
Hi
Docs says
"Masked widgets receive mouse events only on their visible portions."
so it should affect the hit testing like seen in
https://doc.qt.io/qt-5/qtwidgets-widgets-shapedclock-example.html# -
Just picked this project up again, but I still didn't manage to get it working.
I have two classes (MainWindow & Button). This is what I tried:
button.cpp:
#include "button.h" Button::Button( QPolygon poly,QWidget *parent) : QPushButton(parent), QPolygon(poly) { setMouseTracking(true); setMask(QPolygon(poly)); }
mainwindow.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPushButton> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { QVector<QPoint> qv_point = {QPoint(100,100), QPoint(200,100), QPoint(200, 200)}; QPolygon polygon = qv_point; Button *pushbutton = new Button(polygon, this); ui->setupUi(this); pushbutton->update(); pushbutton->setGeometry(0,0,202, 502); connect(pushbutton, &QPushButton::clicked, this, &MainWindow::pushbutton_clicked); //pushbutton->setStyle() } MainWindow::~MainWindow() { delete ui; } void MainWindow::pushbutton_clicked() { qDebug() << "clicked"; }
It does not print "clicked".
-
Hi
It must be something in your button class.
Like overriding mousePress and not call
base class.If i use your code + QpushButton. it works.
QVector<QPoint> qv_point = {QPoint(100, 100), QPoint(200, 100), QPoint(200, 200)}; QPolygon polygon = qv_point; QPushButton *pushbutton = new QPushButton(this); pushbutton->setGeometry(0, 0, 202, 502); pushbutton->setMouseTracking(true); pushbutton->setMask(QPolygon(qv_point)); connect(pushbutton, &QPushButton::clicked, this, &MainWindow::pushbutton_clicked); } void MainWindow::pushbutton_clicked() { qDebug() << "test"; }
-
@mrjj said in QPolygon as QPushButton:
QVector<QPoint> qv_point = {QPoint(100, 100), QPoint(200, 100), QPoint(200, 200)};
QPolygon polygon = qv_point;
QPushButton *pushbutton = new QPushButton(this);
pushbutton->setGeometry(0, 0, 202, 502);
pushbutton->setMouseTracking(true);
pushbutton->setMask(QPolygon(qv_point));connect(pushbutton, &QPushButton::clicked, this, &MainWindow::pushbutton_clicked);
}
void MainWindow::pushbutton_clicked()
{
qDebug() << "test";
}Alright, then there must be something wrong with the Button class. Thank you so much for your help.
I'll post the solution here if I got it to work with the Button class. -
@hobbyProgrammer
Ok. it should work with custom class too.
I would guess on you have a custom mousePress or mouseMove or similar
and forgot to call base class. Else im not sure what could be wrong. -
This post is deleted! -
It works! This is the solution...
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QDebug>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
QVector<QPoint> qv_point = {QPoint(100, 100), QPoint(200, 100), QPoint(200, 200)};
QPolygon polygon = qv_point;
Button *pushbutton = new Button(polygon, this);connect(pushbutton, &Button::clicked, this, &MainWindow::pushbutton_clicked);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::pushbutton_clicked()
{
qDebug() << "test";
}#include "button.h"
Button::Button( QPolygon poly,QWidget *parent) : QPushButton(parent), QPolygon(poly)
{
setGeometry(0,0,200,500);
setMouseTracking(true);
setMask(QPolygon(poly));
}