Reputation: 2584
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!
Upvotes: 0
Views: 2023
Reputation: 2584
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);
}
Upvotes: 1
Reputation: 98425
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 = ...;
}
};
Upvotes: 3
Reputation: 1320
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.
Upvotes: -1