How to replace old C code (printf) with C++/Qt for GUI application?
-
I have a code that was written some years ago for command line. There are lots of printf codes that was writing on screen what is happening. Now I want to replace all printf codes and I want to show everything on GUI.
int FLASH_Image(char *fname, unsigned StartAddr, unsigned imgSize) { .... for (i = StartSector; i < StartSector + NrOfSectors; i++) { printf("\rErasing sector %02d ...",i); FLASH_SectorErase((unsigned)i*SECTORSIZE); } .... }
If I use a parameter
char *msg
pointer for text messages, it will be "valid" after function ends. I want to "update" it during function runs. How is it possible? -
Hi! Are you familiar with Qt's concept of signals and slots? Your function could emit a signal that contains a string with the current text to be displayed. Your displaying UI component can be connected to that signal.
-
@Wieland Yes I am familiar with signal/slots. But the problem is that there is no class in those old C code . Without object how can I emit a signal? It doesn't work even if I declare signal functions as static.
I have a UDP class in UDP.h, I have this old C code in flash.h and implementation in flash.c, Mainwindow.h includes flash.h, and flash.h includes udp.h, the include hieararchy is so:udp.h (with object, signals and slots) | V flash.h (wihtout objects, only func declarations) | V mainwindow.h
-
Replace printf() with your own function which then passes the strings to main window.
You could even name it printf() and remove the include <stdio.h> (and include your header file) from your C code, then you do not have to change anything else :-) -
I wouldn't mix UI components and business logic, e.g. access the MainWindow from flash.cpp. IMAO it's better to create an adapter for message passing:
msgadapter.h
#ifndef MSGADAPTER_H #define MSGADAPTER_H #include <QtCore> class MsgAdapter : public QObject { Q_OBJECT public: MsgAdapter(QObject *parent = 0); public: void printf(const char *format, ...); signals: void msg(QString); }; #endif // MSGADAPTER_H
msgadapter.cpp
#include "msgadapter.h" MsgAdapter::MsgAdapter(QObject *parent) : QObject(parent) { } void MsgAdapter::printf(const char *format, ...) { va_list args; va_start(args, format); char *result; const int n = vasprintf(&result, format, args); va_end(args); if (n>=0) { emit msg(result); free(result); } }
flash.h
#ifndef FLASH_H #define FLASH_H class MsgAdapter; void setMsgAdapter(MsgAdapter *msgAdapter); void FLASH_Image(); #endif // FLASH_H
flash.cpp
#include "flash.h" #include "msgadapter.h" static MsgAdapter *g_msgAdapter = 0; void setMsgAdapter(MsgAdapter *msgAdapter) { g_msgAdapter = msgAdapter; } void FLASH_Image() { g_msgAdapter->printf("\rErasing sector %02d ...", 23); }
In mainwindow.cpp
#include "msgadapter.h" #include "flash.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); MsgAdapter * msgAdapter = new MsgAdapter(this); setMsgAdapter(msgAdapter); connect(msgAdapter, SIGNAL(msg(QString)), ui->label, SLOT(setText(QString))); } MainWindow::~MainWindow() { setMsgAdapter(0); delete ui; }