I have an LCDNumber display panel in QT. I want to update the value of it continuously with a variable being received from an external servo motor (the speed)
I have the following code
HANDLE RS232Handle;
UCHAR Address = 0;
UCHAR Status = 0;
int Value = 0;
GetResult(RS232Handle, &Address, &Status, &Value);
printf("Result: Address=%d, Status=%d, Value=%d\n", Address, Status, Value);
ui->lcdNumber_TarRot_Status->display(Value);
All these lines must run to get the proper value. I have looked into calling a function every x seconds, and I have tried a for loop that runs forever, but nothing really works as desired. Is there a proper way of doing this?
Thanks!
I don't know how you tried to "calling a function every x seconds" - most likely you used a blocking wait to do so. Instead, call it from a timer, without blocking the event loop.
class MyClass : public QWidget {
Q_OBJECT
Ui::MyClass ui;
HANDLE m_device = 0;
QBasicTimer m_queryTimer;
void timerEvent(QTimerEvent *event) override {
if (event->timerId() == m_queryTimer.timerId())
queryDevice();
}
void queryDevice() {
UCHAR address = 0;
UCHAR status = 0;
int value = 0;
GetResult(m_device, &address, &status, &value);
qDebug() << "Result: Address" << address << "Status" << status << "Value" << value;
ui->lcdNumber_TarRot_Status->display(value);
}
}
...
public:
explicit MyClass(QObject *parent = nullptr) : QObject(parent) {
ui.setupUi(this);
m_queryTimer.start(1000, this);
...
}
void openDevice() {
...
m_device = ...;
}
};
I ended up using a QTimer since I'm working with QT:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateMCvalues()));
timer->start();
}
void MainWindow::updateMCvalues() {
HANDLE RS232Handle;
UCHAR Address = 0;
UCHAR Status = 0;
int Value = 0;
GetResult(RS232Handle, &Address, &Status, &Value);
printf("Result: Address=%d, Status=%d, Value=%d\n", Address, Status, Value);
ui->lcdNumber_TarRot_Status->display(Value);
}
From the Qt docs on the QLCDNumber class. display() is a slot, not a public function. Calling it directly won't work unless on the UI thread. See here for info on signals and slots if you are unfamiliar.
The proper usage would be to connect a signal of your choosing (i.e. make your own) to the ````display()``` slot.
Say you made a signal called output_number, once connected to the display() slot. You could call:
emit output_number(Value);
Which would in turn call the display slot on the Qt UI thread.
Related
How to properly connect to QObject's destroyed signal from Qt Script?
When I connect to it like to your average signal, it does not work. I did test that I removed the object and that other QObjects did get the signal, but the script function I connected to it is not invoked.
Below is the sample I'm using for testing. The most important part is the line:
_engine.evaluate("obj.destroyed.connect(function() { debug(\"obj destroyed\") })");
I'd expect it to invoke that function when obj is destroyed. The code below ensures that object is deleted when even loop already started and it also tests that the object did send destroyed signal to another QObject. What I want to fix is for the script to invoke the script function with debug("obj destroyed") after destroyed signal is emitted.
ScriptTester.h:
#ifndef SCRIPTTESTER_H
#define SCRIPTTESTER_H
#include <QtScript>
#include <QtCore>
class ScriptTester: public QObject {
Q_OBJECT
public:
explicit ScriptTester(QObject *parent = 0);
private slots:
void signalTest();
void boo();
private:
QScriptEngine _engine;
};
#endif // SCRIPTTESTER_H
ScriptTester.cpp:
#include "ScriptTester.h"
static QScriptValue scriptDebug(QScriptContext *context, QScriptEngine *engine) {
Q_UNUSED(engine);
if (context->argumentCount() >= 1) {
QString msg = context->argument(0).toString();
for (int i = 1; i < context->argumentCount(); ++i) {
msg = msg.arg(context->argument(i).toString());
}
qDebug() << msg;
}
return QScriptValue();
}
ScriptTester::ScriptTester(QObject *parent) :
QObject(parent)
{
QTimer::singleShot(0, Qt::CoarseTimer, this, SLOT(signalTest()));
_engine.globalObject().setProperty("debug", _engine.newFunction(scriptDebug, 1));
}
void ScriptTester::signalTest() {
QObject *obj = new QObject(this);
_engine.globalObject().setProperty("obj", _engine.newQObject(obj));
_engine.evaluate("obj.destroyed.connect(function() { debug(\"obj destroyed\") })");
if (_engine.hasUncaughtException()) {
qDebug() << "Exception:" << _engine.uncaughtException().toString();
}
connect(obj, SIGNAL(destroyed()), this, SLOT(boo()));
QTimer *timer = new QTimer;
_engine.globalObject().setProperty("timer", _engine.newQObject(timer));
_engine.evaluate("timer.timeout.connect(function() { debug(\"timer timeout\"); obj.deleteLater(); })");
if (_engine.hasUncaughtException()) {
qDebug() << "Exception:" << _engine.uncaughtException().toString();
}
timer->setSingleShot(true);
timer->start(100);
}
void ScriptTester::boo() {
qDebug() << "was destroyed!";
}
Note that I don't want a hack like passing destroyed first to C++ code and then manually or by a signal informing script of that. I'm searching for an implementation that's done fully in the script.
I try to implement this: when app is started I need to create multiple threads that would use the same QDialog window to get messages from user. When thread is started, it asks user for input and if button OK pressed, it prints the message to console. I can't figure out why but I get dialog window only once and after that it prints my one message to console and application finishes.
Here's how I describe dialog window:
#include <QtWidgets>
class MyDialog : public QDialog
{
Q_OBJECT
public:
QWaitCondition* condition;
explicit MyDialog(QWidget *parent = 0);
signals:
void got_message(QString);
public slots:
void show_message_input();
void show_message();
private:
QLabel* message_label;
QVBoxLayout* vbox;
QHBoxLayout* hbox;
QLineEdit* message_input;
QDialogButtonBox* dialog_buttons;
};
MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
{
setModal(true);
message_label = new QLabel("Message");
message_input = new QLineEdit();
dialog_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
hbox = new QHBoxLayout();
hbox->addWidget(message_label);
hbox->addWidget(message_input);
vbox = new QVBoxLayout();
vbox->addLayout(hbox);
vbox->addWidget(dialog_buttons);
setLayout(vbox);
connect(dialog_buttons, SIGNAL(accepted()), this, SLOT(accept()));
connect(dialog_buttons, SIGNAL(rejected()), this, SLOT(reject()));
condition = new QWaitCondition();
}
void MyDialog::show_message_input()
{
int result = this->exec();
if (result == QDialog::Accepted)
{
emit got_message(message_input->text());
condition->wakeAll();
}
}
Here's MyThread class:
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(int id, MyDialog* window, QObject *parent = 0);
signals:
void show_input();
public slots:
void print_message(QString);
private:
static QMutex mutex;
static QMutex mutex2;
MyDialog* window;
int id;
void run();
void get_captcha_value();
};
QMutex MyThread::mutex;
QMutex MyThread::mutex2;
MyThread::MyThread(int id, MyDialog* window, QObject *parent) :
QThread(parent)
{
this->id = id;
this->window = window;
connect(this, SIGNAL(show_input()), this->window, SLOT(show_message_input()));
}
void MyThread::get_captcha_value()
{
QMutexLocker lock(&mutex);
connect(this->window, SIGNAL(got_message(QString)), SLOT(print_message(QString)));
emit show_input();
mutex2.lock();
window->condition->wait(&mutex2);
mutex2.unlock();
}
void MyThread::run()
{
mutex.lock();
qDebug() << "Starting thread " << id;
mutex.unlock();
get_captcha_value();
mutex.lock();
qDebug() << "Finishing thread " << id;
mutex.unlock();
}
void MyThread::print_message(QString message)
{
qDebug() << message;
QObject::disconnect(this, SLOT(print_message(QString)));
}
And main function:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog* window = new MyDialog();
QList<MyThread*> threads;
for(int i = 0; i < 5; i++)
{
MyThread* thread = new MyThread(i, window);
threads << thread;
thread->start();
}
return a.exec();
}
The first problem you have is that you're inheriting from QThread. Unless you're wanting to re-write how Qt handles threading, you're doing it wrong!.
What you need to do is have a class that inherits from QObject and move the instances to a new thread. The main problem from inheriting QThread is that it can cause confusion about thread affinity (which thread an object is running on).
Also, creating more threads than processor cores is just a waste of resources.
I suggest you read this article on how to use Qt threading and stop inheriting from QThread.
Finally, the use of QMutex is to protect multiple threads accessing the same data simultaneously. You should be able to remove all of them in the code you've shown. Emitting signals with data from one thread to be received by a slot on another thread is the preferred method in Qt.
QT 5.1.0rc2 , msvc 2010 , Microsoft Visual Studio 2010
It is working code on Qt 4.8.4 , msvc 2008
I have compile error at
#if defined( HANDLE_PROCESSING_IN_MAIN_THREAD )
if(QThread::currentThread() != this)
emit started();
#endif
inherited::run();
and
#if defined( HANDLE_PROCESSING_IN_MAIN_THREAD )
if(QThread::currentThread() != this)
emit finished();
#endif
error C2660: 'QThread::started' : function does not take 0 arguments
error C2660: 'QThread::finished' : function does not take 0 arguments
In QThread i have seen
Q_SIGNALS:
void started(
#if !defined(Q_QDOC)
QPrivateSignal
#endif
);
void finished(
#if !defined(Q_QDOC)
QPrivateSignal
#endif
);
when I defined Q_QDOC I got many errors in QT sources.
QPrivateSignal is empty structure that defined in macro Q_OBJECT
Need a solution that does not affect the architecture of the application, as to be backward compatible with Qt4.8.4
Some ideas?
The thread's signals are emitted automatically. You should never emit them manually.
You're trying to use preprocessor to handle two variants of the code: processing in the gui thread or a dedicated thread. Qt provides a very easy way of dealing with it.
Implement your processing functionality in a slot in a class deriving from QObject. You could also do it in the reimplemented event() method if it's easier for you to start processing by posting an event rather than invoking a slot.
If you want your object's slots to run in a different thread, use the moveToThread() method.
You don't need to derive from QThread. Its default implementation of run() spins a local event loop.
If you want your object to be compatible with living in the GUI thread, it must do its processing in small chunks and relinquish control so that the GUI doesn't stall.
Below is a complete example that demonstrates how you can start the worker object in either GUI or a separate thread, and how you can safely move it between threads.
// https://github.com/KubaO/stackoverflown/tree/master/questions/qobject-thread-18653347
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
/// See http://stackoverflow.com/a/40382821/1329652
bool isSafe(QObject * obj) {
Q_ASSERT(obj->thread() || qApp && qApp->thread() == QThread::currentThread());
auto thread = obj->thread() ? obj->thread() : qApp->thread();
return thread == QThread::currentThread();
}
class Helper : private QThread {
public:
using QThread::usleep;
};
class Worker : public QObject {
Q_OBJECT
int m_counter;
QBasicTimer m_timer;
void timerEvent(QTimerEvent * ev) override;
public:
Worker(QObject *parent = nullptr) : QObject(parent) {}
/// This method is thread-safe.
Q_SLOT void start() {
if (!isSafe(this)) return (void)QMetaObject::invokeMethod(this, "start");
if (m_timer.isActive()) return;
m_counter = 0;
m_timer.start(0, this);
}
/// This method is thread-safe.
Q_INVOKABLE void startInThread(QObject *targetThread) {
if (!isSafe(this)) return (void)QMetaObject::invokeMethod(this, "startInThread", Q_ARG(QObject*, targetThread));
QObject::moveToThread(qobject_cast<QThread*>(targetThread));
start();
}
Q_SIGNAL void done();
Q_SIGNAL void progress(int percent, bool inGuiThread);
};
void Worker::timerEvent(QTimerEvent * ev)
{
const int busyTime = 50; // [ms] - longest amount of time to stay busy
const int testFactor = 128; // number of iterations between time tests
const int maxCounter = 30000;
if (ev->timerId() != m_timer.timerId()) return;
const auto inGuiThread = []{ return QThread::currentThread() == qApp->thread(); };
QElapsedTimer t;
t.start();
while (1) {
// do some "work"
Helper::usleep(100);
m_counter ++;
// exit when the work is done
if (m_counter > maxCounter) {
emit progress(100, inGuiThread());
emit done();
m_timer.stop();
break;
}
// exit when we're done with a timed "chunk" of work
// Note: QElapsedTimer::elapsed() may be expensive, so we call it once every testFactor iterations
if ((m_counter % testFactor) == 0 && t.elapsed() > busyTime) {
emit progress(m_counter*100/maxCounter, inGuiThread());
break;
}
}
}
class Window : public QWidget {
Q_OBJECT
QVBoxLayout m_layout{this};
QPushButton m_startGUI{"Start in GUI Thread"};
QPushButton m_startWorker{"Start in Worker Thread"};
QLabel m_label;
QThread m_thread{this};
Worker m_worker;
Q_SLOT void showProgress(int p, bool inGuiThread) {
m_label.setText(QString("%1 % in %2 thread")
.arg(p).arg(inGuiThread ? "gui" : "worker"));
}
Q_SLOT void on_startGUI_clicked() {
m_worker.startInThread(qApp->thread());
}
Q_SLOT void on_startWorker_clicked() {
m_worker.startInThread(&m_thread);
}
public:
Window(QWidget *parent = {}, Qt::WindowFlags f = {}) : QWidget(parent, f) {
m_layout.addWidget(&m_startGUI);
m_layout.addWidget(&m_startWorker);
m_layout.addWidget(&m_label);
m_thread.start();
connect(&m_worker, SIGNAL(progress(int,bool)), SLOT(showProgress(int,bool)));
connect(&m_startGUI, SIGNAL(clicked(bool)), SLOT(on_startGUI_clicked()));
connect(&m_startWorker, SIGNAL(clicked(bool)), SLOT(on_startWorker_clicked()));
}
~Window() {
m_thread.quit();
m_thread.wait();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
#include "main.moc"
I have a simple setup to change a label on a timed interval, for testing purposes. It seems that the signal does not ever get emitted. I'm using Visual Studio 2010 with the Qt add-in. Here is my setup...
Window::Window(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
my_label = new QLabel();
timer = new QTimer(this);
timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame()));
}
void Window::nextFrame()
{
static int i = 0;
std::stringstream ss;
ss << "C:/files/" << i << ".txt";
QString qstr = QString::fromStdString(ss.str());
ui.label->setText(qstr);
ss.str("");
i++;
repaint();
}
And in the header file,
public:
Window(QWidget *parent = 0, Qt::WFlags flags = 0);
~Window();
public slots:
void nextFrame();
private:
Ui::TrackerClass ui;
QTimer *timer;
};
Why is the slot nextFrame() never being triggered?
There is nothing in this code which calls start() and so based on this code nextFrame() would never be triggered by timeout().
Couple of things I think I can help with:
nextFrame() is a SLOT. SLOTs don't emit. They receive. SIGNALS emit. Not trying to be rude, just want to be clear on this as it's an important distinction. (in this case the signal is timeout() )
you need to have a start() to begin the timer. something like the line below would seem to do the trick:
Hope this helps clear up some confusion.
timer->start(1000);
im using Qt QProgressBar and place it in the statusBar on my main window
Like this in the constructor :
pb = new QProgressBar(statusBar());
pb->setTextVisible(false);
pb->hide();
statusBar()->addPermanentWidget(pb);
then im running procsses (web page loadding in this case )
and trying to show the progress with :
connect(ui.webView, SIGNAL(loadProgress(int)), SLOT(setProgress (int)));
void myMainWindow:: setProgress(int progress)
{
pb->show();
pb->setRange(0,100);
pb->setValue(progress);
}
But im getting Unhandled exception when it comes to pb->show()
I guess it has to do something with loading the parent main windows and then the progress bar
I was reading about the QAbstractEventDispatcher and processEvents but not understood how to implement it .
i did small test and put the pb->show() function call in button click signal/slut
that means im triggering the pb->show() after the web page and the mainwindows fully loaded and its working fine without the exception. that make me belive there is problem
with the event processing.
here is the class :
class MainWindowMainWindowContainer : public QMainWindow
{
Q_OBJECT
public:
MainWindowContainer(QWidget *parent = 0);
public slots:
void adjustLocation();
void changeLocation();
void adjustTitle();
void setProgress(int p);
void finishLoading(bool);
void finishedSlot(QNetworkReply* reply);
private:
Ui::OnLineBack_mainWindow ui;
int progress;
void createWebViewActions();
QProgressBar *pb;
void setprogressBar(int progress,bool show);
};
MainWindowContainer::MainWindowContainer(QWidget* parent) :
QMainWindow(parent),
{
ui.setupUi(this);
progress = 0;
createWebViewActions();
ui.webView->load(QUrl("www.cnnnn.com"));
ui.webView->show();
pb = new QProgressBar(statusBar());
pb->setTextVisible(false);
pb->hide();
statusBar()->addPermanentWidget(pb);
}
void MainWindowContainer::createWebViewActions()
{
connect(ui.webView, SIGNAL(loadFinished(bool)), SLOT(adjustLocation()));
connect(ui.webView, SIGNAL(titleChanged(QString)), SLOT(adjustTitle()));
connect(ui.webView, SIGNAL(loadProgress(int)), SLOT(setProgress(int)));
connect(ui.webView, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));
connect(ui.webView, SIGNAL(linkClicked(const QUrl&)),this, SLOT(linkClicked(const QUrl&)));
}
void MainWindowContainer::setProgress(int p)
{
progress = p;
adjustTitle();
}
void MainWindowContainer::adjustTitle()
{
qApp->processEvents();
pb->show();
if (progress <= 0 || progress >= 100)
{
QString titl = ui.webView->title();
statusBar()->showMessage(titl);
setprogressBar(-1,false);
}
else
{
statusBar()->showMessage(QString("%1 (%2%)").arg(ui.webView->title()).arg(progress));
setprogressBar(progress,true);
}
}
void MainWindowContainer::finishLoading(bool)
{
progress = 100;
adjustTitle();
}
void MainWindowContainer::setprogressBar(int progress,bool show)
{
if(show)
{
pb->show();
pb->setRange(0,100);
pb->setValue(progress);
}
else
{
pb->hide();
}
}
In your createWebViewActions() function you connect the signals to their respective slots. (One small remark, the connect for the titleChanged(QString) signal and adjustTitle() slot fails because they have different signatures)
Among others you are connecting the signal loadProgress(int) to slot setProgress(int). In this slot you call adjustTitle() where the instruction pb->show() is being executed.
Notice that you are calling the createWebViewActions() function before the call to QProgressBar constructor
(...)
createWebViewActions(); <------ here you establish the signal->slot connections
ui.webView->load(QUrl("www.cnnnn.com"));
ui.webView->show();
pb = new QProgressBar(statusBar()); <------ here you call the QProgressBar constructor
pb->setTextVisible(false);
pb->hide();
statusBar()->addPermanentWidget(pb);
(...)
I think that maybe this slot (setProgress()) is being called before the QProgressBar is constructed which triggers the Unhandled exception you are getting.
You could try and move the call to the QProgressBar constructor so that it is created before the slots connection.