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.
Related
I have a QProgressDialog that I want to display immediately
QProgressDialog *progress = new QProgressDialog("Downloading files...",
"Cancel", 0, 2*selection.size()+1);
progress->setMinimumDuration(0);
progress->setWindowModality(Qt::WindowModal);
progress->setValue(0);
Then I run a for loop with the task and finally assign the maximum value:
for (int i = 1; i < selection.size()+1; ++i)
{
progress->setValue(2*i-1);
if (progress->wasCanceled())
break;
do_half_task();
progress->setValue(2*i);
if (progress->wasCanceled())
break;
do_second_half();
}
progress->setValue(2*selection.size()+1);
But with this code, the dialog window borders appear, transparents without any widgets inside, and only gets filled with the label and progressbar when a full for loop has completed.
I think this is because only after a full loop has completed is that Qt can compute the duration of each step, and check that it will be >0 which I am setting as minimum duration. However, from the docs I see
minimumDuration : int
If set to 0, the dialog is always shown as soon as any progress is set. The default is 4000 milliseconds.
From where I would've expected the dialog to show up immediately in the first loop pass after setting progress->setValue(1).
How can I get my QProgressDialog to appear immediately?
Not sure if this matches for Qt too
but in C# if you run the execution of your code in the same thread like
ProcessBar p = new ProcessBar();
this.Controls.Add(p);
...
for (int i = 0; i < 100; i++) {
p.Value = i;
Thread.Sleep(1);
}
then you have the problem that your Form does not get to the code where it is redrawn.
Maybe try making your execution loop in a nother thread?
When you make the main thread go into a loop, it can't do any event processing until the loop ends and your method returns.
So it can only process all "paint update" requests once you are done.
You can call QCoreApplication::processEvents() inside the loop to allow it to return to event processing for a while.
To show the dialog immediately then just call QProgressDialog::show().
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).
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.
I have finished my first Qt application, and noticed that the QApplication constructor in the main.cpp files take up to 10 seconds to execute. This results in an annoying startup delay where I can't even show a splash screen.
When profiling this delay it turns out that the initializeMultitouch_sys method in the QApplicationPrivate class is the culprit. Specifically, the iInkTablets->get_Count(...) call takes all the time.
void QApplicationPrivate::initializeMultitouch_sys()
{
[...]
IInkTablets *iInkTablets = 0;
HRESULT hr = CoCreateInstance(QT_CLSID_InkTablets, NULL, CLSCTX_ALL, QT_IID_IInkTablets, (void**)&iInkTablets);
if (SUCCEEDED(hr)) {
long count = 0;
iInkTablets->get_Count(&count); // <== Takes 5-10 seconds!!
for (long i = 0; i < count; ++i) {
[...]
}
}
I am using Windows 7, but not utilizing any multi-touch feature. Any idea what causes this problem and how I can avoid it?
Thanks,
Fabian
UPDATE 2010-11-14 - PROBLEM SOLVED
I noticed that the problem then occured with all Qt based applications, including Qt Designer. A reboot fixed it.
This is fixed in 4.6.3 (QTBUG-6007/commit)
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. ;)