<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to redraw a button ??]]></title><description><![CDATA[<p dir="auto">I want to display a button that will have the standard functionality of a button (QPushButton), but at the same time have a different appearance</p>
<p dir="auto">But for some reason they do not allow you to output a class that inherits from QPushButton</p>
<p dir="auto"><a href="https://i.stack.imgur.com/SheHO.png" target="_blank" rel="noopener noreferrer nofollow ugc"><img src="https://i.stack.imgur.com/SheHO.png" alt="введите сюда описание изображения" class=" img-fluid img-markdown" /></a></p>
<p dir="auto">here is the code (I can't figure out what I'm doing wrong):</p>
<p dir="auto"><strong>CMakeLists.txt</strong></p>
<pre><code>cmake_minimum_required(VERSION 3.5)

project(myButton VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)

set(PROJECT_SOURCES
        main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
        custombutton.cpp
        custombutton.h
        lineitem.cpp
        lineitem.h
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(myButton
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET myButton APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(myButton SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(myButton
            ${PROJECT_SOURCES}
        )
    endif()
endif()

target_link_libraries(myButton PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

set_target_properties(myButton PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(myButton)
endif()
</code></pre>
<p dir="auto"><strong>main.cpp</strong></p>
<pre><code>#include "mainwindow.h"

#include &lt;QApplication&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
</code></pre>
<p dir="auto"><strong>mainwindow.cpp</strong></p>
<pre><code>#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)

{
    _line = new lineItem(this);

    ui-&gt;setupUi(this);

    ui-&gt;gridLayout-&gt;addItem(_line);
}

MainWindow::~MainWindow()
{
    delete ui;
}
</code></pre>
<p dir="auto"><strong>mainwindow.h</strong></p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;

#include "lineitem.h"


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;

    lineItem*  _line;

};
#endif // MAINWINDOW_H
</code></pre>
<p dir="auto"><strong>custombutton.cpp</strong></p>
<pre><code>#include "custombutton.h"

customButton::customButton(QPushButton *parent)
    : QPushButton{parent}
{
    _myPushButton = qobject_cast&lt;QPushButton*&gt;(parent);
}

void customButton::setPosition(float radiusPos, float angle)
{

    if(radiusPos&lt;0)
        _rPos = 0;
    else
        _rPos = radiusPos;

    if(angle&gt;360)
        _angle = 360;
    else if(radiusPos&lt;0)
        _angle = 0;
    else
        _angle = angle;

    _myPushButton-&gt;update();
}

QPointF customButton::getCenterPoint(float radPos, float angle)
{
    float r = getRadius(_myPushButton-&gt;rect());
    float xx=cos(qDegreesToRadians(angle+90))*r;
    float yy=sin(qDegreesToRadians(angle+90))*r;

    QPointF pt;
    xx=_myPushButton-&gt;rect().center().x()-xx*radPos;
    yy=_myPushButton-&gt;rect().center().y()-yy*radPos;
    pt.setX(xx);
    pt.setY(yy);
    return pt;
}


void customButton::setColors(const QColor &amp;substrate, const QColor &amp;item, const QColor &amp;outline)
{
    _colorSubstrate = substrate;
    _colorItem = item;
    _colorOutline = outline;
}

void customButton::setValueRange(float minValue, float maxValue)
{
    _minValue = minValue;
    _maxValue = maxValue;
}

void customButton::setDegreeRange(float minDegree, float maxDegree)
{
    _minDegree = minDegree;
    _maxDegree = maxDegree;
}

void customButton::setSubstrate(bool val)
{
    _drawSubstrate = val;
}

void customButton::setThicknessOutline(float val)
{
    _thicknessOutline = val;
}

void customButton::setOutline(bool val)
{
    _drawOutline = val;
}

void customButton::setShadow(bool val)
{
    _drawShadow = val;
}

float customButton::getRadius(const QRectF &amp;tmpRect)
{
    float r = 0;
    if(tmpRect.width() &lt; tmpRect.height()*_wh)
        r = tmpRect.width()/(2.0*_wh);
    else
        r = tmpRect.height()/2.0;
    return r;
}

float customButton::getDegFromValue(float val)
{
    float a = (_maxDegree-_minDegree)/(_maxValue-_minValue);
    float b = -a*_minValue+_minDegree;
    return a*val+b;
}

void customButton::setScaleFactor(float val)
{
    _scaleFactor = val;
}

void customButton::setThickness(float val)
{
    _thickness = val;
}

void customButton::setProportions(float wh)
{
    _wh = wh;
}
</code></pre>
<p dir="auto"><strong>custombutton.h</strong></p>
<pre><code>#ifndef BUTTONITEM_H
#define BUTTONITEM_H

#include &lt;QObject&gt;
#include &lt;QButtonGroup&gt;
#include &lt;QPushButton&gt;

#include &lt;QPainter&gt;
#include &lt;QColor&gt;
#include &lt;QFont&gt;
#include &lt;QDebug&gt;

#include &lt;QPainterPath&gt;
#include &lt;QPointF&gt;
#include &lt;QRect&gt;
#include &lt;QGraphicsBlurEffect&gt;
#include &lt;QGraphicsItem&gt;
#include &lt;QtMath&gt;
#include &lt;QMessageBox&gt;

class customButton : public QPushButton
{

public:
    explicit customButton(QPushButton *parent = nullptr);

    virtual void draw(QPainter *p) = 0;

    ///
    /// \brief setPosition установить радиус (1=100%) и угол (в градусах) определяющий расположение item на виджете
    ///
    void setPosition(float radiusPos , float angle);

    ///
    /// \brief getPoint расчитать центральную точку
    ///
    QPointF getCenterPoint(float radPos, float angle);

    ///
    /// \brief setColors: установить три цвета ( подложки, основной и обводки) для item
    ///
    void setColors(const QColor &amp;substrate, const QColor &amp;item, const QColor &amp;outline);

    ///
    /// \brief setValueRange установить диапазон изменения значения
    ///
    void setValueRange(float minValue,float maxValue);

    ///
    /// \brief setDegreeRange установить диапазон изменения угла
    ///
    void setDegreeRange(float minDegree,float maxDegree);

    /// inner function

    ///
    /// \brief включить отрисовку подложки с заданным ранее цветом и прозрачностью
    ///
    void setSubstrate(bool val);

    ///
    /// \brief setThicknessOutline установить толщину обводки
    ///
    void setThicknessOutline(float val);

    ///
    /// \brief включить отрисовку подложки с заданным ранее цветом и прозрачностью
    ///
    void setOutline(bool val);

    ///
    /// \brief включить отрисовку "тени"
    ///
    void setShadow(bool val);

    ///
    /// \brief getRadius получить радиус окружности в которую вписан заданный прямоугольник
    ///
    float getRadius(const QRectF &amp;tmpRect);

    ///
    /// \brief getDegFromValue
    ///
    float getDegFromValue(float val);

    ///
    /// \brief setScaleFactor установить коэффициент масштаба
    ///
    void setScaleFactor(float val);

    ///
    /// \brief setThickness установить толщину линии
    ///
    void setThickness(float val);

    ///
    /// \brief setProportions задать соотношение ширины к высоте в виджете с данным item
    ///
    void setProportions(float wh);

//    QWidget *_parentWidget;
    QPushButton *_myPushButton;


    QColor   _colorSubstrate    {Qt::black};
    QColor   _colorItem         {Qt::black};
    QColor   _colorOutline      {Qt::white};
    QColor   _colorCornerBound  {Qt::darkRed};

    QColor   _colorShadowB      {Qt::black};
    QColor   _colorShadowW      {Qt::white};

    QColor   _colorKaracurtWhite  {255, 255, 255, 255};
    QColor   _colorKaracurtYellow {255, 255,  0 , 255};
    QColor   _colorKaracurtSubstrate { 0, 0,  0 , 128};
    QColor   _colorKaracurtShadow { 0 ,  0 ,  0 , 200};


    float    _thicknessOutline  {5};
    float    _thickness         {3};

    float    _scaleFactor       {1};

    float    _rPos              {0.5};
    float    _angle             {0.5};

    float    _minValue          {0};
    float    _maxValue          {100};
    float    _minDegree         {0};
    float    _maxDegree         {360};

    bool     _drawSubstrate     {false};
    bool     _drawOutline       {false};
    bool     _drawShadow        {false};

    float    _wh                {1};


signals:

};

#endif // BUTTONITEM_H
</code></pre>
<p dir="auto"><strong>lineitem.cpp</strong></p>
<pre><code>#include "lineitem.h"

lineItem::lineItem(QPushButton *parent)
    : customButton(parent)
{

}

void lineItem::draw(QPainter *p)
{
    p-&gt;save();

    QPointF center = getCenterPoint(_rPos, _angle);
    float r = getRadius(_myPushButton-&gt;rect())*_scaleFactor;

    if(_drawSubstrate)
    {
        p-&gt;setBrush(QBrush(_colorSubstrate));
        p-&gt;drawEllipse(center,(int)(r), (int)(r));
    }

    p-&gt;translate(center);

    p-&gt;setPen(Qt::NoPen);
    p-&gt;setBrush(QBrush(_color));
    p-&gt;drawRect(-r/1.5, -r/1.5, r*1.34, r*1.34);


    QVector&lt;QPointF&gt; tmpPoints;
    tmpPoints.append(QPointF(0.0 - r*0.46 , 0.0 + r*0.2 ));
    tmpPoints.append(QPointF(0.0 - r*0.36 , 0.0 + r*0.2 ));

    tmpPoints.append(QPointF(0.0 - r*0.26 , 0.0 - r*0.15 ));
    tmpPoints.append(QPointF(0.0 - r*0.06 , 0.0 + r*0.45 ));

    tmpPoints.append(QPointF(0.0 + r*0.06 , 0.0 - r*0.4  ));
    tmpPoints.append(QPointF(0.0 + r*0.26 , 0.0 + r*0.25 ));

    tmpPoints.append(QPointF(0.0 + r*0.36 , 0.0 + r*0.1 ));
    tmpPoints.append(QPointF(0.0 + r*0.46 , 0.0 + r*0.1 ));

    QPainterPath path;
    path.addPolygon(tmpPoints);

    p-&gt;setBrush(Qt::NoBrush);
    p-&gt;setPen(QPen(_color_line, 2*_thickness, Qt::SolidLine));
    p-&gt;drawPath(path);

    p-&gt;restore();
}
</code></pre>
<p dir="auto"><strong>lineitem.h</strong></p>
<pre><code>#ifndef LINEITEM_H
#define LINEITEM_H

#include "custombutton.h"

class lineItem : public customButton
{
public:
    lineItem();

    explicit lineItem(QPushButton *parent = nullptr);

    virtual void draw(QPainter * p) override final;

    private:

        QColor   _color        {220, 0, 0, 227};
        QColor   _color_line {255,255,255,255};
};
#endif // LINEITEM_H
</code></pre>
]]></description><link>https://forum.qt.io/topic/137590/how-to-redraw-a-button</link><generator>RSS for Node</generator><lastBuildDate>Sun, 14 Jun 2026 03:57:48 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/137590.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 30 Jun 2022 21:20:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to redraw a button ?? on Fri, 01 Jul 2022 07:28:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/timob256">@<bdi>timob256</bdi></a> said:</p>
<blockquote>
<p dir="auto">I have somehow already done inherited from qwidget</p>
</blockquote>
<p dir="auto">no, inheriting from QPushButton is fine, I'm talking about the constructor parameter that should be QWidget and not QPushButton.</p>
<blockquote>
<p dir="auto">I still didn't understand what exactly to do</p>
</blockquote>
<p dir="auto">Summarizing what I said:</p>
<ul>
<li>Change <code>explicit lineItem(QPushButton *parent = nullptr)</code> to <code>explicit lineItem(QWidget *parent = nullptr)</code></li>
<li>remove the unnecessary <code>lineItem()</code> constructor</li>
<li>remove the unnecessary <code>QPushButton *_myPushButton</code> member</li>
<li>use your own geometry instead of parent's in your logic e.g. instead of <code>_myPushButton-&gt;rect()</code> use <code>rect()</code>.</li>
<li>override <code>paintEvent()</code> and paint there, not in custom <code>draw()</code> method.</li>
</ul>
<blockquote>
<p dir="auto">Is there an example ???</p>
</blockquote>
<p dir="auto">Of what? Of exactly what you're doing? I don't think so.</p>
]]></description><link>https://forum.qt.io/post/719763</link><guid isPermaLink="true">https://forum.qt.io/post/719763</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Fri, 01 Jul 2022 07:28:30 GMT</pubDate></item><item><title><![CDATA[Reply to How to redraw a button ?? on Wed, 06 Jul 2022 18:21:57 GMT]]></title><description><![CDATA[<p dir="auto">The constructors are still wrong. Should be</p>
<pre><code>QSliderButton::QSliderButton(QWidget* parent) : QWidget(parent) {}
</code></pre>
<p dir="auto">and the empty one is just redundant. You don't need it at all.</p>
<p dir="auto">Better yet don't manually implement any constructors, just do</p>
<pre><code>class QSliderButton : public QWidget {
    Q_OBJECT
public:    
    using QWidget::QWidget;

...
</code></pre>
<p dir="auto">The using statement will inherit all the constructors from the base class so you don't have to implement any.</p>
]]></description><link>https://forum.qt.io/post/720325</link><guid isPermaLink="true">https://forum.qt.io/post/720325</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Wed, 06 Jul 2022 18:21:57 GMT</pubDate></item><item><title><![CDATA[Reply to How to redraw a button ?? on Wed, 06 Jul 2022 18:14:43 GMT]]></title><description><![CDATA[<p dir="auto">I spied the answer</p>
<p dir="auto"><a href="https://blog.altuninvv.ru/%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5/qt5/%D0%B2%D0%B8%D0%B4%D0%B6%D0%B5%D1%82%D1%8B/153-%D1%81%D0%BE%D0%B7%D0%B4%D0%B0%D0%B5%D0%BC-%D1%81%D0%BB%D0%B0%D0%B9%D0%B4%D0%B5%D1%80-%D1%81%D0%BA%D0%BE%D0%BB%D1%8C%D0%B7%D1%8F%D1%89%D0%B8%D0%B9-%D0%BF%D0%B5%D1%80%D0%B5%D0%BA%D0%BB%D1%8E%D1%87%D0%B0%D1%82%D0%B5%D0%BB%D1%8C-slider-button-%D0%B2-qt5" target="_blank" rel="noopener noreferrer nofollow ugc">https://blog.altuninvv.ru/программирование/qt5/виджеты/153-создаем-слайдер-скользящий-переключатель-slider-button-в-qt5</a></p>
<p dir="auto"><strong>CMakeLists.txt</strong></p>
<pre><code>cmake_minimum_required(VERSION 3.5)

project(myButton3 VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)

set(PROJECT_SOURCES
        main.cpp
        mainwindow.cpp
        mainwindow.h
        qsliderbutton.cpp
        qsliderbutton.h
        mainwindow.ui
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(myButton3
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET myButton3 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(myButton3 SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(myButton3
            ${PROJECT_SOURCES}
        )
    endif()
endif()

target_link_libraries(myButton3 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

set_target_properties(myButton3 PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(myButton3)
endif()
</code></pre>
<p dir="auto"><strong>main.cpp</strong></p>
<pre><code>#include "mainwindow.h"

#include &lt;QApplication&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
</code></pre>
<p dir="auto"><strong>mainwindow.h</strong></p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;
#include &lt;QVBoxLayout&gt;
#include "qsliderbutton.h"

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;

    QSliderButton *sldBtn;
};
#endif // MAINWINDOW_H
</code></pre>
<p dir="auto"><strong>mainwindows.cpp</strong></p>
<pre><code>#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

    QVBoxLayout *vlay = new QVBoxLayout();
    QHBoxLayout *hlay1 = new QHBoxLayout();

    sldBtn =  new QSliderButton;
    hlay1-&gt;addWidget(sldBtn);

    hlay1-&gt;addStretch(1);
    vlay-&gt;addItem(hlay1);

    vlay-&gt;addStretch(1);
    ui-&gt;centralwidget-&gt;setLayout(vlay);
}

MainWindow::~MainWindow()
{
    delete ui;
}
</code></pre>
<p dir="auto"><strong>qsliderbutton.h</strong></p>
<pre><code>#ifndef QSLIDERBUTTON_H
#define QSLIDERBUTTON_H

#include &lt;QWidget&gt;
#include &lt;QMouseEvent&gt;
#include &lt;QDebug&gt;


class QSliderButton : public QWidget {
    Q_OBJECT
public:    
    explicit QSliderButton(QWidget* parent);
    QSliderButton();


    int getStatus() const;
    void setStatus(int value);

    static const int off = 0;
    static const int on  = 1;

private:
    int status = 0;

protected:
    virtual void paintEvent(QPaintEvent *event);
    virtual QSize sizeHint() const;
    virtual void mousePressEvent(QMouseEvent * event);

};

#endif // QSLIDERBUTTON_H
</code></pre>
<p dir="auto"><strong>qsliderbutton.cpp</strong></p>
<pre><code>#include "qsliderbutton.h"
#include &lt;QPainter&gt;

QSliderButton::QSliderButton(QWidget *parent)
{
    this-&gt;setParent(parent);
}

QSliderButton::QSliderButton()
{
}

int QSliderButton::getStatus() const
{
    return status;
}

void QSliderButton::setStatus(int value)
{
    status = value;
    repaint();
}

void QSliderButton::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing, true);

    painter.setPen(QPen(QColor("#fff"), 0.1));

    QString bgColorTxt = "#ffffff";
    QColor bgColor = QColor(bgColorTxt);

    painter.setBrush(bgColor);

    painter.drawRoundedRect(QRectF(0, 0, 50, 20),10,10);

    QLinearGradient linearGradBtn(QPointF(0, 0),QPointF(16, 16));

    QString onColor = "#444";
    QColor mainColorOn = QColor(onColor);
    QColor subColorOn = QColor(onColor);
    subColorOn.setHsl(0,100,95,0);


    if (this-&gt;status==QSliderButton::on) {
        QLinearGradient linearGrad(QPointF(32, 2), QPointF(46, 16));
        linearGrad.setColorAt(0, subColorOn);
        linearGrad.setColorAt(1, mainColorOn);

        painter.setBrush(linearGrad);

        painter.drawEllipse( QRectF(30, 2, 17, 16) );
    } else {
        QLinearGradient linearGrad(QPointF(2, 2), QPointF(16, 16));
        linearGrad.setColorAt(0, subColorOn);
        linearGrad.setColorAt(1, mainColorOn);

        painter.setBrush(linearGrad);

        painter.drawEllipse( QRectF(2, 2, 16, 16) );
    }
}
QSize QSliderButton::sizeHint() const {
    return QSize(50, 20);
}

void QSliderButton::mousePressEvent(QMouseEvent *event)
{
    if (event-&gt;button() == Qt::LeftButton ) {
        if (this-&gt;status==QSliderButton::on) {
            this-&gt;status = QSliderButton::off;
        } else {
            this-&gt;status = QSliderButton::on;
        }
        repaint();
    }
}
</code></pre>
<p dir="auto"><a href="https://i.stack.imgur.com/eFwpv.png" target="_blank" rel="noopener noreferrer nofollow ugc"><img src="https://i.stack.imgur.com/eFwpv.png" alt="введите сюда описание изображения" class=" img-fluid img-markdown" /></a></p>
]]></description><link>https://forum.qt.io/post/720323</link><guid isPermaLink="true">https://forum.qt.io/post/720323</guid><dc:creator><![CDATA[timob256]]></dc:creator><pubDate>Wed, 06 Jul 2022 18:14:43 GMT</pubDate></item><item><title><![CDATA[Reply to How to redraw a button ?? on Fri, 01 Jul 2022 07:28:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/timob256">@<bdi>timob256</bdi></a> said:</p>
<blockquote>
<p dir="auto">I have somehow already done inherited from qwidget</p>
</blockquote>
<p dir="auto">no, inheriting from QPushButton is fine, I'm talking about the constructor parameter that should be QWidget and not QPushButton.</p>
<blockquote>
<p dir="auto">I still didn't understand what exactly to do</p>
</blockquote>
<p dir="auto">Summarizing what I said:</p>
<ul>
<li>Change <code>explicit lineItem(QPushButton *parent = nullptr)</code> to <code>explicit lineItem(QWidget *parent = nullptr)</code></li>
<li>remove the unnecessary <code>lineItem()</code> constructor</li>
<li>remove the unnecessary <code>QPushButton *_myPushButton</code> member</li>
<li>use your own geometry instead of parent's in your logic e.g. instead of <code>_myPushButton-&gt;rect()</code> use <code>rect()</code>.</li>
<li>override <code>paintEvent()</code> and paint there, not in custom <code>draw()</code> method.</li>
</ul>
<blockquote>
<p dir="auto">Is there an example ???</p>
</blockquote>
<p dir="auto">Of what? Of exactly what you're doing? I don't think so.</p>
]]></description><link>https://forum.qt.io/post/719763</link><guid isPermaLink="true">https://forum.qt.io/post/719763</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Fri, 01 Jul 2022 07:28:30 GMT</pubDate></item><item><title><![CDATA[Reply to How to redraw a button ?? on Fri, 01 Jul 2022 07:10:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chris-kawa">@<bdi>Chris-Kawa</bdi></a> Thanks for the answer, but I still didn't understand what exactly to do. I have somehow already done inherited from<br />
qwidget , but I didn't output actions (signal-slot) like "clicking" And "Hovering the mouse".</p>
<p dir="auto">Is there an example   ???</p>
]]></description><link>https://forum.qt.io/post/719748</link><guid isPermaLink="true">https://forum.qt.io/post/719748</guid><dc:creator><![CDATA[timob256]]></dc:creator><pubDate>Fri, 01 Jul 2022 07:10:46 GMT</pubDate></item><item><title><![CDATA[Reply to How to redraw a button ?? on Thu, 30 Jun 2022 21:54:22 GMT]]></title><description><![CDATA[<p dir="auto">You have a constructor <code>explicit lineItem(QPushButton *parent = nullptr)</code> and you're calling it as <code>new lineItem(this)</code>, where <code>this</code> is a MainWindow and not an instance of QPushButton.</p>
<p dir="auto">It's unusual to have a QPushButton parent. Usually you'd put plain QWidget as the parent type so you can make the widget a child of any other widgets, not just QPushButtons.</p>
<p dir="auto">Btw. it's also weird to have both a default constructor with no parameters and another with single default parameter. The second one should be enough for both cases.</p>
<p dir="auto">Another weird thing is code like this: <code>_myPushButton = qobject_cast&lt;QPushButton*&gt;(parent);</code> when parent already is a QPushButton. You don't even use any of QPushButton's methods, so you could just use plain QWidget as a parent. You don't need to store it either. All QWidgets already store their parent and you can access it via <code>parentWidget()</code> method. You should also always check if parent is null before you use it, because you yourself created a constructor that allows to pass nullptr and that's even the default parameter.</p>
<p dir="auto">I don't really get the custom button logic either. Why is it using its parent geometry? Shouldn't it be using its own? Any well behaving widget doesn't rely on existance of a parent at all. What if someone instantiates your widget as a top level widget without a parent?</p>
<p dir="auto">Another thing is that a widget should draw itself in an overriden <code>paintEvent</code> method, which is called by the framework when the widget needs to be redrawn. Why do you have a custom <code>draw</code> method that someone will have to always remember to call for you?</p>
]]></description><link>https://forum.qt.io/post/719725</link><guid isPermaLink="true">https://forum.qt.io/post/719725</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Thu, 30 Jun 2022 21:54:22 GMT</pubDate></item></channel></rss>