tags:

views:

29

answers:

1

I'm writing my own 4 state button and I'm not quite sure what to put in the checkStateSet() method, if anything.

Here is what I've got so far:

    SyncDirectionButton::SyncDirectionButton(QWidget *parent) :
 QAbstractButton(parent)
    {
 setCheckable(true);
 setToolTip(tr("Click to change the sync direction"));
 _state = NoSync;
    }

    void SyncDirectionButton::paintEvent(QPaintEvent *e)
    {
 static QPixmapCache::Key noneKey;
 static QPixmapCache::Key bothKey;
 static QPixmapCache::Key leftKey;
 static QPixmapCache::Key rightKey;

 QPainter p(this);

 QPixmap pix;
 if (checkState() == SyncLeft) {
  if (!QPixmapCache::find(leftKey, &pix)) {
   pix.load(":/icons/sync-left.png");
   leftKey = QPixmapCache::insert(pix);
  }
 } else if (checkState() == SyncBoth) {
  if (!QPixmapCache::find(rightKey, &pix)) {
   pix.load(":/icons/sync-right.png");
   rightKey = QPixmapCache::insert(pix);
  }
 } else if (checkState() == SyncRight) {
  if (!QPixmapCache::find(bothKey, &pix)) {
   pix.load(":/icons/sync-both.png");
   bothKey = QPixmapCache::insert(pix);
  }
 } else if (checkState() == NoSync) {
  if (!QPixmapCache::find(noneKey, &pix)) {
   pix.load(":/icons/application-exit.png");
   noneKey = QPixmapCache::insert(pix);
  }
 }
 p.drawPixmap(0,0,pix);
    }

    SyncDirectionButton::DirectionState SyncDirectionButton::checkState() const
    {
 return _state;
    }

    void SyncDirectionButton::setCheckState(DirectionState state)
    {
 setChecked(state != NoSync);
 if (state != _state) {
  _state = state;
 }
    }

    QSize SyncDirectionButton::sizeHint() const
    {
 return QSize(180,90);
    }

    void SyncDirectionButton::checkStateSet()
    {

    }

    void SyncDirectionButton::nextCheckState()
    {
 setCheckState((DirectionState)((checkState()+1)%4));
    }
A: 

First, the QAbstractButton has 1 "unchecked" state and may have several "checked" states.

This method is called when check state is changed from "unchecked" to "checked". You have to set the inital "checked" state. It should be the first state in your 3 "checked" values,

Also your implementation nextCheckState() should call setChecked(false), when called on 3.rd checked value to return to "unchecked" state.

Betters see the code of QAbstractButton: http://www.koders.com/cpp/fid1779E80AD2DA4C93CA22AB575FAA092A9681AE7B.aspx?s=mdef%3Ainsert

extropy