将 QLabel 作为参数 Qt C++传递
Passing QLabel as parameter Qt C++
我有一个小GIF,它在带有QMovie
的QLabel
上动画,我希望在GIF的动画完成后,删除Qlabel
。我试过这个,但它不起作用:
QMovie *movie = new QMovie("countdown.gif");
QLabel *processLabel = new QLabel(this);
processLabel->setMovie(movie);
movie->start();
QTimer::singleShot(1000, this, SLOT(movie_finished(backgroundLabel)));
这是我的函数:
void movie_finished(QLabel *processLabel){
processLabel->deleteLater();
}
基本的误解,这是非法的:
QTimer::singleShot(1000, this, SLOT(movie_finished(backgroundLabel)));
您不能为连接提供这样的参数。只需输入SLOT
,如下所示:
QTimer::singleShot(1000, this, SLOT(movie_finished(QLabel*)));
(至少)有三种方法可以解决这个问题。首先从插槽中删除QLabel*
参数。然后:
- 使用 QSignalMapper,它基本上封装了下面的两个替代方案。
- 在某个类中创建一个中间槽,该时隙具有 QLabel* 成员变量,然后在不带参数的槽中使用该变量,并将定时器信号连接到此时隙。
- 在插槽中使用
sender()
方法(但这通常被认为是丑陋的,破坏封装,并且首选QSignalMapper)。
这里实际上并不需要使用 QTimer 来同步电影的结尾。
实现此目的的真正简单方法是让电影在完成后删除标签:
connect(movie, SIGNAL(finished()), processLabel, SLOT(deleteLater()));
QMovie 完成后会发出finished()
。因此,只需将其连接到QLabel的deleteLater()
插槽即可。
时泄漏QMovie,所以你可能希望将其父级设置为QLabel,因为将其设置为电影并不意味着QLabel实际上清理了它。
QLabel *processLabel = new QLabel(this);
QMovie *movie = new QMovie("countdown.gif");
movie->setParent(processLabel);
相关文章:
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 如何在MS Visual Studio 2019中运行QT UI
- QT通过C++添加映射QML项目
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用Qt C++计算类似Git的SHA1哈希
- Qt focusObjectChanged example?
- 如何在cpp文件之间切换窗口?在Qt中
- Qt和C++:将QLineEdit添加到QTabWidget中
- 从数据库实时显示QT c++中的数据
- Qt Quick-如何仅从c++代码与qml属性交互
- 无法使用Qt Creator在Windows中构建yaml-cpp
- 如何在Qt中取消捕获字符串
- Qt SQLite没有查询或参数计数不匹配
- Qt SQlite无法创建表