我有几个从QAbstractButton
withautoExclusive
和checkable
属性 TRUE 派生的自定义按钮应用于同一个父级。(因此只能同时检查一个项目)。
父级是 aQDialog
并且我希望每当显示对话框时,例如项目 1 获得键盘焦点,以便用户可以轻松地在键盘上的项目之间导航,但只有在用户选择带有鼠标释放的项目时才会触发某些功能。
当我在键盘导航上捕获信号时(在本例中为第 2 项)所有QAbstractButton
信号:
clicked
pressed
released
toggled
将被触发。
为什么会这样?
我能做些什么?
Item
和Dialog
实施:
Item::Item(QWidget *parent) : QAbstractButton(parent) {
setAutoExclusive(true);
setCheckable(true);
}
void Item::paintEvent(QPaintEvent *) {
QPainter p(this);
p.setPen(Qt::NoPen);
p.setRenderHint(QPainter::Antialiasing);
p.setBrush(/*brush*/);
p.drawRoundedRect(rect(), /* raduis*/, /* radius */);
p.setRenderHint(QPainter::Antialiasing, false);
if (isChecked()) p.drawPixmap(rect(), /*pixmap*/);
}
QSize Item::sizeHint() const {
return QSize(/*size*/, /*size*/);
}
Dialog::Dialog(QWidget *parent) : QDialog(parent) {
_mainLayout.setContentsMargins(24, 24, 24, 24);
_mainLayout.setSpacing(12);
_mainLayout.addWidget(&_item1, 0, 0);
_mainLayout.addWidget(&_item2, 0, 1);
_mainLayout.addWidget(&_item3, 0, 2);
QObject::connect(&_item2, SIGNAL(clicked()), this, SLOT(onItemClicked()));
QObject::connect(&_item2, SIGNAL(released()), this, SLOT(onItemReleased()));
QObject::connect(&_item2, SIGNAL(pressed()), this, SLOT(onItemPPress()));
QObject::connect(&_item2, SIGNAL(toggled(bool)), this, SLOT(onToggle(bool)));
}
void Dialog::showEvent(QShowEvent *e) {
_item1.setFocus(Qt::TabFocusReason);
QDialog::showEvent(e);
}
void Dialog::onItemClicked() {
qDebug() << "CLICKED";
}
void Dialog::onItemReleased() {
qDebug() << "RELEASED";
}
void Dialog::onItemPPress() {
qDebug() << "PRESS";
}
void Dialog::onToggle(bool f) {
qDebug() << "Toggle";
}