QAbstractButton::checkStateSet() 方法的用途是什么?

发布于 2024-09-03 02:50:42 字数 1782 浏览 6 评论 0原文

我正在编写自己的 4 状态按钮,但我不太确定要在 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));
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心不设防 2024-09-10 02:50:42

首先,QAbstractButton 有 1 个“未选中”状态,并且可能有多个“选中”状态。

当检查状态从“未检查”更改为“检查”时调用此方法。您必须设置初始“已检查”状态。它应该是您的 3 个“已检查”值中的第一个状态,

而且您的实现 nextCheckState() 应该在调用第 3 个已检查值时调用 setChecked(false) 以返回到“未检查”状态。

更好的查看QAbstractButton的代码: http://www.koders.com /cpp/fid1779E80AD2DA4C93CA22AB575FAA092A9681AE7B.aspx?s=mdef%3Ainsert

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文