I'm trying to change the image on QLabel in Qt.
At first, I did the basic set image as follows:
void MainWindow::setMainDisplayNew(QString imageName){
QPixmap pix7 = imageName;
QPixmap pix8 = pix7.scaled(QSize(720,480), Qt::KeepAspectRatio);
ui->mainDisplay->setStyleSheet(imageName);
ui->mainDisplay->setPixmap(pix8);
}
Now I want to change this so I can pass 2 arrays. List of images and duration they should appear for and I want the display to show them for the indicated duration.
void MainWindow::setMainDisplay(QString imageName[], int size)
{
for(unsigned int i=0; i<size; i++)
{
QTimer * timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [=](){
setMainDisplayNew(imageName[i]);
timer->deleteLater(); // ensure we cleanup the timer
});
timer->start(3000);
}
}
EDIT
With the help of the responses, I reached the above code. I am sending the 3 images. It is display the final image after 3 seconds and stays as is... Any help?
while(true){
This is your problem. Qt is an event-driven framework. The while(true) prevents events from being processed. Such events include the timeout from the QTimer and updating of the GUI. You need to allow the function to exit.
In addition, you're not cleaning up your timers, so you're leaking memory every time you enter the function (although that's currently only once!). You can clean up with a call to deleteLater.
Using C++ 11, it would be something like this: -
for(int i=0; i<mySize; i++)
{
QTimer * timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [=](){
setMainDisplayNew(imageName[i]);
timer->deleteLater(); // ensure we cleanup the timer
});
timer->start(duration[i]);
}
Related
I have a QT GUI application in c++ that aims to process and generates binary files.
The App works fine but it looks like it's frozen when it enters the while loop of processing and writing in the file.
I solve this by coping qApp->processEvents(); into the while loop. but the problem is that it takes much more time for generating a file:
without qApp->processEvents(); in the loop => it takes 4second
with qApp->processEvents(); in the loop => it takes 50 second for exact the same file
for (unsigned long int k=0; k<DATASIZE ; k++){
qApp->processEvents();
/* Some process*/
DataToFile.push_back(Process_Function(DATA));
}
/*Generating file*/
myFile.write((char *)&DataToFile[0], DataToFile.size()*sizeof (float));
DATA SIZE around a couple of millions
Process_Function: take specific data, calculate the value and return it back.
Questions:
1- Is there a way to process the data, generate files without being frozen and without the huge delay of the qApp->processEvents();
2- Is it possible to run qApp->processEvents(); in another thread? / OR is there another way to do it?
First, create an object that encapsulates your work:
class Generator : public QObject {
Q_OBJECT
signals:
void progress(float);
void done();
slots:
void doWork() {
QVector<float> DataToFile;
for (unsigned long int k=0; k<DATASIZE ; k++){
/* Some process*/
DataToFile.push_back(Process_Function(DATA));
if (k % 100 == 0) { // Inform the UI thread every 100 datapoints
emit progress(k/DATASIZE);
}
}
myFile.write((char *)&DataToFile[0], DataToFile.size()*sizeof (float));
emit done();
}
};
Then create a new thread and have the object do its work there:
QThread *t = new QThread(this);
Generator *g = new Generator;
g->moveToThread(t);
QObject::connect(t, &QThread::started, g, &Generator::doWork);
QObject::connect(g, &Generator::done, t, &QThread::quit);
QObject::connect(t, &QThread::finished, g, &QObject::deleteLater);
QObject::connect(t, &QThread::finished, t, &QThread::deleteLater);
t->start();
The first two connect statements tie the lifetime of the generator to that of the thread, the last two clean up everything once the thread exits.
And you can of course connect to the Generator::progress signal to monitor the generation progress.
How can I start a loop on a different thread by pressing a QPushButton?
The main idea is, when pushButtonStart is clicked, start a loop on a QThread, and when pushButtonStop is clicked, stops the loop in the QThread.
The loop can be done by QTimer, for loop or while loop, but he needs a way to stop by pressing a button.
I have this code to create a new timer and set up a connection when it fires. This is in the Start code.
checkTimer = new QTimer(this);
connect( checkTimer, SIGNAL(timeout()), this, SLOT(checkTimerFired()) );
checkTimer->start(3000);
My "stop running" button sets programCreated to false, and checkTimerFired starts with this:
if (!programCreated) {
checkTimer->stop();
return;
}
That should be the Qt-specific things you need. The rest is simple C++.
To do the loop, I use QTimer, which run the slot pressButton multiple times until stop(); function of QTimer is executed. Of course, to use this method I have to recode the program, to improve the algorithm.
QTimer loop:
void MainPrograma::on_pushTurnOn_clicked()
{
tierLoop = new QTimer(this);
timerLoop->setInterval(5000);
timerLoop->setSingleShot(true);
connect(timerLoop, SIGNAL(timeout()), SLOT(on_pushTurnOn_clicked()));
QPushButton *ThirdButton = uimain->QPushButton_3;
Nmb2 = 2
Nmb4 = 4;
if(Busy==1){
ThirdButton->setEnabled(false);
}
if(Busy==0){
timerLoop->start(); //start the loop
StartFunction(Nmb2, Nmb4);
if(X==1){
ThirdButton->setEnabled(false);
}
if(X==0){
ThirdButton->setEnabled(true);
}
}
}
void MainPrograma::on_pushTurnOff_clicked()
{
timerLoop->stop(); //stop the loop
}
QTimer declaration on .h:
private:
QTimer *timerLoop;
I still do not understand how to use QThread... But the loop is already answered!
timer = new QTimer(this);
timer->setInterval(50);
QPushButton *start = new QPushButton("Start/Stop", this);
start->setText("Start/Stop");
layout->addWidget(start);
connect(start, &QPushButton::clicked, this, [this]() {
if (!timer->isActive()) {
timer->start();
} else {
timer->stop();
}
});
connect(timer, &QTimer::timeout, this, [&slider, sliderDisplay]() {
//increment slider value until at max, then reset to min
}
How can I use another slider to change the interval of timeouts emitted by my QTimer? I've tried using setInterval but it seems that I can't change the interval once it is set. Is the best way to do this just delete this QTimer and create a new QTimer with the specified interval every time?
OP has claimed that
How can I use another slider to change the interval of timeouts emitted by my QTimer? I've tried using setInterval but it seems that I can't change the interval once it is set.
I wondered a bit about that claim because I could've sworn that you can change the interval at any time.
The only restriction, I would consider: the new set interval might not be considered before the next timeout happens. (Usually, I use single-shot timers or timers with small intervals so that you wouldn't notice the difference.)
While playing with my demo, I got the impression that a call of QTimer::setInterval() restarts the interval.
Unfortunately, the doc. of QTimer::setInterval() doesn't mention this behavior explicitly, except:
Setting the interval of an active timer changes its timerId().
Thanks to #Scopchanov who had the look into source code (I was too lazy to).
QTimer::setInterval():
void QTimer::setInterval(int msec)
{
inter = msec;
if (id != INV_TIMER) { // create new timer
QObject::killTimer(id); // restart timer
id = QObject::startTimer(msec, Qt::TimerType(type));
}
}
So, in fact, setting the interval while the timer is running, it kills the currently running timer and restarts a new one.
My MCVE for demonstration – testQTimer.cc:
// Qt header:
#include <QtWidgets>
// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
QWidget qWinMain;
qWinMain.setWindowTitle("Test QTimer");
QFormLayout qForm;
QSlider qSliderInterval(Qt::Horizontal);
qSliderInterval.setRange(100, 1000);
qForm.addRow("Interval: ", &qSliderInterval);
QSlider qSliderStep(Qt::Horizontal);
qSliderStep.setRange(1, 10);
qForm.addRow("Step: ", &qSliderStep);
QPushButton qBtnStartStop("Start / Stop");
qForm.addRow("Timer:", &qBtnStartStop);
QSlider qSliderAnim(Qt::Horizontal);
qForm.addRow("Animation:", &qSliderAnim);
qSliderAnim.setRange(0, 100);
qWinMain.setLayout(&qForm);
qWinMain.show();
// setup timer
QTimer qTimer;
qTimer.setInterval(qSliderInterval.value());
// install signal handlers
QObject::connect(&qSliderInterval, &QSlider::valueChanged,
[&](int value) { qTimer.setInterval(value); });
QObject::connect(&qBtnStartStop, &QPushButton::clicked,
[&]() {
if (qTimer.isActive()) qTimer.stop();
else qTimer.start();
});
QObject::connect(&qTimer, &QTimer::timeout,
[&]() {
int value = qSliderAnim.value() + qSliderStep.value();
if (value > qSliderAnim.maximum()) {
value -= qSliderAnim.maximum() - qSliderAnim.minimum();
}
qSliderAnim.setValue(value);
});
// runtime loop
return app.exec();
}
Output:
Qt Version: 5.13.0
I'm trying to understand the correct way to update a widget at frame-time.
The specific problem I'm trying to solve is to set the remaining time of a timer on a label.
I created and started the timer
MainTimer = new QTimer(this);
MainTimer->setSingleShot(true);
MainTimer->start(5000);
and on the QML I have a label, UI_MainTimerLabel, that I can access through ui->UI_MainTimerLabel->setNum(int).
Since the QTimer doesn't provide a OnTimerUpdate signal or callback method, I suppose I have to create some kind of loop to read the timer's value and set it to the label.
Should I do it through a QThread?
QThread::create([&]() {
while(true)
{
ui->UI_RemainingTimer->setNum(MainTimer->remainingTime());
}
})->start();
(note: I know that this won't work, but it's not a problem since I'm just trying to understand the concept)
Should I use a 0-timed QTimer?
UpdateTimer = new QTimer(this);
//{binding the UpdateTimer end signal to a ui->UI_RemainingTimer->SetNum(MainTimer->RemainingTimer() function}
UpdateTimer->start(0);
Should I use a QEventLoop (but I have yet to fully understand what is their correct usage)?
Should I use a user-created "MyTimerLabel" widget that self-updates (in which virtual overridden method?)?
Or is there some other correct way to manage a frame-time update, that I couldn't understand? (I'm trying to get the general correct approach, not the solving approach of this specific problem, though)
Thanks in advance
Is it necessary to update the GUI at every moment? No, each frame is updated every 30ms so something appropriate is to update half of that time, that is 15 ms. So the second timer is set to that period by calculating the remaining time showing it in the GUI:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTimer main_timer;
main_timer.setSingleShot(true);
QTimer update_timer;
QLabel label;
label.setAlignment(Qt::AlignCenter);
label.resize(640, 480);
QObject::connect(&update_timer, &QTimer::timeout, [&main_timer, &update_timer, &label](){
int rem = main_timer.remainingTime();
if(rem <0){
label.setNum(0);
update_timer.stop();
}
else{
label.setNum(rem);
}
});
label.show();
main_timer.start(5000);
update_timer.start(15);
return a.exec();
}
I need to capture video from a live camera plugged to my PC ethernet socket. I used Phonon to capture video from a file in my system first. It works fine. Then, I created a socket to read the video. Here, I do not know how to get the buffered data and set it as a source to my Phonon video! I would thank if anyone could help me with this.
Here's the code to read a video :
void PlayVideo::rollOn()
{
media = new Phonon::MediaObject(movieLabel);
media->setCurrentSource(Phonon::MediaSource(QString("/home/saman/4.7/phonon_test/sample.mp4")));
videoPlayer = new Phonon::VideoPlayer(Phonon::VideoCategory, movieLabel);
videoPlayer->setFixedSize(QSize(400, 300));
videoPlayer->show();
connect(videoPlayer, SIGNAL(finished()), videoPlayer, SLOT(deleteLater()));
videoPlayer->play(media->currentSource());
}
and this is how I added sockets to the code:
void PlayVideo::rollOn()
{
udpSocketin = new QUdpSocket(this);
udpSocketin->bind(localPort);
connect(udpSocketin, SIGNAL(readyRead()),this, SLOT(readDatagrams()));
QDataStream out(&datagramout, QIODevice::WriteOnly);
out.setVersion (QDataStream::Qt_4_7);
timer2 = new QTimer(this);
connect(timer2, SIGNAL(timeout()), this, SLOT(playbuff()));
media = new Phonon::MediaObject(movieLabel);
media->setCurrentSource(Phonon::MediaSource(QString("/home/saman/4.7/phonon_test/sample.mp4")));
//media->setCurrentSource (Phonon::MediaSource());
videoPlayer = new Phonon::VideoPlayer(Phonon::VideoCategory, movieLabel);
videoPlayer->setFixedSize(QSize(400, 300));
videoPlayer->show();
connect(videoPlayer, SIGNAL(finished()), videoPlayer, SLOT(deleteLater()));
videoPlayer->play(media->currentSource());
}
void PlayVideo::readDatagrams()
{
if(udpSocketin->hasPendingDatagrams ())
{
datagramin.resize (udpSocketin->pendingDatagramSize ());
qint64 receiveBytes = udpSocketin->readDatagram (datagramin.data (), datagramin.size ));
if(receivedBytes <= 0)
{
qDebug("receivedBytes <= 0");
}
}
}
You can put your data into a QBuffer, which is a subclass of QIODevice. Then, there's a media source constructor that accepts a QIODevice.