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. Send a text to textEdit from a non qt cpp file
Qt 6.11 is out! See what's new in the release blog

Send a text to textEdit from a non qt cpp file

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 1.1k 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.
  • JonBJ JonB

    @saeid0034
    There are no files here in code. Do you mean send something from these .cpp files??

    Would you be prepared to include QObject stuff here so that you can send signals back to your Qt code?

    It's hard to say, maybe somebody else knows what you are asking because I am struggling.

    As I said, this code has nothing at all to do with Qt, and I have no idea how or why you're using it your Qt app....

    S Offline
    S Offline
    saeid0034
    wrote on last edited by saeid0034
    #5

    @JonB
    I know I cant explain my problem right and im sorry about that
    the only thing I want is send a text from a cpp file that include my codes to mainwindow ui
    I tried this, but its wont work
    in CreateRemoteThread_Type1 header

    #pragma once
    
    #include <QObject>
    
    class CreateRemoteThread_c: public QObject
    {
    	Q_OBJECT
    
    public:
    	CreateRemoteThread_c(QObject *parent = Q_NULLPTR);
    	void SendMSG(int error, QString str_2, QString str);
    
    signals:
    	void updateUI(int error, QString str_2, QString str);
    };
    

    in in CreateRemoteThread_Type1 cpp file

    #include "Injector.h"
    #include "CreateRemoteThread_Type1.h"
    
    CreateRemoteThread_c::CreateRemoteThread_c(QObject *parent)
    	: QObject(parent)
    {
    }
    
    void CreateRemoteThread_c::SendMSG(int error, QString str_2, QString str)
    {
    	emit updateUI(error, str_2, str);
    }
    
    bool CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess)
    {
    	CreateRemoteThread_c Sender;
    	Sender.SendMSG(0, "string", "string 2");
    
    }
    

    and after that I include CreateRemoteThread_Type1.h in my mainwindow header
    also Im created a slot in mainwindow header and created it in cpp file too

    private slots:
        void AppendToTextEdit(int error, QString str_2, QString str);
    

    then connect updateUI signal to AppendToTextEdit slot in mainwindow::mainwindow(QWidget *parent) in my mainwindow.cpp

    CreateRemoteThread_c* MsgSender = new CreateRemoteThread_c;
    
    	QObject::connect(MsgSender, &CreateRemoteThread_c::updateUI, this, &mainwindow::AppendToTextEdit);
    

    but slot never called
    sorry for long text, can you please tell me what is my problem?

    JonBJ 1 Reply Last reply
    0
    • S saeid0034

      @JonB
      I know I cant explain my problem right and im sorry about that
      the only thing I want is send a text from a cpp file that include my codes to mainwindow ui
      I tried this, but its wont work
      in CreateRemoteThread_Type1 header

      #pragma once
      
      #include <QObject>
      
      class CreateRemoteThread_c: public QObject
      {
      	Q_OBJECT
      
      public:
      	CreateRemoteThread_c(QObject *parent = Q_NULLPTR);
      	void SendMSG(int error, QString str_2, QString str);
      
      signals:
      	void updateUI(int error, QString str_2, QString str);
      };
      

      in in CreateRemoteThread_Type1 cpp file

      #include "Injector.h"
      #include "CreateRemoteThread_Type1.h"
      
      CreateRemoteThread_c::CreateRemoteThread_c(QObject *parent)
      	: QObject(parent)
      {
      }
      
      void CreateRemoteThread_c::SendMSG(int error, QString str_2, QString str)
      {
      	emit updateUI(error, str_2, str);
      }
      
      bool CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess)
      {
      	CreateRemoteThread_c Sender;
      	Sender.SendMSG(0, "string", "string 2");
      
      }
      

      and after that I include CreateRemoteThread_Type1.h in my mainwindow header
      also Im created a slot in mainwindow header and created it in cpp file too

      private slots:
          void AppendToTextEdit(int error, QString str_2, QString str);
      

      then connect updateUI signal to AppendToTextEdit slot in mainwindow::mainwindow(QWidget *parent) in my mainwindow.cpp

      CreateRemoteThread_c* MsgSender = new CreateRemoteThread_c;
      
      	QObject::connect(MsgSender, &CreateRemoteThread_c::updateUI, this, &mainwindow::AppendToTextEdit);
      

      but slot never called
      sorry for long text, can you please tell me what is my problem?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #6

      @saeid0034
      Glancing through your code, it looks reasonable. As ever, while developing use qDebug() messages to verify your signal is being emitted, received etc.

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @saeid0034
        Glancing through your code, it looks reasonable. As ever, while developing use qDebug() messages to verify your signal is being emitted, received etc.

        S Offline
        S Offline
        saeid0034
        wrote on last edited by saeid0034
        #7

        @JonB said in Send a text to textEdit from a non qt cpp file:

        Glancing through your code, it looks reasonable.

        as you said I check the code with qDebug()
        SendMSG function called but seems like signal doesn't emit
        I dont know what is my problem
        edit
        yes, I tested connect with lambda and tried to show a massage when signal received, but nothing happened.

        JonBJ 1 Reply Last reply
        0
        • S saeid0034

          @JonB said in Send a text to textEdit from a non qt cpp file:

          Glancing through your code, it looks reasonable.

          as you said I check the code with qDebug()
          SendMSG function called but seems like signal doesn't emit
          I dont know what is my problem
          edit
          yes, I tested connect with lambda and tried to show a massage when signal received, but nothing happened.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #8

          @saeid0034
          Look, I'm not sure [actually, I am sure], but: in CreateRemoteThread_Type1 you have a CreateRemoteThread_c Sender; local variable which is the signal sender. Meanwhile in the connect() you have a new CreateRemoteThread_c object, MsgSender. What is going on here? The connection must be from the object which sends the signal, and I think you have two completely separate objects here??

          S 1 Reply Last reply
          1
          • JonBJ JonB

            @saeid0034
            Look, I'm not sure [actually, I am sure], but: in CreateRemoteThread_Type1 you have a CreateRemoteThread_c Sender; local variable which is the signal sender. Meanwhile in the connect() you have a new CreateRemoteThread_c object, MsgSender. What is going on here? The connection must be from the object which sends the signal, and I think you have two completely separate objects here??

            S Offline
            S Offline
            saeid0034
            wrote on last edited by saeid0034
            #9

            @JonB
            oh, yes you right, but one question how can I use one object for both mainwindow and CreateRemoteThread_Type1?

            JonBJ 1 Reply Last reply
            0
            • S saeid0034

              @JonB
              oh, yes you right, but one question how can I use one object for both mainwindow and CreateRemoteThread_Type1?

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #10

              @saeid0034
              I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

              Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

              S 1 Reply Last reply
              1
              • JonBJ JonB

                @saeid0034
                I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

                Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

                S Offline
                S Offline
                saeid0034
                wrote on last edited by
                #11

                @JonB said in Send a text to textEdit from a non qt cpp file:

                @saeid0034
                I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

                Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

                thank you for all your help and Sorry for bother you so much
                at the end I think my best solution is change my code, one last question😅
                you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

                JonBJ 1 Reply Last reply
                0
                • S saeid0034

                  @JonB said in Send a text to textEdit from a non qt cpp file:

                  @saeid0034
                  I knew you were going to ask that next! :) I'm afraid, that's your problem! I don't know enough about how your CreateRemoteThread_c code/object(s) fit together with your main window code. Do you create CreateRemoteThread_c instances more than once? If there really is only one you could use a singleton pattern. Or, main window can create it, I don't know what your non-class function CreateRemoteThread_Type1 is about, why you needed that.

                  Only you know how your code relates. Whatever, the instance that does the emit of the signal must be the same instance as main window specifies in its connect() statement, that is what is vital.

                  thank you for all your help and Sorry for bother you so much
                  at the end I think my best solution is change my code, one last question😅
                  you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #12

                  @saeid0034 said in Send a text to textEdit from a non qt cpp file:

                  you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

                  This is not ideal! You really want to keep separation between a Qt main window/program and some piece of Windows-specific code to do goodness what. Try to make a self-contained class out of what you have with this code. But this is a general programming issue.

                  S 1 Reply Last reply
                  1
                  • JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by JoeCFD
                    #13

                    send out your text via standard output std::cout in your cpp code and catch it in Qt code.
                    https://forum.qt.io/topic/80024/how-to-redirect-output-of-stdout-to-gui-qtextedit-shell-from-one-process-to-another-process/2

                    1 Reply Last reply
                    1
                    • S Offline
                      S Offline
                      saeid0034
                      wrote on last edited by saeid0034
                      #14

                      I manage to fix it wait add in function deceleration from worker.h to mainwindow.h
                      know I can send signal and receive it
                      I'm still don't know if its a good way to do this...

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        saeid0034
                        wrote on last edited by
                        #15

                        one other thing
                        know I can access ui from all c++ file I declared in header
                        its bad? and why?

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @saeid0034 said in Send a text to textEdit from a non qt cpp file:

                          you think if I include all my needed function from CreateRemoteThread_Type1 and SetPrivilege in my mainwindow code its okay?

                          This is not ideal! You really want to keep separation between a Qt main window/program and some piece of Windows-specific code to do goodness what. Try to make a self-contained class out of what you have with this code. But this is a general programming issue.

                          S Offline
                          S Offline
                          saeid0034
                          wrote on last edited by saeid0034
                          #16

                          @JonB
                          Hi, I worked with it a little more and I come up with this codes

                          worker.h

                          class Worker : public QObject
                          {
                          	Q_OBJECT
                          
                          public:
                          	bool GetOSInfo();
                          	bool SetPrivilege(HANDLE hToken, LPCTSTR Privilege, BOOL bEnablePrivilege);
                          	void DisplayError(LPCSTR szAPI);
                          	int EscalatePrivilege();
                          	bool CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess);
                          
                          signals:
                          	void updateUI(const int error, const QString text, const QString value);
                          
                          };
                          

                          CreateRemoteThread.cpp

                          bool Worker::CreateRemoteThread_Type1(LPCSTR ID, HANDLE hProcess) {
                          //some code
                          	emit updateUI(0, "text", Null);
                          }
                          

                          mainwindow.h

                          private slots:
                              void Reciver(const int error, const QString text, const QString value);
                          

                          and mainwindow.cpp

                          Worker worker; //global
                          
                          Mainwindow::Mainwindow(QWidget *parent)
                              : QMainWindow(parent)
                          {
                              ui.setupUi(this);
                          		
                          	QObject::connect(&worker, &Worker::updateUI, this, &mainwindow::Reciver);
                          }
                          
                          //some code
                          
                          void mainwindow::Reciver(const int error, const QString text, const QString value)
                          {
                          	ui.textEdit->append(text);
                          }
                          

                          its working and slot Trigger when signal fired
                          I only want to know there is any better way then declare Worker globally in mainwindow? (i declare it globally because I need it some other function other then main)
                          and one other thing, after all this code ideal?

                          thanks for all your helps and sorry for bothering you so many times

                          1 Reply Last reply
                          0

                          • Login

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