在qt中使用连接和断开不同的进度条
Using connect and disconnect for different progress bars in an if in qt
我还在学习如何使用Qt(事实上这是我第一天这样做),我遇到了一个障碍,当它涉及到信号。我想要一个滑块它的值被进度条复制直到进度条的值达到50。一旦完成,另一个进度条将"接管"并继续复制滑块的值。
下面是我的代码:#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUi(this);
ui->progressBar->setValue(ui->horizontalSlider->value());
ui->progressBar_2->setValue(ui->horizontalSlider->value());
//connecting the slider with the second progress bar
connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->progressBar_2, SLOT(setValue(int)));
if(ui->progressBar_2->value() == 50){ //once the progress bar 2 reach 50
//disconnects the connection it had with the slider
disconnect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->progressBar_2, SLOT(setValue(int)));
//The first progress bar takes on the slider's value (50)
ui->progressBar->setValue(ui->horizontalSlider->value()); //could also have ui->progressBar->setValue(50)
//connect the slider with the first progress bar
connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->progressBar, SLOT(setValue(int)));
}
}
MainWindow::~MainWindow(){
delete ui;
}
我不知道为什么if条件被忽略了。是我写条件的方式,还是我不理解connect()和disconnect()函数?
if
条件在构造函数之外没有任何意义,您的水平滑动条值正在改变。这里最简单的方法是连接到一个插槽,在那里过滤值并更改滑块值。例如,创建一个名为updateSliders(int)
的插槽,然后使用:
connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
this, SLOT(updateSliders(int)));
下面是一个合适的槽的实现:
void MainWindow::updateSliders(int value)
{
if (value > 50) {
ui->progressBar_2->setValue(value);
} else {
ui->progressBar->setValue(value);
}
}
相关文章:
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 如何在MS Visual Studio 2019中运行QT UI
- 当套接字连接断开时检测C/C++Unix
- QT通过C++添加映射QML项目
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用Qt C++计算类似Git的SHA1哈希
- 如何从Qt绑定到在Windows 7 / 8 / 10中连接/断开USB设备事件
- Qt/C++/Valgrind 在 QTcpSocket 断开连接时读取大小为 8 无效
- 如何在Qt中安全地调用断开连接
- 在插槽断开连接时清除ExtraSelections Qt
- 如何在Qt中运行函数时断开所有信号
- 如何在基于c++/QT的安装程序中检测互联网断开连接
- 在qt中使用连接和断开不同的进度条
- 断开并稍后重新连接Qt信号