Qt之QPushButton按下连续事件

有时候我们不想一下一下的按QPushButton来执行某个事件,希望一直按着按钮,就使事件不断的执行,比如翻页等操作。Qt中三个函数可以帮助我们实现这个操作:

void setAutoRepeat ( bool )

void setAutoRepeatDelay ( int )

void setAutoRepeatInterval ( int )

顾名思义,三个函数的作用分别为:设置是否允许自动重复,设置重复操作的时延,设置自动操作的间隔

由此,我们可以按下面这样设置:

setAutoRepeat (true);

setAutoRepeatDelay(任意值);

setAutoRepeatInterval(任意值);

应该注意,后面两个函数的单位都是毫秒

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的基于Qt的颜色连续范围选择器示例代码: ```cpp #include <QApplication> #include <QWidget> #include <QHBoxLayout> #include <QVBoxLayout> #include <QLabel> #include <QSlider> #include <QColorDialog> #include <QPalette> class ColorSelector : public QWidget { Q_OBJECT public: explicit ColorSelector(QWidget *parent = 0); private: void updateColor(); private slots: void onMinHueChanged(int value); void onMaxHueChanged(int value); void onColorButtonClicked(); private: QLabel *m_minHueLabel; QLabel *m_maxHueLabel; QSlider *m_minHueSlider; QSlider *m_maxHueSlider; QPushButton *m_colorButton; QPalette m_palette; QColor m_color; int m_minHue; int m_maxHue; }; ColorSelector::ColorSelector(QWidget *parent) : QWidget(parent), m_minHueLabel(new QLabel("Min Hue:")), m_maxHueLabel(new QLabel("Max Hue:")), m_minHueSlider(new QSlider(Qt::Horizontal)), m_maxHueSlider(new QSlider(Qt::Horizontal)), m_colorButton(new QPushButton("Select color")), m_minHue(0), m_maxHue(179) { // 设置最小最大值和步长 m_minHueSlider->setMinimum(0); m_minHueSlider->setMaximum(179); m_minHueSlider->setSingleStep(1); m_maxHueSlider->setMinimum(0); m_maxHueSlider->setMaximum(179); m_maxHueSlider->setSingleStep(1); // 设置默认值 m_minHueSlider->setValue(m_minHue); m_maxHueSlider->setValue(m_maxHue); // 连接信号槽 connect(m_minHueSlider, SIGNAL(valueChanged(int)), this, SLOT(onMinHueChanged(int))); connect(m_maxHueSlider, SIGNAL(valueChanged(int)), this, SLOT(onMaxHueChanged(int))); connect(m_colorButton, SIGNAL(clicked()), this, SLOT(onColorButtonClicked())); // 初始化颜色 updateColor(); // 设置布局 QHBoxLayout *minHueLayout = new QHBoxLayout; minHueLayout->addWidget(m_minHueLabel); minHueLayout->addWidget(m_minHueSlider); QHBoxLayout *maxHueLayout = new QHBoxLayout; maxHueLayout->addWidget(m_maxHueLabel); maxHueLayout->addWidget(m_maxHueSlider); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(minHueLayout); mainLayout->addLayout(maxHueLayout); mainLayout->addWidget(m_colorButton); setLayout(mainLayout); } void ColorSelector::updateColor() { // 根据当前选择的颜色和范围更新颜色选择器的颜色 m_palette.setColor(QPalette::Button, m_color); for (int i = m_minHue; i <= m_maxHue; ++i) { QColor color; color.setHsv(i, 255, 255); m_palette.setColor(QPalette::Button, color); } m_colorButton->setPalette(m_palette); } void ColorSelector::onMinHueChanged(int value) { // 更新最小色调值 m_minHue = value; updateColor(); } void ColorSelector::onMaxHueChanged(int value) { // 更新最大色调值 m_maxHue = value; updateColor(); } void ColorSelector::onColorButtonClicked() { // 显示颜色选择对话框 QColorDialog dialog(m_color, this); dialog.setOption(QColorDialog::ShowAlphaChannel); if (dialog.exec()) { // 更新颜色 m_color = dialog.selectedColor(); updateColor(); } } int main(int argc, char *argv[]) { QApplication a(argc, argv); ColorSelector w; w.show(); return a.exec(); } ``` 在代码中,我们创建了一个名为`ColorSelector`的自定义`QWidget`类,其中包含了一个最小色调值和最大色调值的`QSlider`、一个选择颜色的`QPushButton`以及一个颜色选择器的`QPalette`。在`updateColor`函数中,我们根据当前选择的颜色和范围更新了颜色选择器的颜色。在`onMinHueChanged`和`onMaxHueChanged`函数中,我们分别更新了最小色调值和最大色调值,并重新调用了`updateColor`函数。在`onColorButtonClicked`函数中,我们显示了颜色选择对话框,并在用户选择了颜色后更新了颜色选择器的颜色。最后,在`main`函数中,我们创建了一个`ColorSelector`对象并显示出来。 运行程序后,您可以使用滑块来选择颜色的连续范围,并使用颜色选择器来选择颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值