Update QLineEdit with Signal and Slot QT - c++

I am writing to ask for advice on how best to implement my code using QT library. I have a class called Action class that every one second retrieve the PC time (with gettimeofday), this value shall be displayed in the GUI. So I have a class widget that defines all the widgets necessary for the GUI. The value (expressed in seconds) will be displayed with a QLineEdit.
So my question is, how I have to implement Signal and slot to update the value in QLineEdit?
Should I emit a signal every time the function retreiveTimetoSend is called?
action.h
class Action: public object
{
Q_OBJECT
private:
Qtimer timer;
unisgned int timetosend;
private:
void retreiveTimetoSend();
public:
Action();
~Action();
public slots:
void doWork();
}
action.cpp
void retreiveTimetoSend()
{
struct timeval Now;
unsigned int Sec;
gettimeofday(&Now, NULL);
Sec = Now.tv_sec;
time.value =Sec;
}
void Action::Action()
{
timer.setInterval(1000);
connect(&timer, SIGNAL(timeout()), this, SLOT (doWork()));
timer.start();
}
void Action::doWork()
{
retreiveTimetoSend()
}
widget.h
class widgets: public QWidget
{
Q_OBJECT
private:
QLineEdit *displayTime;
public:
widget(action *w_test);
}
widget.cpp
widgets::widgets(action *w_test)
{
displayTime= new QLineEdit();
displayTime->setText(QString::number(w_test->timetosend,10));
displayTC->setStyleSheet("color: blue; background-color: red");
}
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Action *test = new Action;
Thread *threadtest = new QThread;
test->moveToThread(threadtest);
QObject::connect(threadtest, SIGNAL(started()), test ,SLOT(doWork()));
widget *mainwindows = new widget(test);
mywindow->show();
threadtest->start();
return app.exec();
}

Instead using gettimeofday use QTime::currentTime then convert it to string (chose the format) and emit result. This signal should be connected to slot QLineEdit::setText.
Using thread is completely obsolete here.

Related

Qt GUI hanging with worker in a QThread

I have a worker which needs to complete an arbitrary blocking task
class Worker : public QObject
{
Q_OBJECT;
public:
using QObject::QObject;
public slots:
void start()
{
for (int i = 0; i < 10; i++)
{
qDebug("I'm doing work...");
Sleep(1000);
}
}
};
In my MainWindow constructor, I start the task like so:
class MainWindow : public QWidget
{
Q_OBJECT;
public:
explicit MainWindow(QWidget* parent = nullptr) : QWidget(parent)
{
QThread* t = new QThread(this);
Worker* w = new Worker(this);
w->moveToThread(t);
this->connect(
t, &QThread::started,
w, &Worker::start
);
this->connect(
t, &QThread::finished,
t, &QThread::deleteLater
);
t->start();
}
};
And my entry point:
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MainWindow mainWindow{};
mainWindow.show();
return app.exec();
}
When I run the program, I just get a spinning mouse circle, and the window contents don't load until 10 seconds later (when the worker is complete). Why is this so? What do I need to do so that Worker::start is run in the background without affecting the GUI?
P.S. I am using Qt 6.2.2 and Windows 11, but I highly doubt that has anything to do with this issue.
The problem is that the worker is actually not moved to thread (Isn't a warning written to console? I bet it is.), because its parent, i.e. MainWindow instance is still in the GUI thread.
When moving to thread, you can only move the whole hierarchy of objects by moving the very top parent. You cannot have parent in different thread than its children.
You should create the worker without parent.
Worker* w = new Worker();
Of course you should also add some finished() signal to your Worker class.
class Worker : public QObject
{
Q_OBJECT;
public:
using QObject::QObject;
void start()
{
for (int i = 0; i < 10; i++)
{
qDebug("I'm doing work...");
Sleep(1000);
}
emit finished();
}
signals:
void finished();
};
and add these connections:
this->connect(
w, &Worker::finished,
t, &QThread::quit
);
this->connect(
t, &QThread::finished,
w, &Worker::deleteLater
);
otherwise your thread's even loop will not end and the worker will not be deleted.

QObject::~QObject: Timers cannot be stopped from another thread

I have this very simple Qt code:
void thread_func()
{
int a1 = 1;
const char* a2[] = { "dummy_param" };
QApplication app(a1, (char**)a2);
QMessageBox msg(QMessageBox::NoIcon, "MyTitle", "Foo bar Foo bar", QMessageBox::Ok);
msg.exec();
}
If I call the above function from my main in a std::thread, it brings up the dialog:
int main()
{
std::thread t(thread_func);
t.join();
}
...but when I close it, I get the warning message:
QObject::~QObject: Timers cannot be stopped from another thread
I've checked that the thread affinity of both QApplication instance and msg is the same. Calling the thread_func function directly from my main() (without creating a std::thread) removes that message.
I am using Qt 5.15.1 on Windows 10.
What am I missing here? Thanks
It's not allowed to operate Qt GUI directly outside the main thread(GUI thead). You can emit signals.
The warning message says it all. Use a signal/slot mechanism to accomplish the same thing.
#include <QApplication>
#include <QMessageBox>
#include <QObject>
#include <QThread>
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(){}
public slots:
void displayMessageBox()
{
QMessageBox msg(QMessageBox::NoIcon, "MyTitle", "Foo bar Foo bar", QMessageBox::Ok);
msg.exec();
this->close();
}
};
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker() {}
void start() { emit askForMessageBox(); }
signals:
void askForMessageBox();
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *widget;
Worker *worker;
QThread *thread(nullptr);
widget = new Widget();
worker = new Worker();
thread = new QThread(nullptr);
QObject::connect(worker, &Worker::askForMessageBox, widget, &Widget::displayMessageBox);
QObject::connect(thread, &QThread::started, worker, &Worker::start);
widget->show();
worker->moveToThread(thread);
thread->start();
return a.exec();
}

Routinely change QML property value from within a threaded c++ loop

I'm brand new to c++ and QML, and am struggling to call and run a threaded loop. I'd like to add an integer to a QML propery value every x milliseconds.
I'll omit my main.qml code for now, as I'm able to access the desired property; this question pertains more to c++.
void fn(QQuickItem x)
{
for (;;)
{
std::this_thread::sleep_for(std::chrono.milliseconds(100));
x->setProperty("value", x->property("value").toReal() + 10);
}
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
//QScopedPointer<TachometerDemo> Tacho(new TachometerDemo);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
QQuickItem *item = engine.rootObjects().at(0)->findChild<QQuickItem*>
("circularGauge");
thread t1(fn, item);
return app.exec();
}
Any guidance on the most efficient means for achieving the above desired functionality would be appreciated. Whilst I'm currently testing on a win32 platform, a cross-platform approach is required.
Better use Qt's event loop mechanism and SIGNAL/SLOTS. And QThreads if needed. Also if you need to update value in QML in msecconds duration don't do it over setProperty, this function call takes too long. You can update your value directly in QML using JS. But if you need to update QML value from C++ you can do it this way:
test.h
#include <QObject>
#include <QTimer>
class Test : public QObject
{
Q_OBJECT
public:
Test();
Q_PROPERTY(int value READ value NOTIFY valueChanged)
int value(){return this->m_value;}
signals:
void valueChanged();
private slots:
void timeout();
private:
int m_value;
QTimer * m_timer;
};
test.cpp
#include "test.h"
Test::Test():m_value(0)
{
this->m_timer = new QTimer(this);
this->m_timer->setInterval(100);
connect(this->m_timer, &QTimer::timeout, this, &Test::timeout);
this->m_timer->start();
}
void Test::timeout()
{
this->m_value += 10;
emit valueChanged();
}
in your main.cpp
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty(QStringLiteral("Test"), new Test());
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
Somewhere in your QML:
Label
{
text: Test.value
anchors.centerIn: parent
}
This is the fastest way to update QML value from C++

How to check how long a button is pressed?

I'm looking for a way to check how long a button is pressed! My idea was starting a counter which starts when pressed() is emitted and stops when released() is emitted. But I currently don't know how to wait for the released- or similar events. Normally I would connect() a signal with a slot but in this case both are slots. Maybe you've got a better idea maybe this one is good enough.
When pressed. Start a timer. When released check for how long the timer has been running (and stop it). Save the elapsed time in a variable in the class.
That's one way to do it at least.
When the pressed signal is sent, start the timer. When the relesed signal is sent, read how much time has elapsed on the timer.
This is a complete example using Qt 5 and a C++ compiler that's not prehistoric:
// https://github.com/KubaO/stackoverflown/tree/master/questions/button-timer-38645219
#include <QtWidgets>
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QWidget w;
QFormLayout layout{&w};
QPushButton button{"Press Me"};
QLabel label;
layout.addRow(&button);
layout.addRow(&label);
QElapsedTimer timer;
QObject::connect(&button, &QPushButton::pressed, [&]{ timer.start(); });
QObject::connect(&button, &QPushButton::released, [&]{
label.setText(QStringLiteral("Pressed for %1 ms").arg(timer.elapsed()));
});
w.show();
return app.exec();
}
Of course you can shove all the variables into a class. You'll notice that C++11 makes this transformation almost a no-brainer: it's by design, not by coincidence.
#include <QtWidgets>
class Widget : public QWidget {
QFormLayout layout{this};
QPushButton button{"Press Me"};
QLabel label;
QElapsedTimer timer;
public:
Widget() {
layout.addRow(&button);
layout.addRow(&label);
connect(&button, &QPushButton::pressed, [&]{ timer.start(); });
connect(&button, &QPushButton::released, [&]{
label.setText(QStringLiteral("Pressed for %1 ms").arg(timer.elapsed()));
});
}
};
int main(int argc, char ** argv) {
QApplication app{argc, argv};
Widget w;
w.show();
return app.exec();
}
It is generally frowned upon to have "business logic" in UI objects, so we can separate out the functionality into that of a Timer, a Widget, and of a Controller. The code external to all of these objects sets up the requisite connections.
#include <QtWidgets>
class Timer : public QObject {
Q_OBJECT
QElapsedTimer timer;
public:
Q_SLOT void start() { timer.start(); }
Q_SLOT void stop() { emit elapsed(timer.elapsed()); }
Q_SIGNAL void elapsed(qint64);
};
class Widget : public QWidget {
Q_OBJECT
QFormLayout layout{this};
QPushButton button{"Press Me"};
QLabel label;
public:
Widget() {
layout.addRow(&button);
layout.addRow(&label);
connect(&button, &QPushButton::pressed, this, &Widget::pressed);
connect(&button, &QPushButton::released, this, &Widget::released);
}
Q_SIGNAL void pressed();
Q_SIGNAL void released();
Q_SLOT void setText(const QString & text) { label.setText(text); }
};
class Controller : public QObject {
Q_OBJECT
public:
Q_SLOT void elapsed(qint64 ms) {
emit hasText(QStringLiteral("Pressed for %1 ms").arg(ms));
}
Q_SIGNAL void hasText(const QString &);
};
int main(int argc, char ** argv) {
QApplication app{argc, argv};
Timer t;
Widget w;
Controller c;
w.show();
//
QObject::connect(&w, &Widget::pressed, &t, &Timer::start);
QObject::connect(&w, &Widget::released, &t, &Timer::stop);
QObject::connect(&t, &Timer::elapsed, &c, &Controller::elapsed);
QObject::connect(&c, &Controller::hasText, &w, &Widget::setText);
return app.exec();
}
#include "main.moc"
Of course you can scream that the controller, even if not really coupled to the other code, is still somewhat dependent on the design of the rest of the code. Thankfully, it doesn't have to be so: lambdas make simple adaptations simple.
We can use a "foreign" controller, provided by our big business vendor, too:
// use Timer and Widget from preceding example
#include <sstream>
#include <string>
#include <functional>
class Controller {
public:
using callback_t = std::function<void(const std::string&)>;
Controller(callback_t && callback) : callback{std::move(callback)} {}
void onElapsed(int ms) {
std::stringstream s;
s << "Pressed for " << ms << " ms";
callback(s.str());
}
private:
callback_t callback;
};
int main(int argc, char ** argv) {
QApplication app{argc, argv};
Timer t;
Widget w;
Controller c{ [&](const std::string & s){ w.setText(QString::fromStdString(s)); } };
QObject::connect(&w, &Widget::pressed, &t, &Timer::start);
QObject::connect(&w, &Widget::released, &t, &Timer::stop);
QObject::connect(&t, &Timer::elapsed, [&](qint64 ms) { c.onElapsed(ms); });
w.show();
return app.exec();
}
#include "main.moc"
All of the examples above are compileable: put any one of them into main.cpp, rebuild the project, and go.
When you're posting test cases for questions, you should go backwards: you start somewhere towards the end of this answer, and work your way towards the beginning, minimal setup. It'll help you understand your own code better, too, since the verbosity is minimized. It's telling, I hope, that both the first and the last version have same functionality.

QTimer with QPaintEvent

I've created a small QT application that redraws a circle at a random position.
What I would like to do is repeat the method a predetermined number of times that draws the circle every second using a QTimer.
I am not sure how to go about to this.
Here is my main.cpp
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
srand (time(NULL));
QApplication app(argc, argv);
widget f;
f.show();
return app.exec();
}
widget.cpp
#include "widget.h"
widget::widget()
{
widget.setupUi(this);
}
void widget::paintEvent(QPaintEvent * p)
{
QPainter painter(this);
//**code
printcircle(& painter); //paints the circle
//**code
}
void paintcircle(QPainter* painter)
{
srand (time(NULL));
int x = rand() %200 + 1;
int y = rand() %200 + 1;
QRectF myQRect(x,y,30,30);
painter->drawEllipse(myQRect);
}
widget::~widget()
{}
widget.h
#ifndef _WIDGET_H
#define _WIDGET_H
class widget : public QWidget {
Q_OBJECT
public:
widget();
virtual ~widget();
public slots:
void paintEvent(QPaintEvent * p);
private:
Ui::widget widget;
};
#endif /* _WIDGET_H */
How would I go about creating a Qtimer to repeat the printcricle() method.
Thanks
You can create a timer in your widget class constructor as:
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
I.e. it will call the widget's paint event every second.
Right, there are a couple of things to modify in your code in order to accomplish this:
Forward declare a QTimer
Add a QTimer member
Include the QTimer header.
Set a continuous QTimer in the constructor of your widget class.
Make sure you set the connection to the update slot so that a repaint is scheduled by the event loop.
You need to add a counter for the predetermined times as there is no such feature built into QTimer.
You need to initialise that variable to zero.
You need to increment that in each slot call.
You need to stop emitting the timeout signal for the QTimer.
In order to achieve all that above, your code would become something like this:
widget.cpp
#include "widget.h"
#include <QTimer>
// Could be any number
const static int myPredeterminedTimes = 10;
widget::widget()
: m_timer(new QTimer(this))
, m_count(0)
{
widget.setupUi(this);
connect(m_timer, SIGNAL(timeout()), SLOT(update()));
timer->start(1000);
}
void widget::paintEvent(QPaintEvent * p)
{
QPainter painter(this);
//**code
printcircle(& painter); //paints the circle
//**code
}
void widget::paintcircle(QPainter* painter)
{
srand (time(NULL));
int x = rand() %200 + 1;
int y = rand() %200 + 1;
QRectF myQRect(x,y,30,30);
painter->drawEllipse(myQRect);
}
widget::~widget()
{}
widget.h
#ifndef _WIDGET_H
#define _WIDGET_H
class QTimer;
class widget : public QWidget {
Q_OBJECT
public:
widget();
virtual ~widget();
public slots:
void paintEvent(QPaintEvent * p);
private:
Ui::widget widget;
private:
QTimer *m_timer;
int m_count;
};
#endif /* _WIDGET_H */