0

I would like to paint a QPolygon on my Window and be able to use it as a QPushbutton. Is there any way to do this? (most preferably without using QMousePressEvent to check the position of the mouse with the position of the polygon)

After the advice of Ton:

MainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    qv_point = {QPoint(10,20), QPoint(20,30), QPoint(50,30)};
    ui->pushButton = new QPolygonPushButton(qv_point);
    ui->setupUi(this);
    ui->pushButton->update();
}

MainWindow::~MainWindow()
{
    delete ui;
}

qpolygonpusbutton.cpp:

#include "qpolygonpushbutton.h"

QPolygonPushButton::QPolygonPushButton(QVector<QPoint> qv_points)
{
    this->polygon << qv_points;
}

void QPolygonPushButton::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    painter.setViewport(e->rect());
    painter.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    painter.drawPolygon(this->polygon);
}

1 Answers1

0

This can be done by declaring your own push button type, something like QPolygonPushButton that derives from QPushButton, for which you then reimplement its paintEvent member function.

Something along the following lines (not tested):

class QPolygonPushButton : public QPushButton
{
public:
  using QPushButton::QPushButton;

private:
  void paintEvent(QPaintEvent* e) override
  {
     QPainter painter(this);
     painter.setViewport(e->rect());
     painter.drawPolygon(...);
  }
};

Update; fully working example. It uses a rectangle instead of a polygon but other than that you will get the idea. The button initially is a red rectangle, clicking it will change its color to blue.

#include <QtCore/QObject>
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QPainter>
#include <QtGui/QPushButton>

namespace
{
  QColor buttonColor{Qt::red};
}

class QRectButton : public QPushButton
{
public:
  using QPushButton::QPushButton;

  void paintEvent(QPaintEvent* e) override
  {
    QPainter painter(this);
    painter.setPen(buttonColor);
    painter.setBrush(buttonColor);
    painter.drawRect(painter.window());
  }
};

class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  MainWindow() : QMainWindow(nullptr)
  {
    QPushButton* button{new QRectButton(this)};
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  }

private slots:
  void onButtonClicked()
  {
    buttonColor = Qt::blue;
    update();
  }
};

int main(int argc, char** argv)
{
  QApplication app(argc, argv);
  MainWindow window;
  window.show();
  return app.exec();
}

#include "moc_polygon_button.cpp"
Ton van den Heuvel
  • 10,157
  • 6
  • 43
  • 82
  • Thank you for your help. I updated the question with new code, but it still isn't working. Nothing is visible in my MainWindow. Just to clarify: I added a pushbutton to my mainwindow.ui and promoted it to a QPolygonPushButton. Do you have any other tips or do you see where it goes wrong? – JustProgrammingQuestions Oct 02 '19 at 09:37
  • @JustProgrammingQuestions, see my updated answer for a working example. – Ton van den Heuvel Oct 02 '19 at 12:25