C++ Qt中的开关按钮偶
我正在从两个C++ Qt中的开关按钮偶,c++,qt,qpushbutton,C++,Qt,Qpushbutton,我正在从两个QPushButton构建一个简单的开关。在开始时,只有一个被启用(比如说按钮1),当你点击它时,它被禁用,而按钮2被启用。如果您单击按钮2,它将被禁用,而按钮1将被启用 这是.cpp代码: #include "SwitchButton.h" #include "ui_SwitchButton.h" SwitchButton::SwitchButton(QWidget * parent, bool
QPushButton
构建一个简单的开关。在开始时,只有一个被启用(比如说按钮1),当你点击它时,它被禁用,而按钮2被启用。如果您单击按钮2,它将被禁用,而按钮1将被启用
这是.cpp代码:
#include "SwitchButton.h"
#include "ui_SwitchButton.h"
SwitchButton::SwitchButton(QWidget * parent,
bool initialStatus,
const QString & trueText,
const QString & falseText)
: QWidget(parent), ui(new Ui::SwitchButton)
{
ui->setupUi(this);
ui->trueButton->setText(trueText);
ui->falseButton->setText(falseText);
// NOTE: Redundant, emits the corresponding signal while constructed
if (initialStatus)
on_trueButton_clicked();
else
on_falseButton_clicked();
}
SwitchButton::~SwitchButton()
{
delete ui;
}
void SwitchButton::on_trueButton_clicked()
{
ui->trueButton->setEnabled(false);
ui->falseButton->setEnabled(true);
emit changeStatus(true);
}
void SwitchButton::on_falseButton_clicked()
{
ui->falseButton->setEnabled(false);
ui->trueButton->setEnabled(true);
emit changeStatus(false);
}
看起来很有前瞻性,而且在一定程度上是有效的。虽然按钮保持一致的启用/禁用状态,但当我快速切换时,禁用按钮的背景色不会变暗。下面是一个例子:
简单的切换会使背景颜色自行调整,但我想知道我是否忽略了什么,是否有办法避免这种行为
编辑:我做了更多的测试,发现它与配对按钮无关,但如果双击速度足够快,即使是一个按钮也会发生这种情况(可能是我在不知不觉中做了类似的事情)。我能够重现这种行为:单击按钮,当鼠标悬停在按钮上时,有时会导致悬停时的动画无法正确完成 在禁用按钮后一段时间更新该按钮将清除错误的动画。在禁用“按钮”的功能中添加以下行可清除错误的动画:
std::thread([&]{std::this_thread::sleep_for(std::chrono::毫秒(500));按钮->更新();}).detach()代码>
调用setEnabled(false)后立即更新禁用按钮
并没有阻止这种行为,但我找不到任何关于如何在qt源代码中实现鼠标悬停动画的信息
对于解决方法,您可以在一段时间后对按钮进行适当的延迟更新,例如通过QTimer或类似工具。什么是更改状态
信号?目前它什么也不做;将来我会将它连接到应用程序的其他部分。无论如何,它不会改变这些按钮的属性。也许按钮仍然聚焦。你能试试按钮->clearFocus()吗?我试过了,但似乎没有解决问题。你的按钮是否标记为可检查、自动默认或类似的?