Qt Making a Timer using QLCDNumber - qt

I am trying to make two timers using QLCDNumber. These timers will be generated as a part of my status bar, in a dll. I have two LCDNumber displays. lcdNumber1 will start at a specified time (e.g. 12:00:00). lcdNumber2 will start at 0 (e.g. 00:00:00).
How am I able to create a timer for lcdNumber2 and let it to start ticking?
How can I add lcdNumber2's timer to lcdNumber1? Or can I create a timer for lcdNumber1 to start ticking from the specified time?
Could anyone please help?
QLCDNumber *lcdNumber1 = new QLCDNumber;
lcdNumber1->setNumDigits(8);
lcdNumber1->display(12:00:00);
statusBar->addWidget(lcdNumber1);
QLCDNumber *lcdNumber2 = new QLCDNumber;
lcdNumber2->setNumDigits(8);
lcdNumber2->display(00:00:00);
statusBar->addWidget(lcdNumber2);

Inherit QLCDNumber adding variable time to hold current time and another slot tick()
QLCDNumber_my::tick(){
time++;
this->display(time);
}
and then
QLCDNumber_my *lcdNumber1 = new QLCDNumber_my;
lcdNumber1->setNumDigits(8);
lcdNumber1->display(12:00:00);
statusBar->addWidget(lcdNumber1);
QTimer *timer = new QTimer(this);
timer->start(1000);
connect(timer, SIGNAL(timeout()), lcdNumber1, SLOT(tick()));
QLCDNumber is simple displaying widget, it cannot run, to produce time change you need to use sepatare timer (QTimer).

Related

connect current time and date to the timeEdit and dateEdit in qt

in qt, I want to connect the current time with the timeEdit so when I launch the application, the time progresses.
I wrote this:
QDate myD = iDate->currentDate();
QTime myT = iTime->currentTime();
ui->dateEdit->setDate(myD);
ui->timeEdit->setTime(myT);
connect (dte, SIGNAL(getDate(myD)), ui->dateEdit, SLOT(setDate(myD)));
connect(dte, &QTimeEdit::timeChanged, ui->timeEdit,&QTimeEdit::setTime);
NB: QDateTimeEdit* dte = new QDateTimeEdit; declared in the .h file.
when I launch the app, the time is still freezing
There is nothing in the example code that is running to update the time. I would think you would need to start a 1 second timer and every timeout you update the QDateTimeEdit with the current QDateTime::currentDateTime.
Something like this:
QTimer* updatetimer = new QTimer();
connect(updatetimer, &QTimer::timeout, this, &UiClass::UpdateTimerTimeout)
updateTimer->start(1000);
UpdateTimerTimeout {
ui->dateEdit->setDate(QDateTime::currentDateTime().date());
ui->timeEdit->setTime(QDateTime::currentDateTime().time());
}
Something like that should do what you want.

day elapsed use of progress bar in Qt?

I want to show the percentage of day elapsed using the progress bar in Qt,
for example, if its 12pm it should show 12/24*100=50 on the progress bar.
Please help!
Create a QTimer which calls slot update() every n minutes/hours:
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000 * 60 * 60); // call update slot every hour
This slot then updates the value of the progressbar:
QDateTime dateTime = QDateTime::currentDateTime();
progressBar->setValue(dateTime.time().hour()/24*100);

What is difference between new or instance directly about QPropertyAnimation?

the code as follow doesn't create the animation... But if the QPropertyAnimation is a new instance , then it can.. Why? what is the difference ? Thank you...
QRect orgRect = this->geometry();
QRect endRect = btExpand ? QRect(*ptNotePadPot, COLLAPSE_SIZE) :
QRect(*ptNotePadPot, EXPAND_SIZE);
/*
QPropertyAnimation* amt = new QPropertyAnimation(this, "geometry", this);
amt->setDuration(10000);
amt->setStartValue(orgRect);
amt->setEndValue(endRect);
amt->start();
*/
QPropertyAnimation amt(this, "geometry", this);
amt.setDuration(10000);
amt.setStartValue(orgRect);
amt.setEndValue(endRect);
amt.start();
After it starts, QProperyAnimation will create its own timer and run outside your main thread.
QPropertyAnimation amt(this, "geometry", this);
it will be destroyed after run pointer exit your function scope.
otherwise if u use
QPropertyAnimation* amt = new QPropertyAnimation(this, "geometry", this);
you will create one animation object in the memory to work and its pointed by amt pointer.
the pointer will be deleted after run pointer exit your function scope, not the QPropertyAnimation object.
but it will be a zombie in the memmory if you not delete it.
the best way is, use a class variable for QPropertyAnimation pointer. so you can delete the object in the pointer after program closed or when ever u want.
i hope it helps..
sory for my bad english.
A better way to solve this without memory leak is to call the start() method like this:
animation->start(QAbstractAnimation::DeleteWhenStopped);

How to read each line of a QPlainTextEdit in Qt?

I want to make a program where I take each line of QPlainTextEdit and send it to a WebView which will load those urls. I don't need to check the URL's because the system makes it like that
http://someurl.com/ + each line of the QPlainTextEdit
I have few ideas which I don't know how to use:
Use a foreach loop which will make its self wait 5 seconds to loop again
Make a QTimer to wait like 5 seconds and tick with an integer and when the integer hits the number of lines it will stop
And all of that will be done on every 4 hours by waiting with another timer.
First of all you need the contents of the QPlainTextEdit. Get them and split them using the new line separator to get a list of QStrings each representing a line.
QString plainTextEditContents = ui->plainTextEdit->toPlainText()
QStringList lines = plainTextEditContents.split("\n");
The easiest way to process the lines is to use a QTimer and store somewhere the current index in the list.
// Start the timer
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(processLine()));
timer->start(5000);
Now the slot is called whenever the timer is triggered. It just gets the current line and you do it whatever you want.
void processLine(){
// This is the current index in the string list. If we have reached the end
// then we stop the timer.
currentIndex ++;
if (currentIndex == lines.count())
{
timer.stop();
currentIndex = 0;
return;
}
QString currentLine = lines[currentIndex];
doSomethingWithTheLine(currentLine);
}
Similarly do the same with the 4h timer.

Trouble with progressbar in QT

i m facing problem in giving animation to progressbar in QT.
where is the mistake in the following code, i am getting continues progress bar, but its not animating
QApplication a(argc, argv);
QProgressDialog *dialog = new QProgressDialog();
QProgressBar *pbar = new QProgressBar(dialog);
pbar->setMinimum(0);
pbar->setMaximum(0);
pbar->setTextVisible(false);
QDesktopWidget *desktop = QApplication::desktop();
QRect rect = desktop->geometry();
pbar->setGeometry(rect.left(),rect.top(),rect.right(),rect.bottom()-300);
pbar->show();
dialog->setBar(pbar);
dialog->showMaximized();
dialog->exec();
return a.exec();
I tried this code on WinXP with Qt 4.5.3 and it works as expected.
I cannot give you a solution but i have a suggestion:
You don't need to set a QProgressBar to QProgressDialog, it already has its own.
Removing the code for QProgressBar, the code below does the same thing with your original code on my machine.
QApplication a(argc, argv);
QProgressDialog *dialog = new QProgressDialog();
dialog->setMinimum(0);
dialog->setMaximum(0);
dialog->showMaximized();
dialog->exec();
return a.exec();
If you're using the Windows Vista theme (QWindowsVistaStyle) then there's a bug that means indeterminate progress bars don't animate. I've written up the bug here, complete with simple patch: http://bugreports.qt-project.org/browse/QTBUG-10984
Dudes, what exactly do you think that a progress bar does? It is supposed to show the user that an activity is ongoing and also what is the current progress state.
Your code
QProgressDialog *dialog = new QProgressDialog();
dialog->setMinimum(0);
dialog->setMaximum(0);
would indicate that a certain operation will start with status 0 and will end when the status (or current value) reaches value ... 0. And you want some animation going with that?
See an example at http://doc.trolltech.com/4.6/qprogressdialog.html#details
Basically you should create a progress dialog with a min and a max value
QProgressDialog *dialog = new QProgressDialog();
dialog->setMinimum(0);
dialog->setMaximum(100);
Then have the actual progress value updated (e.g. on a timer which triggers the perform slot) in order to have it represented in the progress bar:
void Operation::perform()
{
dialog->setValue(steps);
//... perform one percent of the operation
steps++;
if (steps > dialog->maximum())
t->stop();
}
A series of updates, with progressively increasing progress value, will create the animation effect you want.
Obviously tagging this with Symbian is pure wrong, this is not at all Symbian specific. Nor is it Qt 4.x specific, hell ... it's not even Qt specific, it's basically a logic issue. ;)

Resources