Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
[email protected] web/email now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!

Paste

Pasted as C++ by registered user raphael ( 1 year ago )
#ifndef PUSHBUTTONROUGE_H
#define PUSHBUTTONROUGE_H

#include <QPushButton>

class PushButtonRouge : public QPushButton
{
public:
    PushButtonRouge(QWidget *parent);
protected:
    void paintEvent(QPaintEvent *event) override;
};

#endif // PUSHBUTTONROUGE_H

//---------------------------------------------------------------------------
#include "pushbuttonrouge.h"
#include <QPainter>
#include <QPaintEvent>
#include <QStyleOptionButton>

PushButtonRouge::PushButtonRouge(QWidget *parent) : QPushButton(parent)
{

}

void PushButtonRouge::paintEvent(QPaintEvent *event)
{
    QStyleOptionButton option;
    option.initFrom(this);
    option.text = text();

    QPainter painter(this);

    if (option.state & QStyle::State_MouseOver) {
        painter.fillRect(event->rect(), Qt::lightGray);
    } else if (option.state & QStyle::State_Sunken) {
        painter.setPen(Qt::green);
        painter.drawText(rect(), Qt::AlignCenter, text());
    } else {
        //État normal
        painter.fillRect(event->rect(), Qt::red);
    }

    painter.setPen(Qt::green);
    painter.drawText(rect(), Qt::AlignCenter, option.text);
}


//-------------------------------------------------------------------------------------------------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    bool drawEllipse;
private slots:
    void onPushButtonPressed();
protected:
    void paintEvent(QPaintEvent *event);
};
#endif // MAINWINDOW_H
//----------------------------------------------------------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),drawEllipse(false)
{
    ui->setupUi(this);
    //this->setStyleSheet("background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 blue, stop:0.5 white, stop:1 red);");
    connect(ui->pushButton, &QPushButton::pressed, this, &MainWindow::onPushButtonPressed);


}

void MainWindow::onPushButtonPressed()
{
    qDebug() << "Bouton pressé";
    drawEllipse = true;
    update();  // Cela déclenchera la méthode paintEvent pour redessiner la fenêtre
}

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

void MainWindow::paintEvent(QPaintEvent *event)
{
    QMainWindow::paintEvent(event);

    if (drawEllipse)
    {
        QPainter painter(this);
        painter.setBrush(Qt::blue);
        painter.setPen(Qt::black);
        painter.drawEllipse(50, 50, 200, 200);  // Dessine une ellipse à la position (100,100) avec une largeur et une hauteur de 200
    }
}

 

Revise this Paste

Parent: 126239
Your Name: Code Language: