qt-interest@trolltech.com
[Top] [All Lists]

Re: Qbutton question

Subject: Re: Qbutton question
From:
Date: Tue, 9 Nov 2004 07:57:00 -0600
Pat,

  This is your lucky day. I recently had the need for a nice LED and
just wrote one from scratch. The source is below, provided "as-is".
I implemented this as a button so I can click on it if needed, but you
can simply call setFlat( true ) and setEnabled( false ) to disable its
clickability. It supports a text overlay, but I usually don't use text
*inside* the led, instead adding a label beside or below it. I typically
construct the LED with code like

    #include "CLedWidget.h"
        ...
        QFont fnt( "Sans", 8, QFont::Bold );
        m_led = new CLedWidget( "", 8, this );
        m_led->setFixedSize( 32, 32 );
        m_led->setFont( fnt );
        ...
Once it has been constructed, you can call setOn( bool ) or isOn() to
set/get the state of the LED.

  Enjoy, and feel free to modify!

-Mark

==================== header file ====================

#ifndef IBMLINCLUDE_CLEDWIDGET_H
#define IBMLINCLUDE_CLEDWIDGET_H

#include <qpushbutton.h>
#include <qpainter.h>

class CLedWidget : public QPushButton
{
    Q_OBJECT

public:

    CLedWidget( const QString &text, int edge = 0, QWidget *parent = 0, 
const char *name = 0, WFlags f = 0 );
    ~CLedWidget() {}

    void    setOn( bool state );
    bool    isOn();

protected:

    bool    m_state;
    int     m_edge;
    QString m_txt;
    QBrush  m_onBrush;
    QBrush  m_offBrush;

    void    drawButtonLabel( QPainter * );
};

#endif  //  IBMLINCLUDE_CLEDWIDGET_H

==================== source file ====================

//
// CLedWidget.cpp
//

#include "CLedWidget.h"

CLedWidget::CLedWidget( const QString &text, int edge, QWidget *parent, 
const char *name, WFlags f )
    : QPushButton( parent, name )
{
    m_state = false;
    //m_edge = 0;
    m_edge = edge;
    m_txt = text;

    m_onBrush = QBrush( green );
    m_offBrush = QBrush( red.dark() );
}

void CLedWidget::setOn( bool state )
{
    m_state = state;
}

bool CLedWidget::isOn()
{
    return( m_state );
}

void CLedWidget::drawButtonLabel( QPainter *p )
{
    bool state = isOn();
    QRect r = rect();

    p->setPen( QPen( black, state ? 2: 1 ) );
    p->setBrush( state ? m_onBrush : m_offBrush );

    p->drawEllipse( r.left() + m_edge, r.top() + m_edge, r.height() - 
2*m_edge, r.height() - 2*m_edge  );
    p->setPen( QPen( white, 2 ) );
    p->drawArc( r.left() + (r.height()/2 + m_edge)/2, r.top() + 
(r.height()/2 + m_edge)/2, r.height()/2 - m_edge, r.height()/2 - m_edge, 
90*16, 90*16 );

    p->setPen( QPen( black ) );
    p->drawText( r.left() + m_edge, r.top() + m_edge, r.width() - 
2*m_edge, r.height() - 2*m_edge, AlignCenter, m_txt );
}
============================================================

owner-qt-interest@xxxxxxxxxxxxx wrote on 11/09/2004 07:18:22 AM:

> Hi All;
>  I'm trying to use a Qradiobutton as an led in an app. I need to set and
> clear the  button from a different function. If I use
> lsdataRecOn->setClicked() I get a sigsegv, if I use lsdataRecOn->setOn() 
the
> compiler complains.
>  Is there a better way to do this ( a QLed would be nice)? If not any 
idea
> on how to set/clear the radio button outside the function it was 
declared
> in?
> Thank
> Pat
> 
> --
> List archive and information: http://lists.trolltech.com/qt-interest/

--
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>