2010-05-29 92 views
1

我正在寫我自己的4狀態按鈕,我不太清楚如果有的話,在checkStateSet()方法中放什麼。QAbstractButton :: checkStateSet()方法的用途是什麼?

這裏是我到目前爲止有:

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)); 
    } 

回答

0

首先,QAbstractButton有1個「未登記」狀態,並且可能有幾個「檢查」狀態。

當檢查狀態從「未選中」更改爲「已檢查」時調用此方法。你必須設置inital「checked」狀態。它應該是您的3個「檢查」值中的第一個狀態,

另外你的實現nextCheckState()應該調用setChecked(false),當調用3.rd時檢查值返回到「unchecked」狀態。

Betters請參閱QAbstractButton的代碼:http://www.koders.com/cpp/fid1779E80AD2DA4C93CA22AB575FAA092A9681AE7B.aspx?s=mdef%3Ainsert