Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to replace old C code (printf) with C++/Qt for GUI application?
QtWS25 Last Chance

How to replace old C code (printf) with C++/Qt for GUI application?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kahlenbergK Offline
    kahlenbergK Offline
    kahlenberg
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • kahlenbergK Offline
        kahlenbergK Offline
        kahlenberg
        wrote on last edited by
        #3

        @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
        
        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 :-)

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #5

            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;
            }
            
            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved